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 :)
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
with warm regards
ramki
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.