[Pdfapi2-users] Fwd: Help - how to print SQL query data into PDF for a report
Status: Beta
Brought to you by:
areibens
|
From: Shivam M. <shi...@ho...> - 2014-10-20 23:09:17
|
Hello Gurus,
I am looking for help in printing data from an SQL Select query into PDF using PERL.
Here is my code and it writes only ‘1’ in the PDF instead of actual country code. Please advise.
My requirement is to print each country code in the record set on a new page in PDF.
use PDF::Create ;
use DB ;
use DBI ;
use Time::Format ; # qw(%time %strftime %manip);
use POSIX qw(strftime) ;
print "Timestamp: $time{'yyyymmdd.hhmmss.mmm'}\n" ;
my $ fileTime = $ time { 'yyyymmdd.hhmmss.mmm' };
# initialize PDF
my $ filename_path = "C:\\Project\\Perl_Workspace\\" ;
my $ file_name ;
$ file_name = "Test_PDF_Image_Print_" . $ fileTime ;
my $ file_format = ".pdf" ;
my $ dest_file = $ filename_path . $ file_name . $ file_format ;
my $ pdf = PDF::Create -> new ( 'filename' => $ dest_file ,
'Author' => 'PerlNewB' ,
'Title' => 'Sample PDF' ,
'CreationDate' => [ localtime ], );
# add a A4 sized page
my $ a4 = $ pdf -> new_page ( 'MediaBox' => $ pdf -> get_page_size ( 'A4' ));
# Add a page which inherits its attributes from $a4
my $ page = $ a4 -> new_page ;
# Prepare a font
my $ f1 = $ pdf -> font ( 'BaseFont' => 'Helvetica' );
# Prepare a Table of Content
my $ toc = $ pdf -> new_outline ( 'Title' => 'Title Page' , 'Destination' => $ page );
# Connect to Oracle Database and return data using a query and store
it in a variable
my $ user = "user1" ;
my $ password = "password1" ;
my $ v_data ;
my $ db = DBI -> connect ( "dbi:Oracle:ORA1" , "$user" , "$password" ) || die ( $ DBI::errstr . "\n" );
$ db ->{ AutoCommit }
= 0 ;
$ db ->{ RaiseError }
= 1 ;
$ db ->{ ora_check_sql }
= 0 ;
$ db ->{ RowCacheSize }
= 16 ;
my $ SEL = "select Country_code from country where rownum<100" ;
my $ ctr = 1 ;
my $ ctr1 = 1 ;
while ( $ ctr < 10 ) {
# include a jpeg image with scaling to 20% size
my $ jpg = $ pdf -> image ( "C:\\Users\\user1\\Pictures\\image1.jpg" );
$ page -> image ( 'image' => $ jpg , 'xscale' => 0 . 2 , 'yscale' => 0 . 3 , 'xpos' => 50 , 'ypos' => 810 );
####Printing Database Values On PDF####
my $ sth = $ db -> prepare ( $ SEL );
$ sth -> execute ();
while ( my @ row = $ sth -> fetchrow_array ()
) {
$ v_data = @ row ;
print $ v_data ;
$ page -> stringc ( $ f1 , 20 , 306 , 300 , $ v_data );
}
$ sth -> finish ();
# Add another page
my $ page2 = $ a4 -> new_page ;
$ jpg = $ pdf -> image ( "C:\\Users\\mathus01\\Pictures\\BOTW
3x1 lockup.jpg" );
$ page2 -> image ( 'image' => $ jpg , 'xscale' => 0 . 2 , 'yscale' => 0 . 3 , 'xpos' => 50 , 'ypos' => 810 );
$ ctr ++;
}
$ pdf -> close ;
END {
$ db -> disconnect if defined ( $ db );
}
Thank you!
|