Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: PDF::API2 paragraph vertical align

by snoopy (Deacon)
on Dec 17, 2009 at 02:27 UTC ( #813121=note: print w/ replies, xml ) Need Help??


in reply to PDF::API2 paragraph vertical align

I've also been digging around in PDF::API2, following on from mykl.

Another angle of attack is to try to solve the problem by subclassing/overriding.

For example here's the code from PDF::API2::Content for outputting centered text.

sub text_center { my ($self,$text,@opts)=@_; my $width=$self->advancewidth($text); return $self->text($text,-indent=>-($width/2),@opts); }
It seems that the text method is used in all cases to output the text (left, right, centered, justified).

The following overrides the text method, giving us the ability to disable text output at will, allowing to precompute the height of the paragraph:

#!/usr/bin/perl package PreflightText; use common::sense; use base 'PDF::API2::Content::Text'; our $PREFLIGHT; sub text { my $self = shift; my $text = shift; return $PREFLIGHT ? $self->advancewidth($text) : $self->SUPER::text($text, @_) } ###################################################################### package main; use common::sense; use PDF::API2; my $para = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, +sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut + enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi + ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehe +nderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur +. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui o +fficia deserunt mollit anim id est laborum."; my $pdf = PDF::API2->new(); my $page = $pdf->page(0); my $txt = $page->text; bless $txt, 'PreflightText'; $txt->font($pdf->corefont('Times-Roman') => 10); $txt->transform(-translate=>[0, 0]); $txt->lead(12); my $actual_height = do { # # compute, but don't output # local(${PreflightText::PREFLIGHT}) = 1; my (undef, $height_remaining) = $txt->paragraph($para,'500', '1000 +', -align => 'center'); 1000 - $height_remaining; }; # # now do actual output of the vertically centered text # $txt->transform(-translate=>[300, 500 + $actual_height/2]); my ($text_remaining) = $txt->paragraph($para,'500', '1000', -align => +'center'); my $saveas = "/tmp/center.pdf"; warn "saving as: $saveas\n"; $pdf->saveas($saveas);
Update July 2012:
(1) Added saveas()
(2) Just for correctness, changed text() override method to return advancewidth() in preflight mode - same as what PDF::API2::Content::text() returns.


Comment on Re: PDF::API2 paragraph vertical align
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://813121]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-11-21 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (107 votes), past polls