Add bookmarks to PDF with PHP

July 14th, 2021 / by api2pdf /


If you are working with PDFs as a php 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: — Ideally a file storage provider like S3 or Azure Blob Storage. See the code sample below.

Add bookmarks to PDF files with PHP

Step 1) Install the PHP client library from github:

Step 2) Grab an API key from 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.

require_once 'your-own-directory/Api2Pdf.php';
$apiClient = new Api2Pdf('YOUR-API-KEY'); 
$linkToPdf = '';
$bookmarks = [
    [ "Page" => 0, "Title" => "Introduction" ]
$result = $apiClient->pdfsharpAddBookmarks($linkToPdf, $bookmarks);
echo $result->getFile();

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 php code.

See full github library

We have a whole php based client library for our API that does a lot more than just this. Check out the full library capabilities here:

Tags: , ,

Comments are closed.