in reply to vertical spacing with PDF::API2

.. 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.

#!/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 = "A quote with a lot of words that spreads over more than one line and possibly even extends to three lines filling the box size of 400 wide 100 deep."; 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',, ); $txt->font( $font, 20 ); my $vspace = 70; for my $i ( 0 .. $#task ) { my $msg = '[ ] ' . $task[$i]; drawline( $page, $linepos ); $txt->translate( 70, $linepos+10 ); $txt->text($msg); $linepos -= $vspace } 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; }
poj

Replies are listed 'Best First'.
Re^2: vertical spacing with PDF::API2
by Aldebaran (Curate) on May 24, 2016 at 08:51 UTC

    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.

        I was fine with the closure on this thread as it regarded the original subject, but if it's perl, you know that I'm thinking about my next way to represent something. What's more, I've been thinking of it in the context of re-reading _Intermediate Perl_, and so I want to replicate their development of Gilligan's Island with my Perl Daily Briefings who are destined to get lost and need a physical list so as to figure out what the heck I really had to do today as opposed to what one might think in a moment of hysteria.

        That said, let me re-state the question, how would you represent an appointment that would appear on your list for the next day?