ssimms / pdfapi2

Create, modify, and examine PDF files in Perl
Other
15 stars 20 forks source link

inserting a logo pdf (correct pdf) into another pdf (correct pdf) using embed_page() results in a broken PDF (tested with gs-9.561) #64

Closed oberw closed 1 year ago

oberw commented 1 year ago

I need to add a cc-by-nc logo to a large number of pdf files. In order to do this, I wrote a short test script, which opens a source file (metz_03_3-preview.pdf) and places a logo (ccbync-inkscape.pdf) at a preset position in the source file. While both the source PDF and the logo pdf open in gs 9.561 without a hitch, the source file with the logo inserted (with-ccbync-logo.pdf) miserably fails to open in gs with the following error:

The following errors were encountered at least once while processing this file: object lacks an endobj PDF file was repaired

This file had errors that were repaired or ignored. The file was produced by: >>>> itext-paulo-155 (itextpdf.sf.net - lowagie.com) <<<< Please notify the author of the software that produced this file that it does not conform to Adobe's published PDF specification.

Interestingly, evince is obviously able to sanitize the broken file and display it, but neither is gs, nor acrobat. Unfortunately, my knowledge of the inner pdf structures is limited and I have not been able to diagnose which object lacks the endobj that gs moans about.

----------------here's the test script ----

!/usr/bin/perl

use strict; use warnings; use PDF::API2;

my $pdf = PDF::API2->new(); my $source_pdf = PDF::API2->open('metz_03_3-preview.pdf');

for (my $page=1; $page <= 1; $page++) { my $source_page = $source_pdf->open_page($page);

#read logo
my $logo= PDF::API2->open('ccbync-inkscape.pdf');
# Import Page 1 from the logo PDF
my $object = $logo->embed_page($logo, 1);

# Add it to the source PDF's first page at 1/2 scale
my ($x, $y) = (498,765);
$source_page->object($object, $x, $y, 0.5);

} $source_pdf->{forcecompress} = 0; ## doesn't solve the problem $source_pdf->save('with-ccbync-logo.pdf');

the tar.gz contains both test script, source-pdf, logo-pdf for testing. Any hint how to fix this is greatly appreciated.

pdf-api2-2044-error-inserting-pdf-into-pdf-endobj-missing.tar.gz

oberw commented 1 year ago

Oh, I forgot to mention the 'participating' versions of PDF::API2 and perl:

perl -MPDF::API2 -E 'say $PDF::API2::VERSION' 2.044 perl -V Summary of my perl5 (revision 5 version 36 subversion 0) configuration: Platform: osname=linux osvers=5.19.2-1-default archname=x86_64-linux-thread-multi

ssimms commented 1 year ago

The issue is with this line:

my $object = $logo->embed_page($logo, 1);

... which is creating the XObject in the logo PDF rather than the source PDF. You want:

my $object = $source_pdf->embed_page($logo, 1);