Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

poj's scratchpad

by poj (Priest)
on Jun 12, 2013 at 17:16 UTC ( #1038520=scratchpad: print w/ replies, xml ) Need Help??

I don't understand what you mean by 'if the account.pl script failed the pdf would get saved ?', Which pdf, the generic template or the specific account one ?. You can put the template in a different path to the account ones.

Instead of starting with a blank document every time, use a template that has the stuff that never changes and just add the account specific stuff and then save with a differennt name. Next account starts with the same template and saves with another name.

Be aware that on your original script $avl_date2 (two) is on the left under $amt_delayed1 (one) - not sure if you inteded that

#!perl use strict; use warnings; use PDF::API2; use MyModule; # db config in here # connect to database my $dbh = MyModule::dbh; # not get_dbh # prepare, execute SQL my $sql = 'SELECT acc_num, acc_name, item_amt, etc'; my $sth = $dbh->prepare($sql); $sth->execute(); while (my $hr = $sth->fetchrow_hashref){ my $pdf = createPDF('name of PDF file',$hr); $pdf->save(); } $dbh->disconnect; sub createPDF { my ($filename,$hr) = @_; my $acc_num = $hr->{'acc_num'}; # # lots of code here, cut and paste # from scripts below # return $pdf; }
------------------
file MyModule.pm
package MyModule; use strict; use DBI; my $USER = 'scott'; my $PWD = 'tiger'; my $DSN = 'dbi:Oracle:HOSTNAME:1521/ORCL'; sub dbh { my $dbh = DBI->connect($DSN, $USER, $PWD, { RaiseError => 1, AutoCommit => 0, ora_check_sql => 0, RowCacheSize => 16, }) or die ($DBI::errstr); return $dbh; } 1;
===================================================
#!perl # template.pl use strict; use warnings; use PDF::API2; use constant mm => 25.4 / 72; my $pdf = PDF::API2->new("template.pdf"); my $font = $pdf->corefont( 'Arial', -encoding => 'latin1' ); # Correct dimensions of page for a PDF # for A4 paper printing my $page = $pdf->page(); $page->mediabox( 215.9 / mm, 279.4 / mm); my $picture = 'test.jpg'; if (-e $picture){ my $photo = $page->gfx; my $photo_file = $pdf->image_jpeg($picture); $photo->image( $photo_file, 20, 610.0, 70/mm, 70/mm); } else { die ("Unable to find image file: $!"); } # static text my $text = $page->text; $text->font( $font, 16 ); $text->fillcolor('black'); $text->textlabel(425, 725, $font, 16, "NOTICE OF HOLD"); my @labels = ( [30,505,'Account No:'], [380,505,'Checking:'], [490,505,'Savings:'], [30,485,'Name:'], [380,485,"Today's Date:"], [30,425,'Amount of Item(s):'], [380,425,"Date Deposited:"], [30,405,'Amount Delayed:'], [380,405,"Amount Delayed:"], [30,385,'Available Date:'], [380,385,"Available Date:"], [30,375,'(Date funds are available for withdrawal)'], [380,375,'(Date funds are available for withdrawal)'], ); $text->font( $font, 10 ); for (@labels){ $text->textlabel($_->[0], $_->[1], $font, 10, $_->[2]); } # paragraph text my $para = get_para(); $text->lead(13); $text->translate(30,335); $text->paragraph($para->[1], 490, 100, align => "justify" ); $text->translate(30,206); $text->paragraph($para->[2], 490, 100, align => "justify" ); my $ypos = 106; for (split "\n",$para->[3]){ $text->textlabel(30,$ypos,$font, 10,$_); $ypos -= 12; } $text->textlabel(460,40,$font, 10,$para->[4]); # lines my $g = $page->gfx; $g->strokecolor('black'); for my $y (360,525){ $g->move( 579, $y ); $g->line( 30, $y ); $g->stroke; } $pdf->saveas('template.pdf'); $pdf->end(); sub get_para{ my @p=(); $p[1] = qq|The item(s) described above, included in your deposit, is + drawn on another financial institution. We are delaying the availab +ility of this item(s) because the funds from the check have not been +collected. If this time period is greater than 2 days it is because: +|; $p[2] = qq|Notice: If you did not receive this notice at the time +you made the deposit or cashed a check, and the check you deposited/c +ashed is paid, we will return to you any fee for the overdraft or ret +urned checks that result solely from the additional delay that we are + imposing. To obtain a refund of such fee, |; $p[3] = qq|400-400-4000 www.google.com XXXXX|; $p[4] = qq|(Disclosure revision: 02/2015)|; return \@p; } ====================================================== #!perl # account.pl use strict; use warnings; use PDF::API2; use constant mm => 25.4 / 72; my $pdf = PDF::API2->open("template.pdf"); my $page = $pdf->openpage(1); my $font = $pdf->corefont( 'Arial', -encoding => 'latin1' ); my $text = $page->text; $text->font( $font, 10 ); # x,y,field name my %value = ( 'acc_num' =>[140,505],'chk_X' =>[440,505], 'acc_name' =>[140,485],'dt_today' =>[490,485], 'item_amt' =>[140,425],'date_deposited' =>[490,425], 'amt_delayed1' =>[140,405],'amt_delayed2' =>[490,405], 'available_date1'=>[140,385],'available_date2'=>[490,385], 'sav_X' =>[550,505],'hold_reason' =>[30,290], ); # variable data my $rec = get_record(); # align these 2 my $w1 = $text->advancewidth($rec->{'item_amt'}); my $w2 = $text->advancewidth($rec->{'amt_delayed1'}); $value{'amt_delayed1'}[0] += ($w1-$w2); for (keys %value){ my ($x,$y) = @{$value{$_}}; if ($_ =~ /acc_name|hold_reason/){ for my $addr (split ",",$rec->{ $_ }){ $text->textlabel($x,$y,$font,10,$addr); $y-=12; } } else{ $text->textlabel($x,$y,$font,10,$rec->{ $_ }); } } # Name and Address my @y = (605,590,575,560); my @addr = ( 'Customer Name','Address','City','State and Zip' ); for my $i (0..3){ $text->textlabel(90, $y[$i], $font, 10, $addr[$i]); } $pdf->saveas('account1.pdf'); $pdf->end(); sub get_record { my $rec = { acc_num => 'AI12345678, AI654321', acc_name => 'First,Middle,Last', item_amt => '7000.00', amt_delayed1 => '40.00', available_date1=> '11/16/2014', date_deposited => '11/13/2014', amt_delayed2 => '3000', available_date2 => '11/16/2014', dt_today => '11/15/2014', chk_X => 'CHK X', sav_X => 'SAV X', hold_reason => 'Large Deposit,The Check(s) you deposited on this day +exceed $5000,reason 3,reason 4,reason 5' }; return $rec; }
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-11-22 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (123 votes), past polls