Need to merge multiple pdf's into a single PDF with Table Of Contents sections

Will have 50-100 single PDF’s that we’ll be generating with a php script. PDF’s are generally grouped into groups of 10-20. Each group needs to have it’s own Table of Contents or Index, and then there also needs to be a Master Table of Contents or Index at the beginning.

Or if that is too difficult we could get away with a single Table of Contents at the beginning.

What’s the best way to go about this?

Will we need to create the Table of Contents and then export that to PDF and append it to the beginning and mash the rest of the files after that? Or is there a better solution?

And what’s the best tool for us to merge the pdf’s?

Will be running on a Linux server.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

With Version 1.45 – December 6, 2012 is pdftk able to create bookmarks with update_info, which could be used as toc.

Its done in 3 Steps:

Precondition for my example

3 PDF files. Single page.


1. Create the bookmark information

# build the bookmark out of an example file
pdftk page1.pdf dump_data output meta.txt
# Edit meta.txt as you need

Heres an example that worked for me, meta.txt:

InfoKey: Creator
InfoValue: PDFTK
NumberOfPages: 3
PageMediaNumber: 1
PageMediaRotation: 0
PageMediaRect: 0 0 595.32 841.92
PageMediaDimensions: 595.32 841.92
BookmarkTitle: Page 1
BookmarkLevel: 1
BookmarkPageNumber: 1
BookmarkTitle: Page 2
BookmarkLevel: 1
BookmarkPageNumber: 2
BookmarkTitle: Page 3
BookmarkLevel: 1
BookmarkPageNumber: 3

2. Create an temporary merged pdf file

pdftk page* cat output temp.pdf

3. Add the bookmarks to the pdf

pdftk temp.pdf update_info meta.txt output final.pdf

When you open the final.pdf in acrobat reader you see the bookmarks on the left side.
enter image description here

Solution 2

And what’s the best tool for us to merge the pdf’s?

On Linux (as well as on Windows), you can install an useful little program, pdftk. It works well to bind PDF’s together. For example:

$ pdftk in1.pdf in2.pdf in3.pdf in4.pdf in5.pdf in6.pdf cat output out.pdf

where in*.pdf are the input files and out.pdf is the result. In between, @jerik already gave an answer how to deal with the TOC.

Solution 3

To simplify the process of generating table of contents for PDF files, I’ve successfully generated LaTeX code from php and then used latex2pdf to generate the PDF file.

Maybe this could be an alternative, in your case.

To merge PDFs, I agree with Federico that pdftk is the best solution.

Solution 4

If you can use PDF bookmarks instead of a table of Contents page you can use pdfrecycle to merge the PDF files and create the bookmarks.

Solution 5

You can use gs like this:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=combined.pdf file1.pdf file2.pdf

Solution 6

PDF SAM will make it:

Need to merge multiple pdf's into a single PDF with Table Of Contents sections

As shown in the image the open source tool can create a bookmark for each file, which provides a correct table of contents:

Need to merge multiple pdf's into a single PDF with Table Of Contents sections

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply