1

so I'm trying to get a HTML table with dynamic php echo's to print to PDF. However I have two different div's that I want to print in the same PDF.

I've tried multiple answers from similar questions on stack overflow including:

  • jspdf
  • tcpdf
  • Zend_pdf
  • Several PDF printers
  • FPDF

I've also Tried answers listed here:

The solution which works the best so far is the one given here:

The Problem that i'm facing is that I can generate a PDF if I only want a general HTML table, however it doesn't allow me to add in the php information, which is what I need for the PDF. Also if i try adding both div's, even without any PHP, then the report doesn't generate.

Any advice on where I can try start would be greatly appreciated. If you want to see code, I can provide a redacted version tomorrow, however for general advice I don't see why it would be necessary.

Community
  • 1
  • 1
Kyhle Ohlinger
  • 152
  • 1
  • 14
  • generating a pdf from html is very error prone. nobody really has a good solution. – developerwjk Aug 11 '15 at 20:10
  • Http://www.cloudformatter.com/css2pdf is jQuery plugin to remote cloud based formatting server. Since it scrapes the dom it would have all information in place but it is meant then to execute inside the browser. It can do multiple divs and tables. – Kevin Brown Aug 12 '15 at 14:45

2 Answers2

1

If you don't mind handling the generation of your PDF to a sub-process I would suggest that you investigate going with a webkit headless browser.

Either Phantom.js or wkthmltopdf will do exactly what you require with barely no trouble at all and the same css support as webkit. They also support javascript if that is of any help.

Both also have PHP wrapper to talk with your app. For Phantom h++p://github.com/kriansa/h2p , and for WK: h++p://github.com/mikehaertl/phpwkhtmltopdf, are two examples but many others are available.

I've used both with great success.

All PDF generator will require from you some kind of flat HTML structure to render, but you can overcome this by using output control function h++p://php.net/manual/en/ref.outcontrol.php, mainly ob_start(); and ob_get_clean(); to output all your PHP/HTML mix into a usable html variable.

Sorry for the weird links, the editor didn't allowed me more than 2 live links.

Malabar
  • 23
  • 5
  • +1 wkhtmltopdf is what I used. It gives great results. I installed it on the server and simply used PHP to send a bash command. Worked wonders. http://fedir.github.io/web/blog/2013/09/25/install-wkhtmltopdf-on-ubuntu/ – 9997 Aug 20 '15 at 19:35
0

Just to let you know, I ran into this very similar problem where any dynamic content created using PHP would not be converted to PDF. There is a very simple solution to it. Dynamically create an HTML page after your PHP content as loaded.

For example, use file_get_contents to scrap the page content, and then write it to an html file file_put_contents($content, "page-to-convert.html");

Once that is done, simply use the PDF converter on this HTML page.

Once converted, you can just delete that HTML page. It's a bit of a "hacky" process, but it worked well for me.

As for the HTML to PDF library, I used WKHTMLTOPDF. Here are some instructions on installing it on Ubuntu.

http://fedir.github.io/web/blog/2013/09/25/install-wkhtmltopdf-on-ubuntu/

Good luck!

9997
  • 811
  • 1
  • 8
  • 14