Search notes:

Perl module PDF::API2

use warnings;
use strict;


use PDF::API2;

use constant mm => 25.4 / 72;
use constant in => 1 / 72;
use constant pt => 1;

# use PDF::API2::Util;

my $pdf = PDF::API2->new;

my $font = $pdf->corefont('Courier New');
my $page = $pdf->page;

$page->mediabox( 216/mm , 303/mm );  # A4 with 3 mm border

my $txt_bottom_left = $page->text;
$txt_bottom_left->font( $font, 18/pt );
$txt_bottom_left->translate( 3/mm, 3/mm );
$txt_bottom_left->fillcolor('#ff7700');
$txt_bottom_left->text("Bottom left");

my $txt_top_right = $page->text;
$txt_top_right->font( $font, 18/pt );
$txt_top_right->translate( 213/mm, 303/mm - 18/pt );
$txt_top_right->fillcolor('#0077ff');
$txt_top_right->text_right("Top right");

$pdf->saveas( "$0.pdf" );
$pdf->end();
Github repository PerlModules, path: /PDF/API2/script.pl

Drawing rectangles and cirles

#!/usr/bin/perl
use warnings;
use strict;

use PDF::API2;

use constant mm => 25.4 / 72;

my $pdf  = PDF::API2->new;
my $page = $pdf  -> page;
my $gfx  = $page -> gfx;

$gfx -> fillcolor('orange');
$gfx -> rect(70/mm, 150/mm, 60/mm, 60/mm);
$gfx -> fill;

$gfx -> fillcolor('darkblue');
$gfx -> circle( 70/mm, 150/mm, 20/mm);
$gfx -> circle(130/mm, 210/mm, 20/mm);
$gfx -> fill;

$pdf -> saveas('rectangles-and-circles.pdf');
$pdf -> end;
Github repository PerlModules, path: /PDF/API2/rectangles-and-circles.pl

Determining text width

#!/usr/bin/perl
use warnings;
use strict;

use PDF::API2;

my $pdf = PDF::API2->new;
my $content = $pdf->page->text;

#  Corefonts: see install-dir/PDF/API2/Resource/Font/CoreFont/*.pm
#    my $courier_new = $pdf->corefont("Courier New");
#    my $helvetica   = $pdf->corefont("Helvetica"  );
#    my $georgia     = $pdf->corefont("Georgia"    );
#    $content->font($courier_new, 20);
#    $content->font($helvetica  , 20);
#    $content->font($georgia    , 20);

# TTF Fonts
my $garamond = $pdf->ttfont('/usr/share/fonts/TTF/LiberationSerif-Regular.ttf');
$content->font($garamond, 200);

print_text_width('i' );
print_text_width('ii');
print_text_width('M');
print_text_width('MM');
print_text_width('iM');
print_text_width('Hello World');

# sort_char_widths();

sub sort_char_widths { #_{
  my %char_widths;
  for my $char ('a' .. 'z', 'A' .. 'Z') {
    $char_widths{$char} = $content->text($char);
  }
  
  for my $char (sort { $char_widths{$a} <=> $char_widths{$b} } keys %char_widths) {
    printf "$char: %f\n", $char_widths{$char};
  }
} #_}

sub print_text_width { #_{
  my $text = shift;
  my $width = $content->text($text);

  printf "%-30s %7.2f\n", $text, $width;

} #_}
Github repository PerlModules, path: /PDF/API2/text-width.pl

See also

Perl modules
PDF

Index