ateague has asked for the wisdom of the Perl Monks concerning the following question:
Good morning!
I am using PDF::API2 version 2.043 on Strawberry Perl 5.032 and am running into some problems figuring out how to use the title() and delete() methods on bookmark outlines in PDFs with existing bookmark outlines.
I have a collection of PDFs that already have outlines in them and I was hoping to rename/re-title the outline on page 1 and delete all other outlines in the PDF. I have not been able to do so and no matter what I have tried, the outlines do not appear to be modified when I open the PDF in a viewer after running my script.
Can this module manipulate/delete existing outlines in a PDF? I even went so far as to "Use the Source AlexiLuke" and took a quick shufti at the module tests to look for an example syntax, and it appears that the tests do not verify if the outlines are actually deleted or edited in the actual PDF. Looking at the actual PDF contents shows that the module is simply slapping some extra objects and xref content after the end of the file.
Here is a small example test case that illustrates the problems I am having:
#!/usr/bin/perl use 5.032; use warnings; use strict; use PDF::API2; use Test::More tests => 2; my ($stringy_bare_pdf, $stringy_outline_pdf); BARE_PDF: { my $pdf = PDF::API2->new(-compress => 0); my $page1 = $pdf->page(); $stringy_bare_pdf = $pdf->to_string(); } OUTLINE: { my $pdf = PDF::API2->new(-compress => 0); my $page1 = $pdf->page(); my $outlines = $pdf->outlines(); my $outline = $outlines->outline(); $outline->title('Test Outline 1'); $outline->dest(1); $stringy_outline_pdf = $pdf->to_string(); } DELETE_OUTLINE: { my $pdf = PDF::API2->from_string($stringy_outline_pdf, -compress = +> 0) or die $!; my $root = $pdf->outlines(); my $outline = $root->outline(); $outline->delete(); is($pdf->to_string(), $stringy_bare_pdf, 'Make sure the outline ac +tually got deleted from the PDF'); } MODIFY_OUTLINE: { my $pdf = PDF::API2->from_string($stringy_outline_pdf, -compress = +> 0) or die $!; my $root = $pdf->outlines(); my $outline = $root->outline(); $outline->title('Test Outline 2'); $outline->dest(1); is($pdf->to_string(), $stringy_outline_pdf, 'Make sure the outline + text actually got changed in the PDF'); } done_testing();
And here is the output of the tests showing the expected and actual PDF content:
perl pdf_test.pl 1..2 not ok 1 - Make sure the outline actually got deleted from the PDF # Failed test 'Make sure the outline actually got deleted from the P +DF' # at pdf_test.pl line 36. # got: '%PDF-1.4 # %╞══╡ # 1 0 obj << /Type /Catalog /Outlines 6 0 R /PageMode /SinglePage /Pag +es 2 0 R /ViewerPreferences << /NonFullScreenPageMode /UseNone >> >> +endobj # 2 0 obj << /Type /Pages /Count 1 /Kids [ 5 0 R ] /Resources 3 0 R >> + endobj # 3 0 obj << /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] >> endobj # 4 0 obj << /Producer (PDF::API2 2.043 \(MSWin32\)) >> endobj # 5 0 obj << /Type /Page /Parent 2 0 R /Resources << /ProcSet [ /PDF / +Text /ImageB /ImageC /ImageI ] >> >> endobj # 6 0 obj << /Type /Outlines /Count 1 /First 7 0 R /Last 7 0 R >> endo +bj # 7 0 obj << /Dest (1) /Parent 6 0 R /Title (Test Outline 1) >> endobj # xref # 0 8 # 0000000000 65535 f # 0000000015 00000 n # 0000000159 00000 n # 0000000235 00000 n # 0000000304 00000 n # 0000000365 00000 n # 0000000477 00000 n # 0000000548 00000 n # trailer # << /Info 4 0 R /Root 1 0 R /Size 8 >> # startxref # 617 # %%EOF # 9 0 obj << /Parent 6 0 R >> endobj # xref # 0 1 # 0000000000 65535 f # 9 1 # 0000000852 00000 n # trailer # << /Info 4 0 R /Prev 617 /Root 1 0 R /Size 10 >> # startxref # 887 # %%EOF # ' # expected: '%PDF-1.4 # %╞══╡ # 1 0 obj << /Type /Catalog /PageMode /SinglePage /Pages 2 0 R /Viewer +Preferences << /NonFullScreenPageMode /UseNone >> >> endobj # 2 0 obj << /Type /Pages /Count 1 /Kids [ 5 0 R ] /Resources 3 0 R >> + endobj # 3 0 obj << /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] >> endobj # 4 0 obj << /Producer (PDF::API2 2.043 \(MSWin32\)) >> endobj # 5 0 obj << /Type /Page /Parent 2 0 R /Resources << /ProcSet [ /PDF / +Text /ImageB /ImageC /ImageI ] >> >> endobj # xref # 0 6 # 0000000000 65535 f # 0000000015 00000 n # 0000000143 00000 n # 0000000219 00000 n # 0000000288 00000 n # 0000000349 00000 n # trailer # << /Info 4 0 R /Root 1 0 R /Size 6 >> # startxref # 461 # %%EOF # ' not ok 2 - Make sure the outline text actually got changed in the PDF # Failed test 'Make sure the outline text actually got changed in th +e PDF' # at pdf_test.pl line 47. # got: '%PDF-1.4 # %╞══╡ # 1 0 obj << /Type /Catalog /Outlines 6 0 R /PageMode /SinglePage /Pag +es 2 0 R /ViewerPreferences << /NonFullScreenPageMode /UseNone >> >> +endobj # 2 0 obj << /Type /Pages /Count 1 /Kids [ 5 0 R ] /Resources 3 0 R >> + endobj # 3 0 obj << /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] >> endobj # 4 0 obj << /Producer (PDF::API2 2.043 \(MSWin32\)) >> endobj # 5 0 obj << /Type /Page /Parent 2 0 R /Resources << /ProcSet [ /PDF / +Text /ImageB /ImageC /ImageI ] >> >> endobj # 6 0 obj << /Type /Outlines /Count 1 /First 7 0 R /Last 7 0 R >> endo +bj # 7 0 obj << /Dest (1) /Parent 6 0 R /Title (Test Outline 1) >> endobj # xref # 0 8 # 0000000000 65535 f # 0000000015 00000 n # 0000000159 00000 n # 0000000235 00000 n # 0000000304 00000 n # 0000000365 00000 n # 0000000477 00000 n # 0000000548 00000 n # trailer # << /Info 4 0 R /Root 1 0 R /Size 8 >> # startxref # 617 # %%EOF # 9 0 obj << /Dest (1) /Parent 6 0 R /Title (Test Outline 2) >> endobj # xref # 0 1 # 0000000000 65535 f # 9 1 # 0000000852 00000 n # trailer # << /Info 4 0 R /Prev 617 /Root 1 0 R /Size 10 >> # startxref # 921 # %%EOF # ' # expected: '%PDF-1.4 # %╞══╡ # 1 0 obj << /Type /Catalog /Outlines 6 0 R /PageMode /SinglePage /Pag +es 2 0 R /ViewerPreferences << /NonFullScreenPageMode /UseNone >> >> +endobj # 2 0 obj << /Type /Pages /Count 1 /Kids [ 5 0 R ] /Resources 3 0 R >> + endobj # 3 0 obj << /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] >> endobj # 4 0 obj << /Producer (PDF::API2 2.043 \(MSWin32\)) >> endobj # 5 0 obj << /Type /Page /Parent 2 0 R /Resources << /ProcSet [ /PDF / +Text /ImageB /ImageC /ImageI ] >> >> endobj # 6 0 obj << /Type /Outlines /Count 1 /First 7 0 R /Last 7 0 R >> endo +bj # 7 0 obj << /Dest (1) /Parent 6 0 R /Title (Test Outline 1) >> endobj # xref # 0 8 # 0000000000 65535 f # 0000000015 00000 n # 0000000159 00000 n # 0000000235 00000 n # 0000000304 00000 n # 0000000365 00000 n # 0000000477 00000 n # 0000000548 00000 n # trailer # << /Info 4 0 R /Root 1 0 R /Size 8 >> # startxref # 617 # %%EOF # ' # Looks like you failed 2 tests of 2.
Thank you for your time!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: PDF::API2 - issues with "delete()" and "title()" methods in Outline.pm
by vr (Curate) on Sep 03, 2022 at 13:54 UTC | |
by ateague (Monk) on Sep 06, 2022 at 17:05 UTC | |
by vr (Curate) on Sep 06, 2022 at 20:50 UTC |