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

Hi there Monks!
I am looking for suggestions on how more efficient this code could be. It creates a standard letter. It works,
the problem I am having is how to add extra new lines to the main text of the main body on the letter, like between “Sincerely” as an example.
I am using "\n" new lines to set where to end the lines, it might not
be the right way of doing it. Anyways, any suggestions!
Here is the code I am using:
#!/usr/bin/perl use strict; use warnings; use PDF::API2; my $font_size=10; my $pdf = PDF::API2->new(-file => "test.pdf"); $pdf->mediabox('A4'); my $page = $pdf->page; my $fnt = $pdf->corefont('Helvetica',-encode => 'latin1'); my $boldfont=$pdf->corefont('Helvetica-Bold',-encode => 'latin1'); my $txt = $page->text(); my $gfx=$page->gfx; $txt->textstart; $txt->font($boldfont,$font_size); # 1X moves right and left - #1Y more moves up and down $txt->translate(380,740); $txt->text("November 21, 2014"); my $image=$pdf->image_jpeg('logo.jpg'); $gfx->image( $image, 20, 770 ); $txt->font($fnt,9); $txt->translate(20,740); $txt->text("I am here"); $txt->font($fnt,9); $txt->translate(20,728); $txt->text("P.O.Box 1234"); $txt->translate(20,717); $txt->text("Earth, ET. 1234-1234"); $txt->translate(20,705); $txt->text("(000) 000-0000"); $txt->font($boldfont,$font_size); $txt->translate(20,650); $txt->text("John The III"); $txt->translate(20,636); $txt->text("A Company Somewhere"); $txt->translate(20,621); $txt->text("12345 Main St. Ste 001"); $txt->translate(20,606); $txt->text("Saturn, ET. 00000"); $txt->font($fnt,10); $txt->translate(20,550); $txt->text("Dear Mr. ET III"); # Main content settings $txt->font($fnt, $font_size); $txt->translate(100,800); $txt->font($fnt, $font_size); # Main text content position on the page my $y=523; my $summary = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac + libero dui. Sed massa quam, ultricies in faucibus nec, volutpat sed velit. Donec aliquet, elit si +t amet hendrerit eleifend, augue augue volutpat enim, quis posuere neque arcu eget nisi. Vestibu +lum ex erat, semper vitae blandit ut, pulvinar mattis lorem. Sed urnaest, ullamcorper quis feug +iat sed, venenatis in lorem. Donec facilisis dolor nibh, sit amet lacinia est viverra vel. Praesent in leo id tellus sagittis luctus. Maecenas quis ante mollis, viverra eros nec, luctus ante. Duis eget sa +pien rutrum, euismod velit et, mattis massa. Aeneanegestas et diam et venenatis. Praesent erat neque +, lacinia vulputate ornare quis, accumsan nec metus. Fusce sollicitudinid risus vulputate aliquam. Cum + sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Interdumet malesuada f +ames ac ante ipsum primis in faucibus. Suspendisse ultricies, nisi ac lobortis posuere, lacus odio porta eros +, Suspendisse ultricies, nisi ac lobortis posuere, lacus odio porta eros +. Sincerely, Mr. Joe Doe Manager Services Manager ET "; my @all_lines = split/\n+/, $summary; foreach my $rows (@all_lines){ #my $string=join(" ",$rows)."\n"; $txt->translate(20,$y); $txt->text("$rows"); $y-=20; } # Bottom Data $txt->font($boldfont,10); $txt->translate(20,70); $txt->text("Code Number:00007"); $txt->textend; $pdf->save; $pdf->end( );
Thanks for looking!

Replies are listed 'Best First'.
Re: PDF Letter format
by blindluke (Hermit) on Nov 21, 2014 at 17:32 UTC

    Look at the following part of your code:

    $txt->font($fnt,9); $txt->translate(20,740); $txt->text("I am here"); $txt->font($fnt,9); $txt->translate(20,728); $txt->text("P.O.Box 1234");

    What it does, is it prints one line of text at (20,740), and then another line at (20,728). The coordinates are given in pt (same unit as font size), and the (0,0) point is located in the lower left point of your page. If you want to put another line below the send one, you would have to subtract some pt from its Y coordinate. The font size is 9pt, so in this example, by subtracting 12pt you get one line below, with an interline roughly equivalent to 130% (in a word processor).

    If you want to add extra vertical space, just mess around with the coordinates. Good luck!

    - Luke

      That part is understandable, my point is more towards the control over the text in here, notice the spaces between paragraphs:
      my $summary = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac + libero dui. Sed massa quam, ultricies in faucibus nec, volutpat sed velit. Donec aliquet, elit si +t amet hendrerit eleifend, augue augue volutpat enim, quis posuere neque arcu eget nisi. Vestibu +lum ex erat, semper vitae blandit ut, pulvinar mattis lorem. Sed urnaest, ullamcorper quis feug +iat sed, venenatis in lorem. Donec facilisis dolor nibh, sit amet lacinia est viverra vel. Praesent in leo id tellus sagittis luctus. Maecenas quis ante mollis, viverra eros nec, luctus ante. Duis eget sa +pien rutrum, euismod velit et, mattis massa. Aeneanegestas et diam et venenatis. Praesent erat neque +, lacinia vulputate ornare quis, accumsan nec metus. Fusce sollicitudinid risus vulputate aliquam. Cum + sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Interdumet malesuada f +ames ac ante ipsum primis in faucibus. Suspendisse ultricies, nisi ac lobortis posuere, lacus odio porta eros +, Suspendisse ultricies, nisi ac lobortis posuere, lacus odio porta eros +. Sincerely, Mr. Joe Doe Manager Services Manager ";

      Thanks!
        You can create multi-line text like this
        $txt->lead(13); # line spacing foreach my $para (@paragraphs){ $txt->translate(20,$y); # width, max height $txt->paragraph($para, 490, 800, align => "left" ); (undef, $y) = $txt->textpos(); $y -= 20; # paragraph spacing }
        poj
        For my sanity, the answer to my own question will be changing this line:
        my @all_lines = split/\n+/, $summary;
        To this:
        my @all_lines = split/\n/, $summary;
        This way, “split” will only match one new line character and if I want to add new lines, I just type it.
        But any suggestions is always welcome!