Forum Moderators: coopster
I want to use PDFlib to create PDF invoices on the fly. The data for the invoices is in my DB, and is called via a variable in the URL (pdf.php?a=123)
I have the static parts of the PDF set up, and also the RecordSet (InvoiceData) which will retrieve the relevant data depending on the variable in the URL.
For some reason, I can'T get the dynamic data to show, and I cant work out why. I've tried the following:
PDF_show_xy($mypdf, $InvoiceData, 50, 250);
which results in Resource ID #4 beig shown on the PDF. After searching, I found a few threads that mention that this is because the data is not being fetched. In my case it is.
I would appreciate any help whatsoever....
Heres my code:
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc()? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string")? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue!= "")? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue!= "")? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue!= "")? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue!= "")? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue!= "")? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_InvoiceData = "-1";
if (isset($_GET['a'])) {
$colname_InvoiceData = $_GET['a'];
}
mysql_select_db($database_connection, $connection);
$query_InvoiceData = sprintf("SELECT ID, Name, Adresse, Artikel1, Preis1, Artikel2, Preis2, Artikel3, Preis3, Artikel4, Preis4, Artikel5, Preis5, Artikel6, Preis6, Porto, Gesamtsumme, Datum FROM invoice WHERE ID = %s", GetSQLValueString($colname_InvoiceData, "int"));
$InvoiceData = mysql_query($query_InvoiceData, $connection) or die(mysql_error());
$row_InvoiceData = mysql_fetch_assoc($InvoiceData);
$totalRows_InvoiceData = mysql_num_rows($InvoiceData);
$mypdf = PDF_new();
PDF_open_file($mypdf, "");
PDF_begin_page($mypdf, 595, 842);
$myfont = PDF_findfont($mypdf, "Helvetica", "host", 0);
PDF_setfont($mypdf, $myfont, 9);
$myimage = PDF_open_image_file($mypdf, "jpeg", "header.jpg");
PDF_place_image($mypdf, $myimage, 1, 752, 0.99);
PDF_show_xy($mypdf, "Oxide Tones", 527, 740);
PDF_show_xy($mypdf, "Weinbergstrasse 35", 500, 730);
PDF_show_xy($mypdf, "14469 Potsdam", 516, 720);
PDF_show_xy($mypdf, "Germany", 542, 710);
PDF_show_xy($mypdf, "Tax ID: 13/353/72544", 10, 740);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 10, 705);
PDF_lineto($mypdf, 585, 705);
PDF_stroke($mypdf);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 10, 707);
PDF_lineto($mypdf, 585, 707);
PDF_stroke($mypdf);
PDF_show_xy($mypdf, "Name:", 15, 640);
PDF_show_xy($mypdf, "Adresse:", 15, 630);
PDF_show_xy($mypdf, "Rechnungsnummer:", 420, 640);
PDF_show_xy($mypdf, $row_InvoiceData['ID'], 450, 640);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 10, 600);
PDF_lineto($mypdf, 585, 600);
PDF_stroke($mypdf);
PDF_show_xy($mypdf, "Pos.", 15, 590);
PDF_show_xy($mypdf, "Menge:", 80, 590);
PDF_show_xy($mypdf, "Bezeichnung:", 180, 590);
PDF_show_xy($mypdf, "Preis:", 500, 590);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 10, 585);
PDF_lineto($mypdf, 585, 585);
PDF_stroke($mypdf);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 10, 583);
PDF_lineto($mypdf, 585, 583);
PDF_stroke($mypdf);
PDF_show_xy($mypdf, $InvoiceData], 50, 250);
PDF_show_xy($mypdf, "Porto:", 460, 150);
PDF_show_xy($mypdf, "Gesamtsumme:", 421, 140);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 421, 138);
PDF_lineto($mypdf, 485, 138);
PDF_stroke($mypdf);
PDF_show_xy($mypdf, "Betrag erhalten am:", 80, 120);
PDF_show_xy($mypdf, "Ich bedanke mich fuer Ihren Auftrag und verbleibe mit freundlichen Gruessen,", 80, 100);
$myimage = PDF_open_image_file($mypdf, "jpeg", "signature.jpg");
PDF_place_image($mypdf, $myimage, 390, 50, 0.5);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 10, 45);
PDF_lineto($mypdf, 585, 45);
PDF_stroke($mypdf);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 10, 25);
PDF_lineto($mypdf, 585, 25);
PDF_stroke($mypdf);
PDF_setcolor($mypdf, "stroke", "rgb", 0, 0, 0);
PDF_moveto($mypdf, 10, 23);
PDF_lineto($mypdf, 585, 23);
PDF_stroke($mypdf);
PDF_end_page($mypdf);
PDF_close($mypdf);
$mybuf = PDF_get_buffer($mypdf);
$mylen = strlen($mybuf);
header("Content-type: application/pdf");
header("Content-Length: $mylen");
header("Content-Disposition: inline; filename=gen02.pdf");
print $mybuf;
PDF_delete($mypdf);
mysql_free_result($InvoiceData);
?>
I suspect the below line is working? If not, what does it display?
PDF_show_xy($mypdf, $row_InvoiceData['ID'], 450, 640);
Added: For debugging how to access the data from your database, you can just print_r/echo out your variables above the $mypdf = PDF_new(); line and then call die... like below:
...
$totalRows_InvoiceData = mysql_num_rows($InvoiceData);
print_r($row_InvoiceData);
echo $totalRows_InvoiceData;
die;
$mypdf = PDF_new();
...