Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: PDF::API2 paragraph vertical align by snoopy
in thread PDF::API2 paragraph vertical align by shibu_pu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-18 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found