PDF Hacks

ziggy on 2005-12-19T22:22:34

I have a handful of PDFs I want to combine into a single PDF. I remember trying to solve this problem on OS X a few years ago, and found no (obvious) native tools to use, and a very few open source tools that could do the job. Eventually, I found some little open source applet written in RealBasic that worked the one time I needed it; I don't remember what it was, and probably never used it again.

Of course, that's totally lame. A real solution would involve something from CPAN and a couple of lines of Perl. For example, something like this:

#!/usr/bin/perl -w

use strict; use PDF::Reuse;

my $output = shift(@ARGV);

prFile($output); prDoc({file => $_}) foreach (@ARGV); prEnd;


PDF::Reuse has lots of other features, including picking and choosing which pages of a PDF to reuse and merge. But for joining a whole mess of PDFs together into a single file, this works wonderfully.

Thanks, Lars!


Concatenating pdf documents

pjm on 2005-12-20T00:21:41

OK, it's not really a "native" solution either, but if you've got a tex distribution (say "teTeX") kicking around on the system there's a similarly nice solution via:

texexec --pdfarrange --result all.pdf file*.pdf

(or even using ghostscript

gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf
  *.pdf

)

If you want more control over importing certain pages you can use the pdfpages package in latex; just run the following file (call it all.tex or whatever) through latex:

\documentclass[letter]{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=5]{me.pdf}
\includepdf[pages={12-14}]{you.pdf}
\includepdf[pages=4-8,12-14]{dog.pdf}
\includepdf[pages=1-]{boo.pdf}
\end{document}

That'll give you a pdf file all.pdf containing the obvious pages from the four constituents: page 5 only from me.pdf, pages 12-14 from you.pdf, pages 4-8 and 12-14 from dog.pdf and all pages from boo.pdf.

Cheers,
Paul

nice!

lachoy on 2005-12-20T03:16:40

and thank you for posting this, very useful

Another option

ChrisDolan on 2005-12-20T05:11:39

Cool. Here's another solution

% cpan install CAM::PDF
% appendpdf.pl file1.pdf file2.pdf out.pdf

or almost equivalently:

use CAM::PDF;
my $doc1 = CAM::PDF->new(shift);
my $doc2 = CAM::PDF->new(shift);
$doc1->appendPDF($doc2);
$doc1->cleanoutput(shift);

-- Chris

P.S. I'm the author of CAM::PDF

thanks

Lecar_red on 2005-12-20T14:46:50

Very cool, I was just thinking about this the other day. I found a different solution (I had the originals and combined into one document first).