Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a Confluence "Space" to PDF using remote API

How can I export a Confluence 'space' as a pdf? It looks like it might still be supported in Confluence 5.0 using the XML-RPC API. I cannot find an example of what to call, though.

https://developer.atlassian.com/display/CONFDEV/Remote+API+Specification+for+PDF+Export#RemoteAPISpecificationforPDFExport-XML-RPCInformation

That link says calls should be prefixed with pdfexport, but then doesn't list any of the calls or give an example.

like image 921
kevzettler Avatar asked Jan 31 '14 18:01

kevzettler


People also ask

How do I export Confluence space to PDF?

If you've got permission to view the page in Confluence, you'll be able to export it in this way; go to the page and choose either Tools > Export to Word or Tools > Export to PDF.

Is there an API for Confluence?

Confluence's REST API provides an advanced search that lets you use structured queries to search for content within Confluence. Your search results will take the same form as the Content model returned by the Content REST API.


1 Answers

This works using Bob Swift's SOAP library ('org.swift.common:confluence-soap:5.4.1'). I'm using this in a gradle plugin, so you'll need to change a few things

void exportSpaceAsPdf(spaceKey, File outputFile) {
    // Setup Pdf Export Service
    PdfExportRpcServiceLocator serviceLocator = new PdfExportRpcServiceLocator()
    serviceLocator.setpdfexportEndpointAddress("${url}/rpc/soap-axis/pdfexport")
    serviceLocator.setMaintainSession(true)
    def pdfService = serviceLocator.getpdfexport()

    // Login
    def token = pdfService.login(user, password)

    // Perform Export
    def pdfUrl = pdfService.exportSpace(token, spaceKey)

    // Download Pdf
    HttpClient client  = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(pdfUrl)
    httpget.addHeader(
            BasicScheme.authenticate(
                    new UsernamePasswordCredentials(user,password),"UTF-8", false))
    HttpResponse response = client.execute(httpget)
    HttpEntity entity = response.getEntity()

    if (entity != null) {
        InputStream inputStream = entity.getContent()
        FileOutputStream fos = new FileOutputStream(outputFile)
        int inByte
        while ((inByte = inputStream.read()) != -1)
            fos.write(inByte)
        inputStream.close()
        fos.close()
    } else {
        throw new GradleException("""Cannot Export Space to PDF:
        Space:  ${spaceKey}
        Dest:   ${outputFile.absolutePath}
        URL:    ${pdfUrl}
        Status: ${response.getStatusLine()}
        """)
    }

}
like image 131
Peter Kahn Avatar answered Sep 21 '22 15:09

Peter Kahn