Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Combining PDFs

by artist (Parson)
on Jun 27, 2005 at 15:47 UTC ( [id://470301]=perlquestion: print w/replies, xml ) Need Help??

artist has asked for the wisdom of the Perl Monks concerning the following question:

Requirement is to combine many small pdf files into one big pdf file.
use PDF::API2; my $big_pdf = 'big.pdf'; foreach my $souce_pdf (@pdf_files){ my $pds; eval { $pds = PDF::API2->open( $source_pdf ) }; if ($@) { next; } my $pn = $pds->pages; $big_pdf->importpage($pds,$_) for 1..$pn; } $big_pdf->saveas; $big_pdf->end;
It works, but not fast enough. It imports one page at a time. It takes lots of memory and processing time to combine, 1000 pdf files. Looking for something faster.

I don't have to modify the pages. May be I am missing something in PDF::API2 to import files instead of pages.

Thanks..

--Artist

Replies are listed 'Best First'.
Re: Combining PDFs
by halley (Prior) on Jun 27, 2005 at 15:54 UTC
    I haven't used the PDF::API2 method, but will be trying it tonight.

    I've had problems in the past with other PDF-merging solutions, because they basically require you to resample any embedded bitmapped graphics into a worse-quality format.

    Those methods, in conceptual form, are essentially:

    pdf2ps | psmerge | ps2pdf

    If the method you show above works without dropping quality, I'm going to use it, whether it is slow or not.

    --
    [ e d @ h a l l e y . c c ]

      You can take a look at PDFmerge4unix it uses ps2pdf too, but seems to bypass the pdf2ps portion of the conversion -- which should leave embedded bitmaps at their original res and be faster then rewriting the entire PDF by looking at all of the PDF internals in perl. Try it out and let me know.


      -Waswas
Re: Combining PDFs
by crenz (Priest) on Jun 27, 2005 at 16:34 UTC

    Tools that can do this kind of thing include tex and GhostScript. For GhostScript, it's something like

    gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -sOutputFile=../combine +d_doc.pdf *.pdf

    For tex, you can use texexec to combine pdfs.

Re: Combining PDFs
by DrPeter (Scribe) on Jun 27, 2005 at 16:36 UTC
Re: Combining PDFs
by RandomWalk (Beadle) on Feb 01, 2009 at 17:30 UTC
    The second line of code contains small error. Needs something like

    $big_pdf = PDF::API2->new(-file => 'big.pdf');

    from PDF::API2 man page. (this node comes up in google search for "perl combine pdf")

Re: Combining PDFs
by ctilmes (Vicar) on Feb 02, 2009 at 14:02 UTC
    pdftk also has functions for merging PDF files.
Re: Combining PDFs
by moggs (Sexton) on Jul 26, 2011 at 11:39 UTC

    I found great success with CAM::PDF, especially appendpdf.pl

Re: Combining PDFs
by Anonymous Monk on Jul 26, 2011 at 00:27 UTC
    I think PDF::API2->open leaks memory. Simply calling it without assigning the result to anything hordes memory very quickly.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://470301]
Approved by halley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-18 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found