PHP convertor Html(css) to Pdf? - php

Duplicate: Convert HTML + CSS to PDF with PHP?
I am looking php based library for HTML->PDF convertor, for report generation in my application. By far I found one, and it seems good, but has some limitations.
Can anyone suggest any other advanced ones?
Thanks.

Your question may be answered by this previously-posted question. However, you may also want to take a look at HTML2PDF, or any of the variety of PHP PDF libraries that can be found with a quick Google search.

A while ago I used html2ps and then ghostscript to convert html pages to pdf, because it gave better results, then all html2pdf libraries.
But the may have changed in the time. The best is to try some libraries, because the quality of the generated pdf's also depends on the style of html you are using as base.

If using FPDF there is a script which extends that class to do some basic HTML formatting (not CSS though). Available here: http://www.fpdf.org/en/script/script41.php

Related

PHP PDF conversion

I am using PHP 5.0 pdf library to convert files dynamically into PDF.
I am using this reference from the official PHP website using PDF_new(), creating its object and using PDF_set_info and PDF_get_buffer functions.
This works fine, but when I want to create PDF pages and writing the content inside of it, there is no reference given anywhere on how to convert an already existing page to PDF. Say a page in my folder bill.php with CSS too needs to be converted to PDF on the fly.
Well converting HTML to PDF is not that easy, there are several libraries out there that might fit your needs. But none has full html/css capabilities, especially not css3.
FPDF
http://www.fpdf.org
TCPDF
http://www.tcpdf.org
DOMPDF
http://code.google.com/p/dompdf/ (this class allows you to easily convert simple layouted websites to pdf. i used this class quite often with almost no problems. sadly this library does not support converting of forms to usable input fields.)
WKHTMLTOPDF
http://code.google.com/p/wkhtmltopdf/ (actually a linux package but php wrappers are existing. this gives almost full html capabilities and awesome results)
html2PDF is a nice library to use. Make sure you change the default language to English though. I've used it many times to create dynamic invoices with tables and divs. Works really well.
Refer some of the articles, which may help to improve your knowledge as well as clear some of doubt of you.
Getting started
Convert HTML To PDF in PHP The Easy Way
How to Generate a PDF With PHP
Convert HTML to PDF
This HTML to PDF SDK may also help you.
Hope, it will help you.

