Dynamic PHP webpage save to PDF [duplicate] - php

This question already has answers here:
How Can I add HTML And CSS Into PDF [closed]
(30 answers)
Closed 9 years ago.
I am creating an invoicing application that generates a dynamic invoice via php/mysql in the browser windows. Tables and css are part of the makeup.
I was thinking it would be nice to have a button that when clicked, a popup would appear asking you to save a pdf to your computer.
Can someone steer me in the right direction?

These might be of help:
fpdf - PHP class-based generator
wkhtmltopdf - Convert html to pdf using webkit (qtwebkit)
mPDF - PHP class which generates PDF files from UTF-8 encoded HTML
TCPDF - an enhanced and maintained version of fpdf
dompdf - (mostly) CSS 2.1 compliant HTML layout and rendering engine
Zend_Pdf - A PDF manipulation engine/component from Zend
PrinceXML - Commercial, but supports CSS3 transforms
It's partly dependent on how complex the HTML is and what you want to display. Some of them can be a bit flaky when rendering certain style-based elements (especially CSS2/CSS3), but in general they are pretty good.
If you're planning to "force-download" your PDFs, then you'll need to use something like the following in PHP:
header('Content-disposition: attachment; filename=filename.pdf');
A good example/explanation can be found in a previous SO answer: php: force download to harddrive?

Related

Create PDF from current HTML page [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert HTML + CSS to PDF with PHP?
I'm on a basic Apache with no extensions apart from default enabled and I want to create a PDF file from the current page.
The current page is PHP echoing out some variables but I want to create a PDF document from the HTML and text currently shown on the page.
Something like WHMCS quote system.
You could use a third party solution such as:
http://pdfcrowd.com/
http://www.web2pdfconvert.com/
http://html-pdf-converter.com/
I'd recommend pdfcrowd.com - it has an API that you can use and i've used the service before and not had any problems.
Look into FPDF, TCPDF, DomPDF, or any other PHP PDF library.
FPDF is very lightweight, and could be perfect for you if you are just echoing out variables.
TCPDF is much more HTML->PDF in terms of syles and css features, but the file size is a bit bigger. It could be used to easily build invoices and make them look nice.
I haven't used DomPDF personally, so I won't comment on it, but it can get the job done.
Take a look at FPDF.
It is a free libary for generating .pdf documents within php.
I've used TCPDF to create a PDF from HTML. I think DOMPDF also does that
you have a few options:
http://php.net/manual/en/book.pdf.php
FPDF: http://www.fpdf.org/
TCPDF: http://www.tcpdf.org/
DOMPDF: http://code.google.com/p/dompdf/
BTW: Convert HTML + CSS to PDF with PHP?
You can also check dompdf . It creates pdf FROM HTML ! it could be more appropriate for your request but I personally prefer FPDF (CSS make me sometimes crazy with domdf :-) )
The only pure PHP solution I've tested is mPDF and it worked pretty well for my purpose. It takes HTML/CSS input at generates a PDF from that.
It requires a lot of memory so you'll have to increase the memory limit for the script. The settings that worked for me were:
ini_set('memory_limit', '1024M');
ini_set('pcre.backtrack_limit', 2000000);

php pdf generator that handles multiple layers (text on an image etc)

I have worked on generating PDF about 3 years ago and used FPDF lib for this purpose. I remember it wasn't great as it was very limiting. I haven't got a chance to work on any other PDF related programming since.
I reckon many libraries came up to light during last few years. What is the best pdf generating lib for php that you can recommend nowadays? I would need to be able to include images or even 2 layers (image + text on it) so the lib should allow to do that easily
thanks
Why should not you try to prepare a html of your choice and convert it to pdf using htmltopdf generator. wkhtmltopdf is one among the best. Try to create a html template where you can pass your data and image space. Pass data to that html template and convert that html into pdf.

