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

Firas Hermez April 10, 2017

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

1 vote
Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 16, 2017

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.

Ramakrishnan Srinivasan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 1, 2020

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events