Export a single Confluence page as PDF (Python)

5k views Asked by At

I have a python script which is trying to export a confluence page as pdf and have tried several methods unsuccessfully:

1.WGET:

wget --ask-password --user xxxxxxxx -O out.pdf -q http://confluence.xxxx.com/spaces/flyingpdf/pdfpageexport.action?pageId=xxxxxxxx

This won't work because it just returns a login dialog rather than the actual pdf.

2.REMOTE API:

Using: https://developer.atlassian.com/confdev/deprecated-apis/confluence-xml-rpc-and-soap-apis/remote-confluence-methods#RemoteConfluenceMethods-Pages

There is an exportSpace method which works but I only want a single page, the getPage method doesn't export to pdf as far as I can tell. Also this is technically deprecated so Atlassian instead recommends:

3.REST API

Using: https://docs.atlassian.com/atlassian-confluence/REST/latest-server/

This doesn't seem to have an option to export a page as PDF

I would appreciate an answer that makes any of these methods work or if you have a completely different approach, I don't care as long as I can get the PDF of the page from a python script.

4

There are 4 answers

0
Dren79 On

The newest docs use confluence.export_page(page_id) For anyone still looking for this info.

0
Michael Pillai On
#This worked for me
#pip install atlassian-python-api
from atlassian import Confluence

#This creates connection object where you provide your confluence URL  and credentials.
confluence = Confluence(
    url='https://confluence.xxxxx.com/',
    username='xxxxxxx',
    password='yyyyyyy')

# If you know page_id of the page, you can get page id by going to "Page Information" menu tab and the page id will be visible in browser as viewinfo.action?pageId=244444444444. This will return a response having key:value pairs having page details.
page = confluence.get_page_by_id(page_id=<some_id>)
your_fname = "abc.pdf"
#A function to create pdf from byte-stream responce
def save_file(content):
    file_pdf = open(your_fname, 'wb')
    file_pdf.write(content)
    file_pdf.close()
    print("Completed")

#Get your confluence page as byte-stream
response = confluence.get_page_as_pdf(page['id'])
#Call function that will create pdf and save file using byte-stream response you received above.
save_file(content=response)
0
Saurabh Gupta On

You can integrate your script with Confluence Bob Swift CLI plugin. This plugin supports various type of exports.

Step 1: Install the plugin in both places the frontend and backend.

Step 2: Verify your installation by this command -

/location-of-plugin-installation-directory/.confluence.sh --action getServerInfo

Step 3: Use the command below to export your space

/location-of-plugin-installation-directory/.confluence.sh --action exportSpace  --space "zconfluencecli"  --file "target/output/export/exportSpacepdf.txt"  --exportType "PDF"

Link to bob swift plugin

0
fhgd On

Based on the answer of Michael Pillai I want to amend that for the Confluence Cloud you must add the api_version='cloud' keyword:

confluence = Confluence(
    url='https://confluence.xxxxx.com/',
    username='xxxxxxx',
    password='yyyyyyy',
    api_version='cloud'  # <<< important for the pdf export <<<<
)
content = confluence.get_page_as_pdf(page_id)

Copied from the official pdf export example.