Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

use PDF::API2 to add pdf bookmark

by vincentaxhe (Beadle)
on Jun 16, 2024 at 10:28 UTC ( [id://11160011]=perlquestion: print w/replies, xml ) Need Help??

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

I write a perl script to add bookmarks to pdf as below
#!/bin/perl # add bookmark to pdf, at most two levels use strict; use PDF::Reuse; use File::Basename; my ($oldfile, $toc, $gap) = @ARGV; my $newfile = basename($oldfile, '.pdf') . '_new.pdf'; our $lastpage = 0; sub insertbookmark(@){ my ($chapter, @section) = @_ or return; my %items = (text => $chapter->{text}, act => $chapter->{act} ); @items{'close', 'kids'} = (1, \@section) if @section; prBookmark(\%items); } sub valid($){ my $line = shift; my ($text, $page) = $line =~ /^(.+)\t(\d+)$/ or die $line, 'is ill + formatted'; # ()have to escape $text =~ s/([()])/\\\1/g; warn $text, ' on page ', $page, ' must be wrong' if $page < $lastp +age; $lastpage = $page; return ($text, $page); } prFile($newfile); prDoc($oldfile) or die; open my $fh, '<', $toc or die; my @section; while (<$fh>){ chomp; my $flag = s/^\t//;# at most one tab my ($text, $page) = valid $_; $page += ($gap - 1); my $bookmark = { text => $text, act => "$page, 40, 700"}; unless ($flag){ insertbookmark @section; @section = (); } push @section, $bookmark; } insertbookmark @section; prEnd();
Lately I try use pdftk xxx.pdf dump_data_utf8 and to do some modification but I found the bookmarks are not encoded well, if I open with firefox or evince, the bookmarks of chinese characters are a mess, pdfs show fine only with zathura. I ask around, the given advice is to use PDF::API2 to do the work, but I can only add bookmarks to an bookmark-empty pdf as below, but the bookmark is well-encoded, show chinese characters well with firefox or evince.
use PDF::API2; use utf8::all; use File::Basename; my $oldfile = shift; my $newfile = basename($oldfile, '.pdf') . '_new.pdf'; my $pdf = PDF::API2->open($oldfile); my $item = $outline->outline(); $item->title('some chinese characters'); $item->destination($pdf->open_page(1)); $pdf->save($newfile);
I cannot add bookmarks to a pdf if It already has bookmarks, nor can I delete all bookmarks.
# perl bookmark writeruse PDF::API2; use PDF::API2; use utf8::all; use File::Basename; my $oldfile = shift; my $newfile = basename($oldfile, '.pdf') . '_new.pdf'; my $pdf = PDF::API2->open($oldfile); my $outline = $pdf->outline(); while (my $item = $outline->next()) { $item->delete(); print $i++, "\n"; } $pdf->save($newfile);
I cannot see what PDF::API2 try to tell me the right way to do modification

Replies are listed 'Best First'.
Re: use PDF::API2 to add pdf bookmark
by Danny (Friar) on Jun 16, 2024 at 15:15 UTC
    I'm assuming the Chinese characters are in the $toc file? What happens if you 'use Encode' then when you assign $bookmark, change $text to decode("UTF-8", $text)?
      PDF::Reuse failed in inner process due to 'wide character', emit 'Wide character in syswrite at blib/lib/PDF/Reuse.pm (autosplit into blib/lib/auto/PDF/Reuse/descend.al)', PDF::API2 will not, use utf8::all, and accept it.
        It looks like PDF::API2::Outline is used for bookmarks?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11160011]
Approved by johngg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-07-12 11:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.