Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Text to PDF conversion

by aseidas (Beadle)
on Mar 14, 2002 at 19:11 UTC ( #151789=perlquestion: print w/ replies, xml ) Need Help??
aseidas has asked for the wisdom of the Perl Monks concerning the following question:

Hello I am pretty new to PERL so please forgive the messy code. I have been working on a program to take in a text file convert it to pdf format and mail said pdf to a predetermined e-mail address. I have been successful in creating the rough outline of the program however I am having 3 problems. problem #1 The text I am pulling in is riddled with control characters, I am not sure how to get rid of them, chomp will only get rid of the newline character correct? problem #2 I am using MIME::Lite to create the e-mail and Net::SMTP to send it, I have tried to add an error trap using die just in case the mailserver is down but it does not seem to be working. problem #3 I am not sure how best to implement new pages if the text file is more than one page, I have an idea of how I would do it but just wanted suggestions. Here is what I have so far:
#!/usr/bin/perl -w use strict; use PDF::Create; use Net::SMTP; use MIME::Lite; #opens ascii file or else! open TEXT_FILE, "textsample" or die " No text file to open! $!\n"; #sets text file to new array textstrings each line is an element my @textstrings=<TEXT_FILE>; close TEXT_FILE; #creates new pdf file and sets initial attributes my $pdf = new PDF::Create('filename' => 'test1.pdf', 'Author' => 'Aseidas Blauvelt', 'Title' => 'Test Invoice to PDF', 'Version' => '1.3'); #sets to landscape orientation my $root = $pdf->new_page('MediaBox' => [ 0, 0, 792, 612 ]); #give my page the properties of root page my $page = $root->new_page; #give me one font my $f1 = $pdf->font('Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Courier'); #set y postition to top left of page my $y=600; # initilize starting array element my $start=0; foreach $start (@textstrings) { #add string of text from @textstrings to pdf $page->string($f1, 10, 20, $y, $start); #move y position down 10 pixels $y= $y-9; } #add mandatory pdf elements and go ! $pdf->close; &mailpdf; ################################################################## sub mailpdf { #We don't want to use mime to send the message we don't have sendmail #lets use smtp instead, we still need mime to create the multi part me +ssage. MIME::Lite->send('smtp', "smtp.someisp.net", Timeout=>60); #create new message headers my $msg = MIME::Lite->new( From =>'someone@someisp.com', To =>'someone@someisp.com', Cc =>'someone@someisp.com, someone@someisp.com', Subject =>'Text invoice converted to pdf', Type =>'multipart/mixed' ); #attach plain text (body) of message $msg->attach(Type =>'TEXT', Data =>"The attached pdf was converted from text +and mailed to\nyou from the same Perl program"); #attach the pdf to send, after some confusion, path is the path+name o +f the file #filename is the name you want it to have as your atachment $msg->attach(Type =>'document/pdf', Path =>'/home/aseidasb/perl/txt2pdf/test1.pdf', Filename =>'test2.pdf', Disposition => 'attachment' ); #done send it ! $msg->send(); }

Comment on Text to PDF conversion
Download Code
Re: Text to PDF conversion
by talexb (Canon) on Mar 14, 2002 at 20:50 UTC
    1. The text I am pulling in is riddled with control characters, I am not sure how to get rid of them, chomp will only get rid of the newline character correct?
    2. I am using MIME::Lite to create the e-mail and Net::SMTP to send it, I have tried to add an error trap using die just in case the mailserver is down but it does not seem to be working.
    3. I am not sure how best to implement new pages if the text file is more than one page, I have an idea of how I would do it but just wanted suggestions
    1. Yes, chomp only removes newlines if they exist. I'd suggest using the tr operator with the d option.
    2. Sorry, can't help you on that one.
    3. This CPAN page should be useful to you.

    --t. alex

    "Here's the chocolates, and here's the flowers. Now how 'bout it, widder hen, will ya marry me?" --Foghorn Leghorn

Re: Text to PDF conversion
by tachyon (Chancellor) on Mar 14, 2002 at 21:08 UTC

    Here is a snippet that covers two of your issues (1 and 3):

    Update

    Fixed an incorrect localisation of $page to make the code run (well I think it will but am to lazy to test it :-) and rememberd to re-init our $y value when we change pages

    my $lines_per_page = 50; # print 50 lines per page my $y_init = 600; # initial top line pos my ( @pages, $page, $y ); # declare and scope these vars foreach my $line ( 0 .. $#textstrings ) { # get line from array using index my $text = $textstrings[$line]; # remove all control characters using tr with /d(elete) # option. these are specified in octal notation $text =~ tr/\000-\037//d; # we will have removed the traling newline from each line. # I doubt that it is required but if it is... $text .= "\n"; # use modulus operator % to make a new page whenever # the current line number divided by the lines per # page has a remainder of zero. this will get us a new # $page object when $line == 0, 50, 100 ... # we push each completed page into the @pages array if ( $line % $lines_per_page == 0 ) { push @pages, $page; $page = $root->newpage; $y = $y_init; # reset the $y value to page top } # add the text to the current page $page->string( $f1, 10, 20, $y, $text ); # move y position down 10 pixels $y = $y - 10; } # now summate the pages in @pages (if that's what you need) $page = join'', @pages;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Thanks for the good advice, tr worked perfectly to clear the control characters, I have not had time to try the new page suggestion, I will let you know how it turns out. Thanks Again Aseidas

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (14)
As of 2014-07-30 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (231 votes), past polls