PHP PDF template library with PDF output? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Is there any PHP PDF library that can replace placeholder variables in an existing PDF, ODT or DOCX document, and generate a PDF file as the end result, without screwing up the layout?
Requirements:
Needs no 3rd party web service
Ability to run on shared web hosting would be ideal (no binary installations / packages required)
Mind you, a library that is able to load an existing PDF file and insert text programmatically at a specific position is not enough for my use case.
As far as my research shows, there is no library that can do this:
TCPDF can only generate documents from scratch
FPDI can read existing PDF templates, but can only add contents programmatically (no template variable replacement)
There are various DOCX/ODT template libraries out there but they don't output PDF
PHPDOCx claims to be able to do exactly what I need - but they don't offer a trial version and I'm not going to buy a cat in a bag, especially not when there seems to be no other product on the web that does this. I find it hard to believe they can do this without problems - if you have successfully done this using the product, please drop a line here.
Am I overlooking something?
Is there a way to do this using PDF forms? I am creating the source documents in OpenOffice 3.
I may be able to use standard Linux commands (pdftk is available for example, trying that out right now.)
Update: *Argh!* I was called out of the office and the bounty expired in the meantime. Starting a new bounty: As far as my testing shows, no solution works for me perfectly yet.
Update II: I will be looking the pdftk approach soon, but I am also starting another bounty for one more round of collecting additional input. This question has now seen 1300 rep points in bounties, must be some kind of a record :)
This is not very practical, but for completeness: If you already have an ODT template, then you might very well retain that as template. Modifying the OpenDocument content.xml and replacing placeholders therein is pretty simple. If so, you could use unoconv or pyodconverter to transform the ODT into a final PDF.
unoconv -f pdf -o final.pdf template.odt
Very obviously this requires a full OpenOffice setup (UNO and Writer) on the webserver. And obviously not every webhoster would go with that! haha. Even if it's simple on any Debian or Fedora setup. The execution speed would probably not be stellar either. But then it might be the cleanest approach, since OOo governs both formats way better than any PHP class ever could.
Pekka,
I looked in to this previously, I think you can use pdftk (a command line utility), to fill in a PDF form using FDF/XFDF data files, which you could easily generate from within PHP. That was the best option I've seen so far, though there may well be a native library.
pdftk is quite useful in general, worth having a look at.
Update: Have a look here: http://php.net/manual/en/book.fdf.php
Have you considered using something like XSL:Formatting Objects (XSL:FO)? Basically they're XML documents that are processed and turned into PDFs. Doing string - or better, DOM - replacements within that should be pretty simple. It supports embedding images, links, annotations, etc.
It's not PHP but there are a number of PHP wrappers for it along with ways of using it via exec, etc. Not an ideal but it takes care of the template portion completely. For some more info: http://techportal.inviqa.com/2009/12/16/transforming-xml-with-php-and-xsl/
There's an implementation available as an Apache project - http://xmlgraphics.apache.org/fop/
fpdf and there is another extention on top of it, which I can't remember, which allows you to import templates
Your best bet would be to generate the entire document on the fly, with the template defined programatically using fpdf or something similar. That way, your text will not be cut off by paragraphs or anything like that, and you can easily position images/other elements as required.
Late, but you can use OpenSource template designer https://github.com/applicius/dhek/releases , to define pkaceholders/areas over any existing PDF, then load it in PHP (as it's JSON format) and write accordingly on original PDF using fpdf lib, to generate custom PDF with dynamic data written on.
Altough not exactly thing you asked, you may consider to make it at two steps: using some php templating sytem (smarty, dwoo) to generate html page and then using tools like Html2Pdf convert it to pdf. I am using it, and results are good (no problems with page layout etc)
Of course it depends of your input documents (can you use html instead of PDF/ ODT as source ) and complexity of the layout of those.
Ok I'm trying to help you solve the problem a little.
First the answer for couple of your question.
Q - Am I overlooking something?
A - No. There is a PHP PDF library that can replace placeholder variables in an existing PDF and generate a PDF file as the end result, without screwing up the layout
Q - Is there a way to do this using PDF forms?
A - Yes. absolutelly the tric to doing this is by using a PDF Forms
For both answer you can use Justin Koivisto fill pdf form field php library.
For more detail you please go to http://koivi.com/fill-pdf-form-fields/tutorial.php.
Take a look there for additional information.
Credit to Justin Koivisto for his work
P.S
For workaround for displaying a table like output from pdf form
please consider to take some reading on Oracle Business Intelligence Publisher User's Guide - Creating a PDF Template
I'll add this new answer since the FDF PHP extension is now dead.
I've just followed these instructions and ended up executing one perl script then the pdftk command
I'm pretty aware it's far from being a real PHP solution but it's reliable and fairly easy to implement on any *nix platform.
The tools described there are also available on Debian, just in case you were wondering.
It's a litte bit late but have a look at the PDFTemplate Library it does exatly what you want. You can create Open Document files (odt) and add placeholders in it. The PDFTemplate library can fill out these placeholders (even with images) and create a PDF file.
ODT Files with placeholders to PDF

PHP HTML to PDF free convertor Resources

What is the best PHP HTML to PDF free converter around, not just in terms of functionality but also in terms of resource usage and speed
Thanks
Have a look at open-souce fpdf library.
Check dompdf, an HTML to PDF converter written in PHP. No external dependencies, it supports complex tables, images and even external style sheets.
http://www.digitaljunkies.ca/dompdf/
If you want to be really clever about it, you could programmatically create a new Google doc containing your HTML and CSS, then programmatically export it as a PDF. No resource usage on your part, and it works very well.
Start here:
http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
We recently used this on a project with quite a bit of luck. I don't know that it will go straight from HTML to PDF like you are looking for, but it is a good set of tools.

How to generate PDF files with PHP? [duplicate]

This question already has answers here:
Closed 14 years ago.
I have some HTML that includes bolding, italics, small tag, big tag, blockquote tag, bullets, and numbered items. I need to convert it to PDF via PHP. I tried FPDF, but it was entirely too complex. I tried the html2pdf PHP library, which uses FPDF, but it was very buggy and unreliable. What do you recommend?
Note: Imagine your typical resume. I'm needing to format something like that.
Take a look at FPDF
I've had a lot of success with dompdf.
Make sure you have valid HTML though, or it can get into a loop.
It's very simple to use - about 4 lines to convert an HTML file to a PDF.
I have had good experiences with PrinceXML. Their rendering engine is very good. Passes acid2 and has good .svg support so you can include vector images instead of bitmaps to keep the .PDF relatively small (which is important when generating reports which need to be sent as an attachment).
(note that princexml is proprietary software).
http://www.ros.co.nz/pdf/
I once used the above to turn incredibly complex architect's tile specifications into PDFs on a dynamic basis and it worked out well. I found the class very approachable and easily modifiable.

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