31

I am trying to merge different pdf files (all produced with latex - but with different document classes) without breaking the corresponding hyperlinks.

I have tried the terminal using ghostscript, apple automator, the \includepdf option in latex: they all brake the hyperlinks created with hyperref.

Any good suggestion?

EDIT: as this seem to be a common problem, can anyone try to explain/comment on why such apparently simple task is instead rather daunting? What is it that makes it difficult to merge the pdfs and not break the hyperlinks?

5
  • Not sure whether it can combine PDFs, but qpdf can at least remove encryption from PDFs without breaking hyperlinks, you might give it a try.
    – Skillmon
    Jun 27, 2019 at 6:19
  • 2
    @Skillmon: this is quite on topic. There is a package (pax) which tries to solve this, and with luatex it is probably possible to (but needs some coding). Jun 27, 2019 at 6:35
  • May be combine. If classes are quite different (e.g., book and beamer) this is not a reasonable option, but otherwise (eg., book and scrbook) probably you can change the class of some document with minor fixings, and then use the combine to merge documents in a convenient way (optinal index of the documents, general list of figures, etc.)
    – Fran
    Jun 27, 2019 at 6:55
  • 1
    The answers to the question Cross-reference with xr package and final PDF combination? might be of interest. Feb 23, 2020 at 10:15
  • Take a look at pdfjam, most of it's options are just passed along to pdfpages, and only documented there. Lots of tweaks available, also keeping hyperlinks (not done by default, though). It is part of TeXlive.
    – vonbrand
    Mar 4, 2020 at 13:35

4 Answers 4

15
+250

My method uses the pax package and its pdfannotextractor Perl script (by Heiko Oberdiek).

If you want to combine two PDF files (a.pdf and b.pdf) keeping the PDF annotations (internal and external links are kind of PDF annotations), use the manual or automatic method.

(In case of problems, you can use the --debug option of the pdfannotextractor script.)

Manual installation

  1. Install the pax package from CTAN (or via TeXLive or MikTeX).

  2. Download and unzip the PDFBox-0.7.3 Java library (an old version of the Apache PDFBox, a Java PDF library).

  3. Use pdfannotextractor on a.pdf and on b.pdf:

    CLASSPATH='PDFBox-0.7.3/lib/PDFBox-0.7.3.jar:%CLASSPATH%' pdfannotextractor a.pdf
    CLASSPATH='PDFBox-0.7.3/lib/PDFBox-0.7.3.jar:%CLASSPATH%' pdfannotextractor b.pdf
    

    (You get two new files: a.pax and b.pax)

  4. Compile the following document with pdflatex:

    \documentclass{article}
    \usepackage{pax}
    \usepackage{pdfpages}
    \begin{document}
    \includepdf[pages=-]{a.pdf}
    \includepdf[pages=-]{b.pdf}
    \end{document}
    

Automatic installation

The pdfannotextractor script can perform the installation itself:

  1. Install the pax package from CTAN (or via TeXLive or MikTeX).

  2. Install automatically the PDFBox-0.7.3 archive:

    pdfannotextractor --install
    
  3. Use pdfannotextractor on a.pdf and on b.pdf:

    pdfannotextractor a.pdf
    pdfannotextractor b.pdf
    

    (You get two new files: a.pax and b.pax)

  4. Compile the following document with pdflatex:

    \documentclass{article}
    \usepackage{pax}
    \usepackage{pdfpages}
    \begin{document}
    \includepdf[pages=-]{a.pdf}
    \includepdf[pages=-]{b.pdf}
    \end{document}
    
3
  • This really looked promising, and I think I got it working with the automatic installation. It seems to retain external web links just fine, but with the run: type links (ie, \href{run:foo.pdf}{My PDF} to open another .pdf file,pdfannotextractor reports java.lang.Exception: Unsupported link annotation type: Launch and the output pdf file has just plain text My PDF and is not clickable. Mar 5, 2020 at 6:34
  • @PeterGrill I use my method to build conference proceedings (from multiple articles). Via pdfpages, I can add page numbering, headers and footers. Fortunately, an article never contains Launch type links. Mar 5, 2020 at 7:13
  • @PeterGrill To link to an other PDF, you can use \href{foo.pdf}{My PDF}... Mar 5, 2020 at 15:19
13
+100

TLDR

Use

  • pdftk if want a command-line interface,
  • pdfsam if you want graphical user interface,
  • sejda if you want a web interface.

They are free (as in beer and speech) and available for most operating systems.
I have no idea why most tools do not preserve hyper links.


Method

For a list of possible solutions -- that unfortunately do not discuss whenever links are preserved or not, cf. https://stackoverflow.com/q/2507766/2657549.

I'm testing with two copies of the following file, that I named a.pdf and b.pdf:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{bib.bib}
@misc{ex,
  author={Doe, Jack},
  title={An Example}
}
\end{filecontents*}
\usepackage{biblatex}
\addbibresource{bib.bib}
\usepackage{hyperref}

\begin{document}

\section{A section}
\label{section}

url: \href{https://tex.stackexchange.com/a/531215/34551}{this post}

reference: \cite{ex}

internal link: \ref{section}

file link: \href{run:a.pdf}{file}

\printbibliography
\end{document}

I combined them into a tot.pdf file using the following tools, and tested whenever the url, the reference and the internal links were preserved in both documents or not.

+-----------+----------------------------------------------------------------------------+------+-------+-------+------+
|  Software |                                   Command                                  |  url |  ref. |  link | file |
+-----------+----------------------------------------------------------------------------+------+-------+-------+------+
|  convert  | convert a.pdf b.pdf tot.pdf                                                | ✗    | ✗     | ✗     | ✗   |
|  pdfjam   | pdfjam a.pdf b.pdf -o tot.pdf                                              | ✗    | ✗     | ✗     | ✗   |
|  gs       | gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=tot.pdf a.pdf b.pdf | ✗    | ✗     | ✗     | ✗   |
|  pdfunite | pdfunite a.pdf b.pdf tot.pdf                                               | ✓    | ✗     | ✗     | ✓   |
|  pdftk    | pdftk a.pdf b.pdf cat output tot.pdf                                       | ✓    | ✓     | ✓     | ✓   |
|  pdfsam   | (it's a gui)                                                               | ✓    | ✓     | ✓     | ✓   |
| sejda.com | (it's a website)                                                           | ✓    | ✓     | ✓     | ✓   |
+-----------+----------------------------------------------------------------------------+------+-------+-------+------+

(Note that the links to files are not converted to internal anchor if the document that was referenced is being merged. I don't think that this is even feasible without re-writting the LaTeX source code.)

Versions

  • convert: ImageMagick 6.9.10-23 Q16 x86_64 20190101
  • pdfjam: This is pdfjam version 3.03.
  • gs: GPL Ghostscript 9.27 (2019-04-04)
  • pdfunite: version 0.71.0
  • pdftk: port to java 3.0.2 a Handy Tool for Manipulating PDF Documents
  • pdfsam: PDF Split And Merge Basic Edition 4.0.1
  • https://www.sejda.com/merge-pdf does not mention any version, but was tested on 2020/03/04.

Licences and Pricing

Other options

Not tested:

3
  • Looks like sejda was able to maintain the run: type links that I need (ie, \href{run:foo.pdf}{My PDF} to open another .pdf file). They have a desktop version so I am trying that. Mar 5, 2020 at 7:04
  • 1
    @PeterGrill Oh, and if you have control over the source file, then you may want to have a look at the documentation of hyperref, the paragraph that starts with "If the final PDF file is going to be merged with another file, …".
    – Clément
    Mar 5, 2020 at 19:23
  • 1
    Another consideration should be what happens to the table of content in the pdf, but I guess that's a different question.
    – Clément
    Mar 13 at 18:48
1

There are still a few tools to allow merging pdf files whilst retaining hyperlinks. I retested a few of the more established ones.

Ghostscript (free)

The commands I tested all broke some links (however I may have missed a command that works for you).
 

PDFSAM / Sejda (freemium) these come in many flavours from the same source.

I had best success using Sejda online 4 files will have internal / external links that work as expected. However note in my limited test the last document say pages 1-5 for arguments sake would now be for example physical 15-19 of the total However they are kept as logical 1-5 so I can jump internally from page3(18) to page2(17) or index 5(19) - contents 1(15) etc I do see the old numbers 1-5 in use which helps.

Pdftk (freemium) also merged well in the free version.
However pages as built similar to above only show the new physical page numbers so it is odd to see a link for page 1 that jumps me to page 15.

In summary are you sure this is the route to go down? since numbering of internal links and page numbers will be all over the place. It makes more sense to include all the files in a main.tex where numbering of pages and links can be controlled.

0

PDF-XChange Editor seems to do this without any problems. However, it is a "premium feature" and the editor will stamp a notice all over your document when you do it using the free version.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .