Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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.

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
Community showcase
Published in Confluence

Announcing Team Calendars in Confluence Data Center

Hi Community! We're thrilled to share that Team Calendars for Confluence is now a built-in feature for Confluence Data Center releases 7.11 and beyond.  A long time favorite,  Team Cale...

101 views 0 3
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you