I have a subroutine to help me work with
PDF::Create. It accepts a hashref as argument, which must contain
page,
font and
text, and can contain some other optional parameters. In return it lays out the text in lines of appropriate width and spacing - that is, turns a string of text into a column of text in PDF file.
The usage of the module, and the module itself, are below.
My question is about the way I call the module. As you see, having created a new page object using
PDF::Create, I then pass this object to my subroutine, which modifies it and returns it. What I'd like to know how to do is write it so I could call it thus:
$page->PDFColumn(....). This seems neater than the three-card monty I have to do at present with objects going back and forth. (Although on mature consideration I think I may wish to pass back the modified
$height parameter as well)
One thing I know I'd have to do is alter the start of my subroutine to get the page directly rather than as an element in a hashref. But that's clearly not all there is to it. I know little of OO, less of Perl OO. I read through
bless, but this doesn't seem to be what I want. I'd be very grateful if any kind monk cd steer me in the direction of an explanation.
#!/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 @te
+xt == 0;
}
# return the modified page:
return $page;
}
§
George Sherston
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.