Pedraz
asked on
PHP MYSQL and PDFLIB Reporting problems
I am trying now to create a report on PHP MYSQL using the PDFlib class. I am using as base one of the examples (the only one) I found to get something similar (invoice.php). I have got a blank page on my browser and a error on Adobe Acrobat (File does not begin with '%PDF-')
I find problems to find how to pass the information from a mysql query to a table on pdf
Any help will be really welcome. I am beginner.
Here is the PHP monster I created. What is wrong. My guess is the $data[] variable twice on the program....I am not able to draw a table to keep there the data
Many thanks
I find problems to find how to pass the information from a mysql query to a table on pdf
Any help will be really welcome. I am beginner.
Here is the PHP monster I created. What is wrong. My guess is the $data[] variable twice on the program....I am not able to draw a table to keep there the data
Many thanks
<?php
/*$Id: stareport.php, v 1.0 15/05/2009 rjs Exp $
*
*PDFlib client. stability report in PHP
*/
$left = 55;
$right = 530;
$fontsize = 12;
$pagewidth = 595;
$pageheight = 842;
$infile = "c:/wamp/www/pdf/data/stationeryreport.pdf";
//Table in database has 9 fields
$baseopt = "ruler { 15 25 160 200 240 280 360 410 460} " .
"tabalignment { right left right right right right right right right} " .
"hortabmethod ruler fontsize 12 ";
/*this is where font/image/PDF input files live*/
$searchpath = "c:/wamp/www/stability/data/";
//this is the connection to mysql
require ('connectionRoot.php');
$queTbl = "SELECT id, item, wts, lcg, tcg, vcg, wts*lcg as LMMT, wts*tcg as TMMT, wts*vcg FROM lightship";
$res = mysql_query($queTbl, $conexion) or die(mysql_error());
$tot = mysql_num_rows($res);
//Database is numeric , it has only one column with string on the field {item}
$ixx = 0;
while($datatmp = mysql_fetch_assoc($res)) {
$ixx = $ixx+1;
$data[] = array_merge($datatmp, array('num'=>$ixx));
}
try{
$p = new PDFlib();
# This means we must check return values of load_font() etc.
$p->set_parameter("errorpolicy", "return");
/* Set the search path for fonts and PDF files */
$p->set_parameter("SearchPath", $searchpath);
/* This line is required to avoid problems on Japanese systems */
$p->set_parameter("hypertextencoding", "winansi");
/* open new PDF file; insert a file name to create the PDF on disk */
if ($p->begin_document("", "") == 0) {
die("Error: " . $p->get_errmsg());
}
$p->set_info("Creator", "pdfrepgenPDFlib.php");
$p->set_info("Author", "Pedraz");
$p->set_info("Title", "Create report from mySql sample (PHP)");
$stationery = $p->open_pdi($infile, "", 0);
if ($stationery == 0) {
die("Error: " . $p->get_errmsg());
}
$page = $p->open_pdi_page($stationery, 1, "");
if ($page == 0){
die("Error: " . $p->get_errmsg());
}
$boldfont = $p->load_font("Helvetica-Bold", "winansi", "");
if ($boldfont == 0){
die("Error: " . $p->get_errmsg());
}
$regularfont = $p->load_font("Helvetica", "winansi", "");
if ($regularfont == 0){
die("Error: " . $p->get_errmsg());
}
$leading = $fontsize + 2;
/*Establish coordinates with the origin in the upper left corner*/
$p->begin_page_ext($pagewidth, $pageheight, 'topdown');
$p->fit_pdi_page($page, 0, $pageheight, "");
$p->close_pdi_page($page);
$p->setfont($regularfont, $fontsize);
/*print left note on the top*/
$y = 140;
$p->set_value("leading",$leading);
$p->show_xy("1.- Lightship & Various", $left, $y);
$p->continue_text("");
$p->setfont($regularfont, $fontsize - 4);
$p->continue_text("(This is the report for lightship & various weights once the Moments have been calculated)");
/* print the report header line (here they are the field names */
$y = 180;
$buf = sprintf("\tID\tItem\tWeigth\tLCG\tTCG\tVCG\tLMMT\tTMMT\tVMMT");
$optlist = sprintf("%s font %d ", $baseopt, $boldfont);
$textflow = $p->create_textflow($buf, $optlist);
if ($textflow == 0){
die("Error: " . $p->get_errmsg());
}
$p->fit_textflow($textflow, $left, $y-$leading, $right, $y, "");
$p->delete_textflow($textflow);
$y += 2*$leading;
$total = 0;
$optlist = sprintf("%s font %d ", $baseopt, $regularfont);
//I think the big problem is here......
for ($i = 0; $i < count($data); $i++){
//$sum = $data[$i]{"price"}*$data[$i]{"quantity"};
$sum = $data[$i]{"wts"};
$buf = sprintf("\t%d\t%s\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f", $i+1, $data[$i]{"id"},
$data[$i]{"item"}, $data[$i]{"wts"},$data[$i]{"lcg"},$data[$i]{"tcg"},$data[$i]{"vcg"},$data[$i]{"LMMT"},$data[$i]{"TMMT"},$data[$i]{"VMMT"});
$textflow = $p->create_textflow($buf, $optlist);
if ($textflow == 0){
die("Error: " . $p->get_errmsg());
}
$p->fit_textflow($textflow, $left, $y-$leading, $right, $y, "");
$p->delete_textflow($textflow);
//$sum is the sum of the field {"wts"}
$y += $leading;
$total +=$sum;
}
$y += $leading;
$p->setfont($boldfont, $fontsize);
$p->fit_textline(sprintf("%.2f",$total), $right, $y, "position {100 0}");
$p->end_page_ext("");
$p->end_document("");
$p->close_pdi($stationery);
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=pdfrepgenPDFlib.pdf");
print $buf;
}
catch (PDFlibException $e) {
die("PDFlib exception occurred in invoice sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
}
catch (Exception $e) {
die($e);
}
$p = 0;
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Great! Thanks for the points. I'm glad FPDF is working for you. I have found it to be very useful. Best, ~Ray
ASKER