Export a html into PDF in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert HTML + CSS to PDF with PHP?
What's the fastest, cleanest and best way to export from PHP a webpage (given URL) into PDF?
Hard. You wont be able to convert HTML to PDF just like that without any manipulation. One shot may be using mPDF, but that probabliy won't satisfy your needs, since it has limited HTML functionality.
The other way would be making a screenshot of a webpage and including it in PDF document, but you'll need specialized software, or at least libray. Read further on that topic.
You can go for TCPDF.
dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) CSS 2.1 compliant HTML layout and rendering engine written in PHP domPdf

Converting HTML to PDF using PHP? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert HTML + CSS to PDF with PHP?
Is it possible to convert a HTML page to PDF using PHP, and if so, how can it be done?
Specifically, the page is an invoice generated dynamically. So I would like it loaded using:
http://example.com/invoices/3333
And the HTML output would have to be converted to PDF.
Any good libraries that do this will be fine.
If you wish to create a pdf from php, pdflib will help you (as some others suggested).
Else, if you want to convert an HTML page to PDF via PHP, you'll find a little trouble outta here.. For 3 years I've been trying to do it as best as I can.
So, the options I know are:
DOMPDF : php class that wraps the html and builds the pdf. Works good, customizable (if you know php), based on pdflib, if I remember right it takes even some CSS. Bad news: slow when the html is big or complex.
HTML2PS: same as DOMPDF, but this one converts first to a .ps (ghostscript) file, then, to whatever format you need (pdf, jpg, png). For me is little better than dompdf, but has the same speed problem.. but, better compatibility with CSS.
Those two are php classes, but if you can install some software on the server, and access it throught passthru() or system(), give a look to these too:
wkhtmltopdf: based on webkit (safari's wrapper), is really fast and powerful.. seems like this is the best one (atm) for converting html pages to pdf on the fly; taking only 2 seconds for a 3 page xHTML document with CSS2. It is a recent project, anyway, the google.code page is often updated.
htmldoc : This one is a tank, it never really stops/crashes.. the project looks dead since 2007, but anyway if you don't need CSS compatibility this can be nice for you.

Using PHP to create a PDF from a mix of plain text and HTML text [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert HTML + CSS to PDF with PHP?
I have a form with 5 fields, two of which are textboxes extended with tinyMCE, the rest are simple inputs of type text.
I need to generate a PDF from this input. I understand that I can use Zend_Pdf to generate the PDF and include the plain text data. But how, for example, can I include a bulleted list from the tinyMCE fields?
Would the best way be to create an HTML file, and then use for example DOMPDF or HTML2PDF? Ideally, I'd prefer to just use the zend framework to create the document, position and insert the fields, and save.
Thanks in advance.
More info in Convert HTML + CSS to PDF with PHP?.
In my experience, Prince XML was the Rolls Royce of such technologies so far away and above any of the other ones it's not even funny. It's expensive though. But I had all sorts of problems with all the others.
Some time ago I tried to use HTML to PDF conversion programs to convert... HTML to PDF, but in the end I gave up with that approach and just created the PDFs directly in code. I use fpdf (http://www.fpdf.org/) as a base and added supporting code for lists and grids etc.
I am using Prince XML mentioned by cletus. Results are very good, even with css styled html with floats etc. It's expensive, but it just works and saves a lots of time.
FPDF is very old library for PHP4. It propably won't even work nowadays. I'd recommend DOMPDF or TCPDF. They both are for PHP5+ and can eat HTML or CSS to some degree.
You could convert it all to HTML and then use openoffice or some other tool (pandoc is quite nifty too) to convert from HTML to PDF.
Alternatively, you could take a look at LiveDocx, which has php-bindings too. It's a hosted service, but you can use it without charge.
I personal recommend command line application instead of any php libraries.
Reasons :
PHP libraries need more time and memory (cache) for conversion process
They need well formatted html pages only, otherwise through errors or warning
Not support for external style sheet.
Command Line Tool:
If run your script on Linux server then I suggest command line tool.
Reasons :
They are extremely fast as compared to PHP libraries.
Support css.
Accept non well formatted html.
Which command line tool to use?
wkhtmltopdf
htmltopdf
html2pdf
for more information refer Converting HTML to PDF (not PDF to HTML) using PHP

Categories