I'm developing an app where the user adds items to a list. That list is stored in an array and passed to PHP with JSON.
The objective is to then create a PDF with all the values extracted from the user. The PDF is quite complicated. It includes images depending on what the user selects and the text varies depending on the images and the input data.
The first idea was to generate the pdf in php with one of those pdf libraries, but that's going to be a real hassle.
Then I thought of creating an html & css (much easier) and the convert it to PDF. But since the html & css are quite complex I don't think those pdf converters will work with this.
Then I thought I could convert the html to jpg and then to pdf.
It'll be much simpler if I could just use html but the output needs to be pdf.
What do you suggest?
Here's a post that discusses creating PDF files with PHP and the PDFLib extension.
Generate PDFs with PHP it's on sitepoint.
Or if you want to go from HTML to the PDF it looks like TCPDF might work.
You can try using FPDF
Then I thought of creating an html & css (much easier) and the convert it to PDF. But since the html & css are quite complex I don't think those pdf converters will work with this.
wkhtmltopdf to the rescue! If you are on a VPS or dedicated machine, it's probably the best (open source) HTML-to-PDF engine out there. It leverages Webkit, the rendering engine used by Google Chrome and Apple Safari, amongst others.
Otherwise, your only other options are going to involve drawing every aspect of the PDF or image yourself, "by hand" in your code.
Related
we are developing a website that needs to convert PDF files into HTML because some of the PDF has a form (not necessarily fillable PDF, these PDFs are printed to be filled up).
So we want it to be filled up through our website instead of printing the files and filled up by pen. We are going paperless.
DocuSign provides these wherein you can upload PDF, then you can customized it to have textboxes, checkbox. So we're kinda using DocuSign as a reference but still haven't figured out how they did it (Almost perfect convertion of PDF to HTML vice-versa).
So far I've tried several 3rd party softwares for converting PDF to HTML. I've tried XPDF, Poppler, & ImageMagick.
ImageMagick converts a PDF to an image which is not suitable as these images has a large size when converted back to a PDF for printing.
Poppler is a fork XPDF based on my research, I've tried it after using XPDF to see if it's better, it basically does what XPDF do but it converts the PDF to have bigger pixels on the CSS when converted to HTML. That's fine but it loses the font family.
XPDF converts PDF to HTML but the pixel is smaller, so when I convert it back to PDF, it does not fit the whole page, and I still have to manually adjust all the CSS to fit it.
So after using these 3rd party softwares, I convert back the HTML files into PDF using MPDF, and the converted files has so much inconsistencies. Texts are not aligned properly. It's basically not the same as the original PDF.
Any help will be appreciated thanks!
What you are trying to do is not as straight forward it may seem. I have worked with Adobe Sign, formerly known as EchoSign, for years and I have a pretty good idea on how these services work. With that been said I strongly suggest looking into one of these eSign services instead of trying to roll out your own. It will save you a lot of time.
This is how it all works
The PDF must have a form itself with named fields. In other words, if you open such PDF in Adobe Reader or Chrome you should be able to fill in the fields. If your PDF does not have a PDF form you will need additional software like Acrobat PRO to create the form.
You must convert the PDF into a flat image that can be rendered in the browser.
You will need a tool to extract the PDF Form information, such as the field names, types, dimensions, and coordinates.
With all this information you can then render the PDF image(s) in the browser. Place absolute positioned HTML form elements over the image using the field type, dimensions, and coordinates from the previous step. Each HTML element needs to reference a PDF form field by name.
Once you have collected the information and a data map like field_name => field_value from your HTML widget, you will need to use additional software to programmatically fill in the PDF form in the original PDF. A PDF form information is often stored in FDF or XFDF file.
I don't know of a single tool that will help you with the things outlined above, at least not in PHP. However, I can provide you with a suggestion can be helpful:
PDFtk Server - Can help you to both, extract the PDF form fields information and fill in the same an XFDF file. Unforutently, the form field information that you can extract with such tool does not include dimensions and coordinates.
iText - A library available in .Net and Java that can be used to extract detailed information about the PDF form including the dimension and coordinates of the fields. You can create microservice using this toolkit that can communicate with PHP.
There are definitely a lot more tools out there for the job. Hopefully, this information will guide you in the right direction or help you make a decision on how to move forward with your project.
I am working with a tool which lets user upload a .csv file.
That csv file contains an address column. I have to use the address from each row in another HTML template. That HTML template is like this
. After creating that template I then need to convert it into a PDF, store the PDF on a file server and give the user a link to the PDF.
I've finished the first two steps - csv upload and created complete template with address, but I'm stuck on how I can convert a template into a PDF.
I have looked into a few php-pdf libraries like fpdf mpdf. I'm facing a problem in creating pdf with html template.
A link to a library wich convert HTML to PDF and works pretty well.
First the link to the library
HTML2PDF
Then some code* to create your PDF using your own generated HTML, where $content is your HTML string.
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
*Code taken from the "example page" of the site.
I have used tcpdf in many cases, https://tcpdf.org/
Works well with tables, I have made receipts and accounting related stuff with it. Handle UTF-8 without problems, why it's my way to go.
Only downside is that code is bit long and complicated and it doesn't keep tables as tables in pdf and turns them to divs, so paddings and other styles might be bit trickier to do.
One way is to use webkit based HTML to PDF converter.
Pros are that it is easy to customize and style and to see in the browser how it will look and then you can be sure that it will look as same in PDF as well. You could use CSS and JavaScript as well to style and modify.
Cons are that it is hard to install it on the production server sometimes. But there are web services and APIs that get you covered.
For example one service is https://pdfapi.io. It is free to use. Only when your amounts get bigger, then it will charge like a cup of coffee.
Hope that helps.
My current application requires me to convert a document (doc, excel, ppt, image, etc) to PDF. It also requires to add a front page with several information (variable) and add footer to converted document. It may also require to add a text or a logo image as an watermark.
I have been able to convert document to PDF using LibreOffice. I generated a static pdf and I could merge it with converted document using PDFtk
But I need to generate front page dynamically because for each document, some information will change. I thought of using FPDF to generate front page, convert with libreoffice and merge it using PDFtk. But again, it will require adding footer and possibly a watermark. I think watermark can be done by PDFtk. And footer can be generated by FPDF.
So whole process will look like this:
Created Front page with FPDF and save pdf
Convert Doc to PDF with LibreOffice
Add footer to converted PDF and store with FPDF
Generate PDF with watermark using FPDF (possibly)
Combine above three pdf using PDFtk
I guess I am doing too much of processing. Is there a simpler way to achieve this? Are there any alternatives to achieve this with lesser resources / third party apps/lib ?
If you're open to commercially licenced software, PDFLib+PDI will handle all the tasks you require (after conversion to PDF). It runs natively on most server environments with generally very good performance.
The Library I have used to handle PDFs with PHP is TCPDF. It's open source, you can use HTML to style the PDF (a subset of HTML anyway), handle things like pages. I believe you should be able to perform the functions you require all in TCPDF.
To handle manipulations to images, which you may need for watermarking etc, I used GD which provides numerous functions for manipulating images. If you require some more complex image manipulations, you may also want to look at imagemagick.
All of this should simplify your workflow, and allow you to do most of what you require directly in PHP and HTML.
To decrease the number of different things that I need to do, I will simply use the FPDI library.
So what I am going to now is,
Convert the document into PDF format
Create Front Page using FPDI (which extends TCPDF and uses it's function)
Then I will import the converted document using FPDI ' s import feature
I will also add watermark and footer same time
So I will ultimately eliminate the use of PDFtk and reduce the number of libraries used and different activities required to be done separately.
These thing will be done in two activities only :
Convert to PDF with LibreOffice and
Add a front page, import and add footer and watermark with FPDI
I haven't tested it yet, but DomPDF looks like a nice project.
I was using HTML2PDF before, but it's not updated anymore so moving on.
If you're using such libraries, just make HTML templates (moreover, DomPDF supports PHP execution inside html templates) and have fun ;)
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.
I want to generate PDF from a PHP file that includes HTML controls like textbox, and textarea. I attached CSS in the same. I tried FPDF, DOMPDF and TCPDF, but still I don't get exactly what I want. How do I pass HTML controls with PHP variables and CSS to these libraries?
mpdf is another option that you could try.
EDIT :
Found another solution for it, TCPDF is a FLOSS PHP class for generating PDF documents. Looks more dominating library.
"PRINCEXML" is a good library (not completely free now).
Others:
If your meaning is to create a PDF file from PHP, pdflib will help you (as some other suggested).
Else, if you want to convert an HTML page in PDF via PHP, you'll find
a little trouble outta here.. For three years I have been trying to do it as best as I
can.
So, the options I know are:
HTML2PS: same of DOMPDF, but this one convert first in .ps
(Ghostscript), then, in whatever format you need (PDF, JPEG, PNG). For
me it is a little better than dompdf, but I have the same speed problem.. Oh,
it has better compatibility with CSS.
Those two are PHP classes, but if you can install some software on the
server, and access it through passthru() or system(), have a look at
these too:
wkhtmltopdf: based on webkit (safari's wrapper), is really fast and
powerful... It seem like it is the best one (atm) for converting HTML pages to PDF on the fly, taking only two seconds for a three pages XHTML document
with CSS 2. It is a recent project. Anyway, the Google Code page is often
updated.
htmldoc: this one is a tank, it really never stops orcrashes... The project
seems to have died in 2007, but anyway if you don't need CSS compatibility
this can be nice for you.
** Thumbs Up For Strae.
If I understand your needs correctly I don't think any PHP-PDF class would do that.
Mostly you could insert only text and images to a PDF file, so if you would want something that looks like an HTML element you would need to insert it as an image.
Usually just putting HTML doesn't mean all your elements would stay intact in the PDF . (Different world, after all)
http://www.fpdf.org/ is the site having a great HTML-to-PDF class which work well. I am using it, but you have to first study its functionality and then start.