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

Hi, Monks.

I have a problem when merging PDF documents. I need to import first page from many one page PDF documents into one result PDF.

I try PDF::API2, PDF::Reuse, CAM::PDF but nothing helps :-(. My main problem is importing barcode from source PDF (see example 1 33 kb, example 2 33 kb). My program got this pdfs from USPS site and i need to create single PDF from them.

PDF::API2 and CAM::PDF don't show barcode at all. PDF::Reuse show barcode only on first page :-( (see PDF::API2 result 53 kb, CAM::PDF result 54 kb, PDF::Reuse result 65 kb).

Here my code.

For PDF::API2:

use PDF::API2; my $result_pdf = PDF::API2->new(); my $pdf = PDF::API2->open('label1.pdf'); my $page = $result_pdf->importpage($pdf, 1); $pdf = PDF::API2->open('label3.pdf'); $page = $result_pdf->importpage($pdf, 1); $result_pdf->saveas("pdfapi2_result.pdf");

For CAM::PDF:

use CAM::PDF; my $doc1 = CAM::PDF->new('label1.pdf') || die "$CAM::PDF::errstr\n"; my $doc2 = CAM::PDF->new('label3.pdf') || die "$CAM::PDF::errstr\n"; $doc1->appendPDF($doc2); $doc1->clearAnnotations(); $doc1->cleanoutput('campdf_result.pdf');

And for PDF::Reuse which works but only for first page:

use PDF::Reuse; prFile('new.pdf'); prDoc( { file => 'label3.pdf', first => 1, last => 1 }); prDoc( { file => 'label1.pdf', first => 1, last => 1 }); prEnd();

Could you explain why barcodes did't copy? What i must to do to copy them?

P.S. When i wrote this message i think about Javascript in the PDF. Maybe barcodes are creating by Javascript? And when i importing page from source file i'm importing Javascript but maybe this Javascript only generates barcode for the first page?

Replies are listed 'Best First'.
Re: Problem importing page from PDF
by almut (Canon) on Nov 30, 2007 at 14:38 UTC

    I suspect you've hit a bug (your various attempts look fine to me).  The barcodes are included as pixel images, and AFAICT, Javascript is not involved here. The strange thing is that although all three tools do in fact copy the images into the merged output file, and there's also some code which is supposed to render them, this doesn't seem to happen...

    At the moment I don't have the time to look into this in more detail, but if you don't mind installing yet another PDF tool (not Perl though), I'd suggest using pdftk:

    pdftk label1.pdf label3.pdf cat output all.pdf

    would concatenate label1.pdf and label3.pdf into all.pdf (and all barcode labels did show up fine when I tried it). You may of course also specify more than two input files.

    Try to get one of the precompiled binaries... Depending on platform, pdftk can sometimes be quite a PITA to build. It depends on GCJ, which means you'll also need to have libgcj.so installed (Java runtime library for use with the gcj front end to gcc).  It's worth the trouble, though. Once you have it working, it allows you to do all kinds of manipulating PDF files. I particularly like its feature to be able to uncompress and re-compress the stream objects in the PDF files, which essentially means that you can edit the contents with your favorite text editor (or Perl :) — or debug problems like these... (You still have to know what you're doing (i.e. PDF syntax), but the tedious work like updating the xref table etc. is being done for you.)

      Thanks a lot! I try to use pdftk and it works!