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

I have been trying to create a pdf munger in perl that reorders pages into a new copy, so that i can print doublesided pages, two up in the correct order...

The script I wrote works seamlessly on small pdfs (up to 50 pages). I tried it on the large file i want (122 pages), and it didn't seem to work at all.

My proposed solution was to chunk up the file into smaller parts. I wrote a smaller script, that simply iterates the large file copying chunks of pages to new files.

It also hung indefinately. I made some diagnostic messages, and trimmed the complexity further and it turned out that
$pdf->importpage($pdf2,$i,0);

is what hangs. The process consumes huge amounts of memory (700 MB virtual), for a 9MB pdf. Aside from that I know nothing. $pdf is the new file, and $pdf2 is the old one. $i is a page number, and 0 means append, as documented.

Anybody know what can be done?

-nuffin
zz zZ Z Z #!perl

Replies are listed 'Best First'.
Re: PDF::API2 / imporpage method hangs
by waswas-fng (Curate) on Feb 01, 2003 at 19:55 UTC
    Have you verified that $pdf2 is parsable by PDF::API2? (I assume you are using PDF::API2 you don't say) I have run into this problem before when the pdf I was sucking pages out of was not completly parseable by PDF::API2. I was able to resolve it a few times by opening a copy of the PDF I was working on and $pdf->clonepage as needed instead of importing from another pdf.

    -Waswas
      see title, =)

      As the documentation is somewhat ambiguous/lacking i have yet to discover how to delete/insert page objects from/to a pdf object, without the use of importpage.

      I copied from one object to another because it's not nice to ruin your originals, which is something i tend to do more often than not, but no just that. It's also because i simply figured no other way to reorder the pages without simply inserting them into another file in the order i want.

      -nuffin
      zz zZ Z Z #!perl
Re: PDF::API2 / imporpage method hangs
by theorbtwo (Prior) on Feb 01, 2003 at 22:22 UTC

    It does indeed sound like PDF::API2 doesn't like your PDF. You might try preprocessing it with pdf2pdf (IIRC, it comes with ghostscript) to normalize it.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).