in reply to blank pdf generated using PDF::API2

Hello lennelei

Welcome to the mnonastery. Try to use PDF::API2/PAGE METHODS/import_page(), it should work I tested on mine. Sample of working code:

Update: Minor note there is no importpage() method you probably mean import_page() which is working as expected, see sample code bellow.

#!/usr/bin/perl use strict; use warnings; use PDF::API2; my $file='test.pdf'; my $newpdf = PDF::API2->new(); my $oldpdf = PDF::API2->open($file); if ($oldpdf->pages() > 1) { printf " (%d pages)\n", $oldpdf->numPages(); for my $page_nb (1..8) { $newpdf->import_page($oldpdf, $page_nb, $page_nb); } $newpdf->saveas("test_2.pdf"); }

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: blank pdf generated using PDF::API2
by poj (Abbot) on Jul 21, 2017 at 08:44 UTC

    Changes at Revison 2.022

    2.022     2014-07-04
    
        - Added $pdf->version() get/set method.  When opening an existing PDF, the
          existing version number will now be retained.
    
        - Renamed the following in PDF::API2:
            - importpage to import_page
            - openScalar to open_scalar
    
    poj

      Thanks poj I had no clue... :D

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re^2: blank pdf generated using PDF::API2
by lennelei (Acolyte) on Jul 21, 2017 at 09:29 UTC

    Thank you for your help and your welcome :)

    You're right for the method name, I don't know why I use importpage (I probably found it on an old example) ; however, both methods return the same result so it doesn't work either for my specific pdf.

    As I (tried to) explain, my script is working: we use it in a production environment for weeks now. There is only one file it failed to process correctly and for which the output is a 100 blank pages pdf.

      Hello again lennelei,

      Notice this line($newpdf->import_page($oldpdf, $page_nb, $page_nb);) it uses 3 parameters not 2. Try to copy the example that I provided you and test it. Does it work?I simulate the scenario with a pdf that I have 7 pages and seems to be working just fine.

      I do not have a pdf 100 pages so I can not really check it but give it a try I assume it should work.

      Update: I just tested the sample of code that I provided you with a pdf of 123 pages. It works just fine. The only line that I modified is the for loop (for my $page_nb (1..$oldpdf->pages())).

      Update2: Full sample of executable code bellow:

      #!/usr/bin/perl use strict; use warnings; use PDF::API2; use feature 'say'; my $file='test.pdf'; my $newpdf = PDF::API2->new(); my $oldpdf = PDF::API2->open($file); if ($oldpdf->pages() > 1) { say $oldpdf->pages() . ' pages.'; for my $page_nb (1..$oldpdf->pages()) { $newpdf->import_page($oldpdf, $page_nb, $page_nb); } $newpdf->saveas("test_2.pdf"); }

      Let us know if it works, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
        I do not have a pdf 100 pages

        You mean you don't have an e-copy of Modern Perl?

        Your code (with some adjustments for my elderly version of PDF::API2) works fine on my first edition of Modern Perl (186 pages).

        Using 3 parameters produces exactly the same result with my pdf (I use your script without the printf " (%d pages)\n", $oldpdf->numPages(); line which is for CAM::PDF). I'd like to provide the pdf file so that you can try but I have to remove sensible information in it before ; and I don't really know how to do this for the moment :)

        Thank you again!