View Full Version : Using PDF.php

02-10-2006, 06:36 PM

Total newbie question : I found this code PDF.php on www.zend.com .

I ftp it " as is" to the location where all my other php files are. I then added the foll. in another php file which i want to appear as pdf. Clearly i am doing something wrong because it says headers already sent. All my php files have as the first line <? include("variable.php")?> which contains db data etc.

<? include("variable.php")?>
<font face="Tahoma" size="2">
<html> // is this needed ?
<head> // is this needed ?
<meta http-equiv="Content-Type" // is this needed ?
content="text/html; charset=iso-8859-1"> // is this needed ?

require 'PDF.php'; // Require the lib.
$pdf = &PDF::factory('p', 'a4'); // Set up the pdf object.
$pdf->open(); // Start the document.
$pdf->setCompression(true); // Activate compression.
$pdf->addPage(); // Start a page.
$pdf->setFont('Courier', '', 8); // Set font to arial 8 pt.
$pdf->text(100, 100, 'First page'); // Text at x=100 and y=100.
$pdf->setFontSize(20); // Set font size to 20 pt.
$pdf->text(100, 200, 'loandata.php'); // Text at x=100 and y=200.
$pdf->addPage(); // Add a new page.
$pdf->setFont('Arial', 'BI', 12); // Set font to arial bold italic 12 pt.
$pdf->text(100, 100, 'Second page'); // Text at x=100 and y=200.
$pdf->output('loandata.pdf'); // Output the file named loandata.pdf

</head> // is this needed ?

$sscid = $_POST["sscid"];
$user = $_POST["user"];
$year = $_POST["year"];
$other = $_POST["other"];
$loan = $_POST["loan"];
$days = $_POST["days"];
$cdate = $_POST["cdate"];
list($cyear, $cmonth, $cday) = explode('-', $cdate);
$cdate1 = $cday."-".$cmonth."-".$cyear ;


mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

or die ("Unable to select database.");

$result = mysql_query("SELECT * FROM `users` WHERE user='user' ");
$myrow = mysql_fetch_array($result);
$first_name = $myrow["firstname"];
$last_name = $myrow["lastname"];

echo $first_name; echo " "; echo $last_name;
<p></strong>Loan : Rs.
echo $loan;
: Rs.
echo $days;
</strong>&nbsp; </strong> dated
echo $cdate1 ;

mysql_connect("localhost", $dbname, $dbpasswd )
or die ("Unable to connect to server.");

or die ("Unable to select database.");

$sql3 = "UPDATE `Contract` SET `Loan` = $loan , `Days` = $days, `CDate` = '$cdate' WHERE `SSCID` = $sscid";
//echo $sql3;

$result3 = mysql_query($sql3) or die (mysql_error());


Could someone guide me on how I should modify the script ? Thanks.

02-11-2006, 02:49 PM
remove the html-code
+ are you trying this with Internet Explorer? there is an IE bug that will generate a header-related error if you have a session_start() in your php-code and then try to stream the pdf to the client.

02-11-2006, 04:56 PM
I removed the <html> and </html> tags and yes I am using IE. Is there a way to use it short of changing browsers ?


02-12-2006, 11:15 AM
i didn't find any fix.

the workaround i implemented was:
- on page 1, serialize the session-collection and store it in the db and get the primary key of the table is stored it in
- redirect to page2 with the PK in the querystring
- on page 2 select the stored string and unserialize it. all references to sessionvariables then need to be changed to the array that contains the unserialized string.

this doesn't cause the IE bug to crash IE.
By the way, when you are setting up dynamic pdf-generation and you are debugging your pages, make sure you close all browserwindows after you corrected an error. Else, the pdf wount show when you refresh the page.