Incompetech.com
Share this page!

Royalty-Free Music Music FAQ Music Licenses Graph Paper other stuff Contact Me Donate!!


Perl API2 Font Examples

Embedding TrueType Fonts

Finding examples of working with TrueType Fonts in API2 is like pulling teeth. I know I lost a few trying to get the thing to work. Turns out, embedding a TTF into a PDF isn't that difficult. This isn't a tutorial, but it should help some of you out there!


#!/usr/local/bin/perl
use strict;
use CGI;
use PDF::API2;

## This was designed to be output via a web server.


my $q = new CGI;
print $q->header('application/pdf');
my $pdf  = PDF::API2->new(-file => "$$.test");

my $page = $pdf->page;                               ## Create a new page.
my $txt = $page->text;                               ## Text Layer
$txt->compress;

## Create standard font references.
my $HelveticaBold = $pdf->corefont('Helvetica-Bold');
my $Georgia = $pdf->corefont('Georgia-Italic', -encode=>'latin1');

## And embed a TTF
## This assumes the "broadvie.TTF" file is in the same directory
## as this script.
my $BroadView = PDF::API2::TrueTypeFont->new_api($pdf, 'broadvie.TTF');



my $y = 740;
my $x = 100;

$txt->font($HelveticaBold, 14);           ## set font
$txt->translate($x,$y);                   ## set insert location
$txt->text_center("Helvetica Bold, 14");  ## insert text
$y-=20;

$txt->font($Georgia, 12);
$txt->translate($x,$y); 
$txt->text_center("Georgia, 12"); 
$y -= 20;

$txt->font($BroadView, 12);            ## this works just like the corefonts.
$txt->translate($x,$y); 
$txt->text_center("BroadView, 12"); 
$y -= 20;



$pdf->finishobjects($page,$txt);
$pdf->saveas;
$pdf->end();

open (OUT, "$$.test");
while (){print}
close OUT;

If anyone out there has successfully done Cyrllic; please let me know!