PhilterPaper / Perl-PDF-Builder

Extended version of the popular PDF::API2 Perl-based PDF library for creating, reading, and modifying PDF documents
https://www.catskilltech.com/FreeSW/product/PDF%2DBuilder/title/PDF%3A%3ABuilder/freeSW_full
Other
6 stars 7 forks source link

mixing 2 pdf's give an error in Builder.pm line 2077 #204

Closed soulman-web closed 7 months ago

soulman-web commented 7 months ago

I want to put a logo behind my newly created pdf, doing this with PDF::API2 is fine, but with PDF::Builder i get this: Use of uninitialized value in concatenation (.) or string at /usr/share/perl5/vendor_perl/PDF/Builder.pm line 2077 running on RedHat 9 version 3.024-2.el9 What i do is merge an existing pdf file in the actual open pdf.

code is: elsif ( /^pdf/ ) { $dum = $_ ; $tmp = rindex($dum , "|"); $pdffile = substr($dum,$tmp+1); $merger = PDF::Builder->open($layout . "images/" . $pdffile ); $xo = $pdf->importPageIntoForm($merger ); $line->formimage($xo, 0, 0, 1 ); $merger->end; } elsif ( /^bc/ ) { ( $dummy,$rg,$pos,$tekst ) = split(/|/); barcode();

    }
    elsif ( /^new-page/ ) {
    pagina($huidig);
    }
}

close (IN); if ( $nieuw == 0) { $xo = $pdf->importPageIntoForm($old, $huidig ); $line->formimage($xo, 0, 0, 1 ); }

PhilterPaper commented 7 months ago

Is this running PDF::Builder 3.024? That's a bit old... is there any chance that you can try with the current GitHub version? 3.026 is close to being released (within a month), and there are a number of changes in it, some affecting the "open" and "importPageIntoForm" methods.

Is your line 2077 the following:

        $content->{' stream'} .= ' ' . unfilter($k->{'Filter'}, $k->{' stream'}) . ' ';

? I don't think any changes have been made lately in this neighborhood, so I want to check if I'm looking at the right version of the code.

Can you narrow down the area where the error is occurring in your example code? Is it in the "PDF" section or the "BC" section? Can you tell which statement it is in your code? There appears there may be some code missing from what you posted (or it could be a formatting problem), so I don't think I can run it as it is. It would be helpful if you could include a complete working minimal test case, along with some small PDF files that help to illustrate the problem. Thanks!

I don't know how long it will take for you (Red Hat) to update to 3.026 once it's released to CPAN, if you're OK with waiting for the release. I will try to set up a 3.024 version to try to replicate this, and will also try it on 3.026. If you have any chance of trying your code on 3.026, that would be great.

soulman-web commented 7 months ago

it was indeed the line you wrote. I did a git clone ( then I have 3.026 ? ) ,and did make etc. and got the same error , now on line 2552 $content->{' stream'} .= ' ' . unfilter($k->{'Filter'}, $k->{' stream'}) . ' ';

