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(); }

In reply to Text to PDF conversion by aseidas

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.