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!

In reply to PDF Letter format by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.