what I do is: open original / create new pdf / merge logo / merge original Seems that last step throws the error stepping through code: main::(./gz_pdfmix:208): if ( $nieuw == 0) { $xo = $pdf->importPageIntoForm($old, $huidig ); DB<3> n Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl5/5.32/PDF/Builder.pm line 2552. at /usr/local/share/perl5/5.32/PDF/Builder.pm line 2552. PDF::Builder::open_page(PDF::Builder=HASH(0x561b580d97e8), 1) called at /usr/local/share/perl5/5.32/PDF/Builder.pm line 2819 PDF::Builder::embed_page(PDF::Builder=HASH(0x561b57df5e60), PDF::Builder=HASH(0x561b580d97e8), 1) called at /usr/local/share/perl5/5.32/PDF/Builder.pm line 2802 PDF::Builder::importPageIntoForm(PDF::Builder=HASH(0x561b57df5e60), PDF::Builder=HASH(0x561b580d97e8), 1) called at ./gz_pdfmix line 208

Can I mail you ( private ) my documents ?

PhilterPaper commented 7 months ago

Can I mail you ( private ) my documents ?

It's possible that there is something unusual in the structure of the documents you're using, but first I want to try this with something that I already have. If you need to send them, do you have my mailing address?

If your debugging is correct, you have narrowed it down to $xo = $pdf->importPageIntoForm($old, $huidig ); near the very end of the code you gave. "importPageIntoForm()" is an alias for "embed_page()", which in turn calls "open_page()". The first argument to embed_page() should be a PDF object (PDF::Builder), which the dump seems to say it is, while the second argument is the "current" page number, in this case 1. (Caution: the object "$pdf" is inserted before the first argument.) Where did "$old" come from (opened an existing PDF?) and does it have at least one page? I would have expected a different error if it had no pages, but it doesn't hurt to check.

The following test case works fine for me, PDF::Builder 3.026 prerelease, Strawberry Perl 5.32. (Content.pdf is the output of examples/Content.pl):

use strict;
use warnings;
use PDF::Builder;

my $pdf = PDF::Builder->new();
my $page = $pdf->page();
my $grfx = $page->gfx();

my $old = PDF::Builder->open("/Users/Phil/Desktop/PDF-Builder/examples/Content.pdf");

my $xo = $pdf->importPageIntoForm($old, 1);  # also with embed_page()
$grfx->formimage($xo, 0,0, 1);
$pdf->saveas("/Users/Phil/Desktop/test204.pdf");

Does this work or fail with the "old" PDF you're using? If it fails, please send me the PDF. Did it come from PDF::API2 or PDF::Builder, or some other source? What PDF version is it? If it's above 1.4, if may use something that the PDF::Builder code chokes on, although I would expect PDF::API2 to also fail.

soulman-web commented 7 months ago

at first my logo was false , but that was because the new pdf was not in landscape. remarkable is that not the logo file is complaining, but the before created pdf with this own PDF::Builder The result of merging the 2 pdf's is good !! So the job was done , but with complaining Use of uninitialized value in concatenation (.) or string at /usr/local/share/perl5/5.32/PDF/Builder.pm line 2552.

PhilterPaper commented 7 months ago

OK, I'm even more confused now. It's good that the result of the merger is OK, but still, you should not be receiving a warning message about an uninitialized value on line 2552 (in open_page() method). If the error can be reliably reproduced, I would like to fix it. Feel free to send me a minimal test case (something similar to what I posted above), along with your logo PDF that produces the warning, and I will try to find a fix. By the way, what version of PDF::API2 did this work on without an error? I believe that there have been several releases, including some major changes to this area, since your Red Hat release, I would be curious if it still runs OK on the latest PDF::API2 (2.045).

soulman-web commented 7 months ago

I think I'm a bad tester. I have 2 processes: create pdf , and mix pdf with logo, so I was testing the mixing. After upgrading , as you advised, I went along with mixing , and still gave the error. ( because I used the old created pdf ) After creating a new pdf with the latest PDF::Builder everything is okay !!!

By the way /usr/local/share/perl5/5.32/PDF/Builder/Basic/PDF.pm says: my $LAST_UPDATE = '3.020'; # manually update whenever code is changed ( so it is not uptodate ) Many thanks !

PhilterPaper commented 7 months ago

Don't worry, none of us perfect are... I thought I made a mistake once, but I was wrong. Although I have to wonder a bit why the "old" PDF gave an error and the new one doesn't. Maybe there was a bug fixed in the newer version?

Don't worry about the $LAST_UPDATE value -- it just shows the last release that this particular file saw an update in. $VERSION shows the overall PDF::Builder release, and should be the same on all files. BTW, Basic/PDF.pm is just a dummy stub anyway, to permit the POD to link to all the children. It might never change again.

If you're happy with the results, I'll go ahead and close this. You can re-open it if you feel it's necessary. Do you have any mechanism with your Red Hat installation to update Perl packages to their latest CPAN release? If you wait to upgrade your Red Hat installation, you might end up many releases behind on Perl packages such as PDF::Builder (I don't know how well RH keeps updated). I run on Strawberry Perl (for Windows), and can run a daily update to keep my packages at the very latest (from cpan.org) via "cpan".