In a recent project, I had to output to a PDF file as well as to the browser. For this purpose first I downloaded the library from here. Then this code:
<?php
ob_start(); //Start buffering the output
//your coding starts here
....
....
...
...
//Coding ends here
$html = ob_get_contents(); //dumb the buffer as a string in variable $html
ob_flush(); //Output the buffer to browser
//Start outputting to PDF
define('HTML2FPDF_VERSION','3.0(beta)');
define('RELATIVE_PATH','fpdf/');
define('FPDF_FONTPATH','font/');
require_once(RELATIVE_PATH.'fpdf.php');
require_once(RELATIVE_PATH.'htmltoolkit.php');
require_once(RELATIVE_PATH.'html2fpdf.php');
$pdf = new HTML2FPDF();
$pdf->WriteHTML($html);
$name="doc.pdf";
$pdf->Output($name);
?>
|
When you run this script, you’ll get a pdf file “doc.pdf” in your script directory with the output of the current script.
For outputting something else other than the current script:
<?php
//Your code
....
....
....
// To make a pdf file:
$html= "<h1>hello</h1>"; //To add more html code use $html.= yourcode; in the next line
define('HTML2FPDF_VERSION','3.0(beta)');
define('RELATIVE_PATH','fpdf/');
define('FPDF_FONTPATH','font/');
require_once(RELATIVE_PATH.'fpdf.php');
require_once(RELATIVE_PATH.'htmltoolkit.php');
require_once(RELATIVE_PATH.'html2fpdf.php');
$pdf = new HTML2FPDF();
$pdf->WriteHTML($html);
$name="doc.pdf";
$pdf->Output($name);
?>
|