#!/usr/bin/perl -w use strict; use PDF::Create; use Font::AFM; my $page = $pdf->new_page('MediaBox' => [ 0, 0, 650, 920 ]); my $fn = $pdf->font( 'Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Helvetica' ); $page = PDFColumn( { page => $page, font => $fn, text => $text, right => 250, height => 850, fontsize => 15 } ); $pdf->close; sub PDFColumn { # get the hashref of parameters: my $params = shift; # get the critical parameters and return if they are not present: my $page = $params->{page}; return unless $page; my $font = $params->{font}; return $page unless $font; # get the non-critical parameters or their default values: my $text = $params->{text} || ""; my $fontsize = $params->{fontsize} || 10; my $lineheight = $params->{lineheight} || $fontsize * 1.4; my $left = $params->{left} || 50; my $right = $params->{right} || 300; my $height = $params->{height} || 750; # get the afm info: my $afm_file = $page->{'pdf'}{'fonts'}{$font}{'BaseFont'}[1]; my $afm = new Font::AFM $afm_file; # get the text and the width of the column: my @text = split /\s+/, $text; my $line = shift @text; my $width = $right - $left; # loop through the text sending each new line to the PDF page: while (@text) { my $word = shift @text; if ($afm->stringwidth($line . " " . $word, $fontsize) > $width) { $page->string($font, $fontsize, $left, $height, $line ); $line = $word; $height -= $lineheight; } else { $line .= " " . $word; } $page->string($font, $fontsize, $left, $height, $line ) if @text == 0; } # return the modified page: return $page; }