Page 1 of 1

fPDF / HTML to PDF - wth Dynamic Data

Posted: Wed Oct 06, 2010 3:39 pm
by diseman
Hi Everyone,

New programmer here having some problems printing my dynamic data to PDF.

Although I can get a .php page to print in PDF, it does not include any of the dynamic data and it does include some of the php code that shouldn't be printed to PDF.

This is the code in 1.php I'm using to call the contact_pdf.php page I want to convert to PDF:

Code: Select all

require('../scripts/html2pdf/html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();
$fp = fopen("../modules/contact_pdf.php","r");
$strContent = fread($fp, filesize("../modules/contact_pdf.php"));
fclose($fp);
$pdf->WriteHTML($strContent);
$pdf->Output("C:\Users\Me\Desktop\sample.pdf");
echo "PDF file is generated successfully!";
The sample.pdf does get placed on the desktop, but with no dynamic data. The contact_pdf.php page has my QUERY & RESULTS with the HTML below it. When this page is called directly, the dynamic data is there.

Has anyone used this fPDF and made if work? Any examples or explanations you could share.

I also tried putting the code above into the contact_pdf.php page at the top, at the bottom, but nothing worked.

Re: fPDF / HTML to PDF - wth Dynamic Data

Posted: Wed Oct 06, 2010 3:49 pm
by Benjamin

Code: Select all

fopen("../modules/contact_pdf.php","r");
fopen is simply reading the file. It is not parsing it. You may want to look into using output buffering and include the file instead.

Re: fPDF / HTML to PDF - wth Dynamic Data

Posted: Wed Oct 06, 2010 4:29 pm
by diseman
I'm sure your explanation is painfully clear and simple, but could you give a beginner an example of what you mean?

Thanks..

Re: fPDF / HTML to PDF - wth Dynamic Data

Posted: Wed Oct 06, 2010 10:46 pm
by Benjamin
Yes, something like this:

Code: Select all

ob_start();
include 'path/to/file.php';
$output = ob_get_clean();
$pdf->WriteHTML($output);

Re: fPDF / HTML to PDF - wth Dynamic Data

Posted: Thu Oct 07, 2010 9:43 am
by diseman
Thank you Benjamin for that snippet of code! That was the stuff. : )

For anyone else struggling through this, here's the final code:

Code: Select all

ob_start();
include '../path-to-the/template-you-want/to-convert-to-pdf' ;
require('../scripts/html2pdf/html2fpdf.php');  //obviously the path to the script file
$pdf=new HTML2FPDF();
$pdf->AddPage();
$output = ob_get_clean();
$pdf->WriteHTML($output);
$pdf->Output("C:\Users\your_name\Desktop\sample.pdf"); // path to where you want the output pdf file placed. Desktop is good for starters
echo "PDF file is generated successfully!";
...and for those of you who want to output more than one page/template to the pdf file, you can thank Benjamin for this code:

Code: Select all

// 1st page to include in PDF doc

ob_start();
include '../path-to-the/template-you-want/to-convert-to-pdf' ;
require('../scripts/html2pdf/html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();
$output = ob_get_clean();
$pdf->WriteHTML($output);

// 2nd page to include in PDF doc

ob_start();
include '../path-to-the/template-you-want/to-convert-to-pdf' ;
$pdf->AddPage();
$output = ob_get_clean();
$pdf->WriteHTML($output);


// Seperate this code from above and run only once

$pdf->Output("C:\Users\your_name\Desktop\sample.pdf");
echo "PDF file is generated successfully!";
The code directly above creates ONE pdf with each page on its own separate page. Meaning... you can simply continue adding pages until you've got them all.

Now, when I'm finished making all my pages, I have to figure out how to email it. E-mailing is surprisingly easy I've learned, but just need to figure out the logic of getting this output into an e-mail as an attachment. Not sure if I have to save the doc first and then e-mail or can I go directly to emailing it. I'm sure I can do the latter; just need to figure it out. : )

Thanks again BENJAMIN, but don't go away just yet. :D Part II is still to come. lol