App-PDF-Link
view release on metacpan or search on metacpan
lib/App/PDF/Link.pm view on Meta::CPAN
my $text; # text content
my $gfx; # graphics content
my $x; # current x for icon
my $y; # current y for icon
my $did;
my $embed = $env->{embed}; # 0 = linked
# 1 = embedded
# 2 = attached
my $action = ( qw( linked embedded attached ) )[$embed];
foreach ( @targets ) {
unless ( -r $_ ) {
warn("\tTarget: ", encode_utf8($_), " missing (skipped)\n");
next;
}
$did++;
my $t = substr( $_, length(File::Spec->catpath($v, $d||"", "") ) );
( my $ext = $t ) =~ s;^.*\.(\w+)$;$1;;
my $p = get_icon( $env, $pdf, $ext );
unless ( $p ) {
warn("\tFile: ", encode_utf8($t), " (ignored)\n");
next;
}
warn("\tFile: ", encode_utf8($t), " ($action)\n");
if ( $action eq "attached" ) {
# Separate objects, no coordinate problems.
$pdf->embeddedfile( $t );
next;
}
my $dx = $env->{iconsz} + $env->{padding};
my $dy = $env->{iconsz} + $env->{padding};
unless ( $page ) {
$page = $pdf->openpage($pageno);
my @m = $page->get_mediabox;
if ( $env->{xpos} >= 0 ) {
$x = $m[0] + $env->{xpos};
}
else {
$x = $m[2] + $env->{xpos} - $env->{iconsz};
$dx = -$dx unless $env->{vertical};
}
if ( $env->{ypos} >= 0 ) {
$y = $m[3] - $env->{ypos} - $env->{iconsz};
}
else {
$y = $m[1] - $env->{ypos};
$dy = -$dy if $env->{vertical};
}
$text = $page->text;
####WARNING: Coordinates may be wrong!
# The graphics context uses the user transformations
# currently in effect. If these were not neatly restored,
# the graphics may be misplaced/scaled.
# By using --gfunder, the images are placed behind the page
# but this only works for transparent pages.
$gfx = $page->gfx( $embed ? 0 : $env->{gfunder} );
}
my $border = $env->{border};
my @r = ( $x, $y, $x + $env->{iconsz}, $y + $env->{iconsz} );
if ( $action eq "embedded" ) {
# This always uses the right coordinates.
my $ann = $page->annotation_xx;
$ann->fileattachment( $t,
-text => "$t $action by pdflink $VERSION",
-icon => $p,
-rect => \@r );
}
else {
my $ann = $page->annotation_xx;
$ann->file( $t, -rect => \@r );
my $scale = $env->{iconsz} / $p->width;
$gfx->image( $p, @r[0,1], $scale );
}
if ( $env->{border} ) {
$gfx->rectxy(@r );
$gfx->stroke;
}
# Next link.
if ( $env->{vertical} ) {
$y -= $dy;
}
else {
$x += $dx;
}
}
return unless $pdfname;
# Finish PDF document.
if ( $env->{output} ) {
warn("Writing PDF ", $env->{output}, " ...\n") if $env->{verbose};
$pdf->saveas($env->{output});
warn("Wrote: ", $env->{output}, "\n") if $env->{verbose};
}
elsif ( $did ) {
warn("Updating PDF $pdfname...\n") if $env->{verbose};
$pdf->update;
warn("Wrote: $pdfname\n") if $env->{verbose};
}
else {
warn("Not modified: $pdfname\n") if $env->{verbose};
}
}
sub linkit {
my $env = shift;
require Text::CSV_XS;
require File::Spec;
require File::Glob;
my ( $pdfname, $csvname ) = @ARGV;
lib/App/PDF/Link.pm view on Meta::CPAN
If the value is negative, icon placement starts relative to the right
side of the page.
Default is 0 (zero); icon placement begins against the left side of
the page.
Icons are always placed from the outside of the page towards the
inner side.
An I<xpos> value may also be specified in the CSV file, in a column
with title C<xpos>. If present, this value is added to position
resulting from the command line / default values.
=item B<--ypos=>I<NN>
If the value is positive, icon placement starts relative to the top
of the page.
If the value is negative, icon placement starts relative to the bottom
of the page.
Default is 0 (zero); icon placement begins against the top of the
page.
Icons are always placed from the outside of the page towards the
inner side.
An I<ypos> offset value may also be specified in the CSV file, in a
column with title C<ypos>. If present, this value is added to position
resulting from the command line / default values. This is especially
useful if there are songs in the PDF that do not start at the top of
the page, e.g., when there are multiple songs on a single page.
=item B<--iconsize=>I<NN>
Desired size of the link icons. Default is 50.
=item B<--padding=>I<NN>
Space between icons. Default is to place the icons adjacent to each
other.
=item B<--vertical>
Stacks the icons vertically.
=item B<--border>
Requests a border to be drawn around the links.
Borders are always drawn for links without icons.
=item B<--gfunder>
Drawing the icon images uses the page transformations in effect at the
end of the page. If these were not neatly restored, the graphics may
be misplaced/scaled/flipped.
By using B<--gfunder>, the images are placed behind the page
but this only works for transparent pages.
This option is only relevant when adding links to external files. With
B<--embed> the problem does not occur.
=item B<--targets=>I<FILE1> [ B<;> I<FILE2> ... ]
Explicitly specifies the target files to link to. In this case no CSV
is processed and the input PDF(s) are updated (rewritten) unless
B<--pdf> is used to designate the output PDF name.
=item B<--help>
Prints a brief help message and exits.
=item B<--man>
Prints the manual page and exits.
=item B<--ident>
Prints program identification.
=item B<--version>
Prints program identification and exits.
=item B<--verbose>
Provides more verbose information.
=item I<directory>
The directory to process. Defaults to the current directory.
=back
=head1 ICONS
B<pdflink> has a number of icons built-in for common file types.
Associations between a filename extension and an icon can be made with
the B<--icons> command line option.
For example,
--icons=pdf=builtin:PDF
This will associate the built-in icon PDF with filename extension C<pdf>.
Alternatively, an image file may be specified to add user defined icons.
--icons=pdf=builtin:myicons/pdficon.png
The following icons are built-in. By default, only MuseScore and
iRealPro icons are associated and all other filename extensions will
be skipped. When pdflink is run with command line option B<--all>, all
built-in icons will be associated and all matching files will get
linked.
=over
( run in 1.494 second using v1.01-cache-2.11-cpan-39bf76dae61 )