Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

vertical spacing with PDF::API2

by Aldebaran (Curate)
on May 21, 2016 at 23:18 UTC ( [id://1163766]=perlquestion: print w/replies, xml ) Need Help??

Aldebaran has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I'm looking for finishing touches on a perl-based list making capability by means of PDF::API2. I can't figure out the vertical spacing. I want the quote to follow the header, but as it is, it prints out on the same line. Furthermore, the text is chopped, rather than allowed to play out in paragraph form. (Maybe the mediabox methods?) Here's what I have

#!/usr/bin/perl use v5.14; use utf8; use PDF::API2; my @months = qw(January February March April May June July August September October November December); my @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun); my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(); print "$mday $months[$mon] $days[$wday]\n"; my $day = $mday + 1; my $yr = $year + 1900; my $header = "List for $days[$wday+1], $months[$mon] $day, $yr"; my $pdf = PDF::API2->new( width => 595, # A4 dimensions in point height => 842, # 1 point = 1/72 inch ); my $page = $pdf->page; my $txt = $page->text; my $font = $pdf->corefont('Times-Roman'); #header $txt->font( $font, 32 ); $txt->translate( 100, 650 ); $txt->text( $header, -encoding => 'utf8' ); #add randomtext my $quote = get_quote(); print "$quote\n"; $txt->font( $font, 20 ); $txt->translate( 100, 650 ); $txt->text( $quote, -encoding => 'utf8' ); my @task = ( 'wake no later than 8', 'stretch/take meds', 'make coffee/eat cereal', 'eat salad', 'bring food',, ); my $line = $page->gfx; $txt->font( $font, 20 ); my $vspace = 70; for my $i ( 0 .. $#task ) { my $linepos = 500 - ( $i * $vspace ); my $msg = '[ ] ' . $task[$i]; drawline( $line, $linepos ); $txt->translate( 70, $linepos + 10 ); $txt->text($msg); } my $new_name = join( '_', $months[$mon], $day, $yr, '.pdf' ); say "new_name is $new_name"; $pdf->saveas($new_name); sub drawline { my ( $line, $y ) = @_; my $x1 = 50; my $x2 = 550; $line->linewidth(3); $line->move( $x1, $y ); $line->line( $x2, $y ); $line->stroke; } sub get_quote { use HTML::TreeBuilder 5 -weak; my $site = 'http://motivationgrid.com/50-inspirational-quotes-to-li +ve-by/'; my $tree = HTML::TreeBuilder->new_from_url($site); my @quotes; for ( $tree->look_down( _tag => 'p' ) ) { if ( ( my $t = $_->as_text ) =~ m{ ^ \d+ \. \s+ }x ) { $t =~ s{ \x{2019} }{'}gx; $t =~ s{ \xA0 }{ }gx; $t =~ s{ \x{2013} }{--}gx; push @quotes, $t; } } my $randomelement = $quotes[ rand @quotes ]; print "$randomelement\n"; $randomelement =~ s/^\d+\.\s+//; return $randomelement; }

Thanks for your comment,

Replies are listed 'Best First'.
Re: vertical spacing with PDF::API2
by poj (Abbot) on May 22, 2016 at 16:42 UTC
    .. it prints out on the same line

    because you are placing the text at the same position

    $txt->translate( 100, 650 ); $txt->text( $header, -encoding => 'utf8' ); $txt->translate( 100, 650 ); $txt->text( $quote, -encoding => 'utf8' );

    Use your variable $linepos for all the elements and decrement to move down the page as required. To wrap the text you can use the paragraph method.

    poj

      Thanks so much for the cleaner treatment. I didn't understand that the numbers in this context are supposed to get smaller as you go down the page. And it does not give you another page automatically: you have to ask for it, but this is where I have to stop for now.

      Output:

      C:\cygwin64\home\Fred\pages2\list>perl list11.pl Wed, May 25, 2016 28. Don't be afraid to give up the good to go for the great. Don't be afraid to give up the good to go for the great. tasks for second page are fold sail new_name is May_25_2016.pdf
      #!/usr/bin/perl use v5.14; use utf8; use PDF::API2; use Time::Piece; use Time::Seconds; my $t = Time::Piece->localtime + ONE_DAY; my $today = $t->strftime("%a, %b %d, %Y"); print "$today\n"; my $header = "List for $today"; my $pdf = PDF::API2->new( width => 595, # A4 dimensions in point height => 842, # 1 point = 1/72 inch ); my $page = $pdf->page; my $txt = $page->text; my $font = $pdf->corefont('Times-Roman'); #header my $linepos = 650; $txt->font( $font, 32 ); $txt->translate( 100, $linepos ); $txt->text( $header ); $linepos -= 50; #add randomtext my $quote = get_quote(); print "$quote\n"; $txt->font( $font, 20 ); $txt->lead(20); # line spacing $txt->translate( 100, $linepos ); $txt->paragraph($quote, 400, 100, -align => "left" ); my $overflow_text = $txt->textpos(); $linepos -= 100; my @task = ( 'wake no later than 8', 'stretch/take meds', 'make coffee/eat cereal', 'eat salad', 'bring food', 'make coconut phone', 'check provisions', 'tie knots', 'fold sail',, ); $txt->font( $font, 20 ); my $vspace = 70; while ( (@task)&&($linepos>0) ) { my $item = shift @task; my $msg = '[ ] ' . $item; drawline( $page, $linepos ); $txt->translate( 70, $linepos+10 ); $txt->text($msg); $linepos -= $vspace } say "tasks for second page are @task"; my $new_name = $t->strftime("%b_%d_%Y.pdf"); say "new_name is $new_name"; $pdf->saveas($new_name); sub drawline { my ( $page, $y ) = @_; my $x1 = 50; my $x2 = 550; my $line = $page->gfx; $line->linewidth(3); $line->move( $x1, $y ); $line->line( $x2, $y ); $line->stroke; } sub get_quote { use HTML::TreeBuilder 5 -weak; my $site = 'http://motivationgrid.com/50-inspirational-quotes-to-li +ve-by/'; my $tree = HTML::TreeBuilder->new_from_url($site); my @quotes; for ( $tree->look_down( _tag => 'p' ) ) { if ( ( my $t = $_->as_text ) =~ m{ ^ \d+ \. \s+ }x ) { $t =~ s{ \x{2019} }{'}gx; $t =~ s{ \xA0 }{ }gx; $t =~ s{ \x{2013} }{--}gx; push @quotes, $t; } } my $randomelement = $quotes[ rand @quotes ]; print "$randomelement\n"; $randomelement =~ s/^\d+\.\s+//; return $randomelement; }

      How would a person represent an appointment in such a scheme?

        The co-ordinates start bottom left of page. Start a new page when your line position gets to the end of the page like this.

        for my $item (@task) { my $msg = '[ ] ' . $item; drawline( $page, $linepos ); $txt->translate( 70, $linepos+10 ); $txt->text($msg); $linepos -= $vspace; if ($linepos < 0){ $page = $pdf->page; $txt = $page->text; $txt->font( $font, 20 ); $linepos = 650; } }

        You might consider a newpage sub to repeat the heading and add a page number.

        How would a person represent an appointment in such a scheme?

        Sorry but I don't understand the question

        poj

        How would a person represent an appointment in such a scheme?
        in a first draft, you could add it to @task but you'd have to guess for the right position.
        Most calendars have different areas (e.g. columns) for tasks (without particular time) and events (on a time scale). I'd go for that, too.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1163766]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-24 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found