Syntax for PDF::Create

software development

#1

I’ve been trying to use PDF::Create to format a pdf report out of my MySQL tables. I’ve been able to get my data out of the table and into arrays, but I’m having trouble getting lines formatted for the PDF using PDF::Create’s page method. When I say:
$page->stringc($f1, 10, 100, 500, $date[1],’ ',$chinook[1]);

all I get is the $date. Am I going to have to format a row of data as one big string to make $page->stringc to work for me?


#2

According to CPAN, yes, only one parameter is accepted for the string value.

$page->stringc($f1, 10, 100, 500, $date[1],' ',$chinook[1]);should be

$page->stringc($f1, 10, 100, 500, join(' ', $date[1], $chinook[1]));And before you start to wonder where join came from, its a standard function:

join EXPR,LIST Joins the separate strings of LIST into a single string with fields separated by the value of EXPR, and returns that new string. :cool: Perl / MySQL / HTML+CSS


#3

Thanks for the good ideas.

I’m going to try another approach. I think I’ll try looping through the arrays and build the table by columns since the pm has the stringr to align right. My chinook counts will be from 1 to 3 digits in length, and maybe this will be the easy way to get the columns to line up. If stringr doesn’t give me the results I’m looking for, there’s stringl.


#4

Ok, that works pretty slick: http://ykfp.org/cgi-bin/pdf/dbpdf1.pdf
But I don’t understand why if I uncomment line 77, the PDF module complains about “Use of uninitialized value in split” and "Use of uninitialized value in substitution (s///).

Next I’ll have to figure out if I want to make the page bigger or trigger page breaks. Also need to format the column headings and drop the TOC or make it useful.

64 $sth->execute() or die $DBI::errstr;
65 $count = 0;
66 $line = 500;
67 while (my @row = $sth->fetchrow_array ()) {
68 $date[$count] = $row[0];
69 $chinook[$count] = $row[1];
70 $jchinook[$count] = $row[2];
71 $steelhead[$count] = $row[3];
72 $wsteelhead[$count] = $row[4];
73 $page->stringr($f1, 10, 100, $line, $date[$count]);
74 $page->stringr($f1, 10, 140, $line, $chinook[$count]);
75 $page->stringr($f1, 10, 180, $line, $jchinook[$count]);
76 $page->stringr($f1, 10, 220, $line, $steelhead[$count]);
77 # $page->stringr($f1, 10, 260, $line, $wsteelhead[$count]);
78 $line = $line - 12;
79 ++$count;
80 }


#5

As I work on this to set up page two and three, with column headers repeated on each page, with date stamps, page numbers, and column totals on the last page, I occurred t me that I might be reinventing the wheel. Are there any Perl modules out there that would assist in formatting the report from lines selected from MySQL? Maybe I could get a formatted report and then feed it line by line into page->string.


#6

[quote]Ok, that works pretty slick: http://ykfp.org/cgi-bin/pdf/dbpdf1.pdf
But I don’t understand why if I uncomment line 77, the PDF module complains about “Use of uninitialized value in split” and "Use of uninitialized value in substitution (s///).[/quote]
Perhaps the PDF::Create module is not checking to see if the string you pass is defined or not, you don’t appear to be doing that yourself. Try something like this:

68 $date[$count] = $row[0] || 'unknown'; 69 $chinook[$count] = $row[1] || 'unknown'; 70 $jchinook[$count] = $row[2] || 'unknown'; 71 $steelhead[$count] = $row[3] || 'unknown'; 72 $wsteelhead[$count] = $row[4] || 'unknown';That way, if the row returned from mysql has NULL values, they become ‘unknown’ instead of undefined. A more verbose way to put it is:

[code]if (defined $row[4]) {

column value not NULL

$wsteelhead[$count] = $row[4];
}
else {

column value is NULL

$wsteelhead[$count] = ‘unknown’;
}[/code]Or a more terse way:

:cool: Perl / MySQL / HTML+CSS


#7

By random changes and retyping, I got line 77 to work, but I’m not sure how. Is it only because I shortened the $ variable to $sth?

67 while (my @row = $sth->fetchrow_array ()) {
68 $date[$count] = $row[0];
69 $chinook[$count] = $row[1];
70 $jchinook[$count] = $row[2];
71 $steelhead[$count] = $row[3];
72 $std[$count] = $row[4];
73 $page->stringr($f1, 10, 100, $line, $date[$count]);
74 $page->stringr($f1, 10, 200, $line, $chinook[$count]);
75 $page->stringr($f1, 10, 300, $line, $jchinook[$count]);
76 $page->stringr($f1, 10, 400, $line, $steelhead[$count]);
77 $page->stringr($f1, 10, 500, $line, $std[$count]);
78 # $page->stringr($f1, 10, 500, $line, $row[4]);
79 $line = $line - 12;
80 ++$count;
81 }
82 print ("$count rows total\n");
83 $dbh->disconnect;


#8

I don’t see anything that would make a difference, so perhaps this time around you did not have any NULL values from the database?

:cool: Perl / MySQL / HTML+CSS