Script Runner for Confluence - How to export a page to PDF or HTML with styling and inline images?
Asked by Firas Hermez:

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.CustomEndpointDelegateimport groovy.json.JsonBuilderimport groovy.transform.BaseScriptimport javax.ws.rs.core.MultivaluedMapimport javax.ws.rs.core.Responseimport javax.ws.rs.core.MediaTypeimport com.atlassian.confluence.pages.PageManagerimport com.atlassian.sal.api.component.ComponentLocatorimport java.lang.RuntimeException@BaseScript CustomEndpointDelegate delegate//End point name is the same as the method's namegetapipage(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 :)


Answers:
author: Jonny Carter
body: 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.PageManagerimport com.atlassian.confluence.setup.settings.SettingsManagerimport com.atlassian.sal.api.component.ComponentLocatorimport com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegateimport groovy.transform.BaseScriptimport javax.ws.rs.core.MultivaluedMapimport javax.ws.rs.core.Response@BaseScript CustomEndpointDelegate delegate//End point name is the same as the method's namegetapipage(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.

comments: author: Ramakrishnan Srinivasan
body: Hi Jonny Carter,

Thank you for your answer, keeping that as a base, I could complete customization of pdf export with some workaround way.

For other users who might visit this page, I created a discussion thread here

<https://community.atlassian.com/t5/Confluence-discussions/Customize-pdf-export-using-Script-Runner-for-Confluence-Server/td-p/1470881>

with warm regards

ramki


Tags: scriptrunner

adaptavist

addon-com.onresolve.confluence.groovy.groovyrunner

atlassian-marketplace

export