atlassian-api / atlassian-python-api

Atlassian Python REST API wrapper
https://atlassian-python-api.readthedocs.io
Apache License 2.0
1.37k stars 664 forks source link

Exporting as pdf #1332

Open Kruttifrutti opened 9 months ago

Kruttifrutti commented 9 months ago

Hi,

Trying to export as pdf, but only getting an empty pdf.

`def save_file(content, title): file_pdf = open(title + ".pdf", "w") file_pdf.write(content) file_pdf.close() print("Completed")

if name == "main": label = "apitesting" pages = confluence.get_all_pages_by_label(label=label, start=0, limit=10) for page in pages: response = confluence.get_page_as_pdf(page["id"]) save_file(content=response, title=page["title"])`

Error message suggests write() argument must be str, not bytes.

Any clues?

ashishpatel1992 commented 9 months ago

You should open file as wb instead of w wb allows files to be opened for writing as binary

Kruttifrutti commented 9 months ago

You should open file as wb instead of w wb allows files to be opened for writing as binary

Thanks for helping out, unfortunately, I am still getting a corrupted file...

ashishpatel1992 commented 9 months ago

Quick check on this, I have tested the code and it is able to generate the pdf for me. Possibly you need to first check if authentication is working correctly or not.

Sharing the code snippet which worked for me

from atlassian import Confluence

def save_file(content, title):
    file_pdf = open(title + ".pdf", "wb")
    file_pdf.write(content)
    file_pdf.close()
    print("Completed")

secret_key="<your_confluence_secret_key>"
label = "apitesting"
confluence = Confluence(
        url='https://<your_confluence_url>',
        token=secret_key)
pages = confluence.get_all_pages_by_label(label=label, start=0, limit=10)
for page in pages:
    response = confluence.get_page_as_pdf(page["id"])
    save_file(content=response, title=page["title"])

Also I have used adobe acrobat to verify the files

frankiedrake commented 9 months ago
label

I have a problem with the export API. I did provide a token and I can successfully get page content etc., but export to pdf returns the HTML content of a login page. Here's my code:

confluence = Confluence(
    url='https://my.company.confluence/',
    token=token)

page = confluence.get_page_by_id(11111, expand='body.storage') # WORKS WELL
print(page['title'])

response = confluence.get_page_as_pdf(68258487)
print(response) # PRINTS Login page content
ashishpatel1992 commented 9 months ago

@frankiedrake It could be possible that your confluence API url is incorrect. thats why you are seeing Login page as HTML result. Check with your administrator for correct url.