Script Runner for Confluence - How to export a page to PDF or HTML with styling and inline images?

I am trying to write a script which can be used either as part of a REST api endpoint or as a scheduled job to export the full content of a page (read: layout and Images) either as a file (preferably PDF) or as (X)HTML.

The script I've got so far seems to only export basic content elements with no CSS formatting and no inline images, below is a copy of my script code:

 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import javax.ws.rs.core.MediaType

import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator


import java.lang.RuntimeException

@BaseScript CustomEndpointDelegate delegate

//End point name is the same as the method's name
getapipage(httpMethod: "GET", groups: ["confluence-administrators"]) { MultivaluedMap queryParams, String body ->
    
    def pageManager = ComponentLocator.getComponent(PageManager)

    def page = pageManager.getPage("SPACE NAME","PAGE TITLE")
    return Response.ok(page.getBodyAsString(),MediaType.TEXT_HTML).build()

I've looked at using : DefaultXhtmlContent.convertWikiToView but I am not entirely sure if that is the way to go.

 

Any help or tips would be greatly appreciated :)

1 answer

This widget could not be displayed.

If you're looking for a PDF export, you could probably use Confluence's built-in PDF exporter.

$confluenceBaseURL/spaces/flyingpdf/pdfpageexport.action?pageId=$yourPageID

You'd need to get the page ID, though, and it looks like you're working with the space / page title combination. So your script could do the work of getting the page and redirecting you to the built-in PDF exporter.

import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.setup.settings.SettingsManager
import com.atlassian.sal.api.component.ComponentLocator
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

//End point name is the same as the method's name
getapipage(httpMethod: "GET", groups: ["confluence-administrators"]) { MultivaluedMap queryParams, String body ->

    def pageManager = ComponentLocator.getComponent(PageManager)

    def spaceKey = queryParams.getFirst("space").toString()
    def title = queryParams.getFirst("title").toString()
    log.debug("$spaceKey $title")
    def page = pageManager.getPage(spaceKey, title)

    def baseUrl = ComponentLocator.getComponent(SettingsManager).globalSettings.baseUrl
    def pdfUrl = "$baseUrl/spaces/flyingpdf/pdfpageexport.action?pageId=${page?.idAsString}"

    return Response.temporaryRedirect(new URI(pdfUrl)).build()
}

The redirect is a bit tedious, but it saves you the trouble of having to build an HTTP request, properly authenticate, and handle the SSL certs.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 30, 2018 in Confluence

How Manon Soubies-Camy uses Confluence + Trello to organize webinar notes and her knitting stash

@Manon Soubies-Camy is an engineer who has been an avid Atlassian user since 2014. She helps companies of all sizes transform the way they work with the Atlassian stack, including Jira and Confl...

861 views 8 13
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you