简体   繁体   中英

HTML2FPDF print page results as pdf

I'm trying to user HTML2FPDF ( http://html2fpdf.sourceforge.net/ ) to create a PDF of a page, but I can't seem to get it to work properly.

My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.

http://portal.flyingstartlifestyle.360southclients.com/leanne/leanne.php <- the graph with the html2fpdf code at the bottom of the page.

HTML2FPDF code:

function createPDF() {

 define('ABSPATH', dirname(__FILE__).'/');

 require(ABSPATH.'classes/pdf/html2fpdf.php');

 $pdf = new HTML2FPDF();
 $pdf->AddPage();

 $html = ob_get_contents();
 //$html = htmlspecialchars($html);

 if ($html) {

  $fileName = "testing.pdf";

  $pdf->WriteHTML($html);
  $pdf->Output("pdfs/".$fileName);

  echo "<p>PDF file is generated successfully! <a href=\"pdfs/$fileName\" target=\"_blank\">Click here</a> to open it.</p>";

 } else {

  echo "<p>There has been an error in creating your PDF.</p>";

 };


};

If I unhide the line "$html = htmlspecialchars($html);" it prints the pdf the text of the page, otherwise it creates an empty PDF. Any ideas how I can transfer my graph to a PDF?

Cheers

A few years back, I've been beating my head against the wall trying to convert HTML into PDF for days. What I wanted to do was really simple - make an invoice for customers into a PDF file. An image (logo) up on top, a few paragraphs, and a table with a list of charges.

The hole shaped like my head on the wall is still there. All of the free libraries that convert things to PDF - they all suck. I found one that sucks the least, it's DomPDF. At least that one ended up doing the job, after a week of suffering and debugging. It's not fast by any means, though (if you want to generate a complex PDF, you might want to do it off-thread).

My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.

jQuery is interpreted by the browser and not by the server. When you send the HTML to be rendered into PDF, it will not run the Javascript. You'll need to find a way to actually generate the image some other way.

I guess I could see a situation where you could use ajax to make a remote call and send all of the html that the js sees.

The remote call then would write a file of that html. The remote call would send back a file name for the pdf to be generated.

Your js then could provide a link to the processing page of the html2pdf that references the file created from the remote call.

This would work, but it might be a bit much.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM