Add bookmarks to PDF with Python

July 14th, 2021 / by api2pdf /

Intro

If you are working with PDFs as a Python developer, you know the pain associated with working with them. In this post we will cover how to conveniently add bookmarks to a PDF using our REST API in just a few lines of code.

Our API will take your .pdf file and return back to you a new .pdf file with the bookmarks added. Just make sure your PDF is saved as a .pdf file and accessible at a URL that our service can ingest. For example, see this: http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf — Ideally a file storage provider like S3 or Azure Blob Storage. See the code sample below.

Add bookmarks to PDFs with Python

Step 1) Open up your package manager and run the command

pip install api2pdf

Step 2) Grab an API key from https://portal.api2pdf.com. Only takes 60 seconds.

Step 3) Use the sample code below and replace “YOUR-API-KEY” with the api key you acquired in step 2.

from api2pdf import Api2Pdf

a2p_client = Api2Pdf('YOUR-API-KEY')
url = 'https://link-to-pdf'
bookmarks = [
    { 'Page': 0, 'Title': 'Introduction' },
    { 'Page': 1, 'Title': 'Second page' }
]
response = a2p_client.PdfSharp.add_bookmarks(url, bookmarks)

And that’s it! Modify the code as you see fit. Hopefully this saves you time and makes adding bookmarks to PDF files easy and painless for those writing python code.

See full github library

We have a whole python based client library for our API that does a lot more than just this. Check out the full library capabilities here: https://github.com/Api2Pdf/api2pdf.python

Tags: , ,

Comments are closed.