using PHP to create PDF, without using PHP's FDF functions - php

Is there any way to create a FDF file within PHP without using the dead FDF functions? I am aware that you can use pdftk to create a PDF from an existing FDF file, but I need to create the FDF file first with one field value set.
Or, is there a way to set one field in a PDF using PHP (skipping the FDF file creation) before allowing a user to download it?
For the heck of it, I tried creating an FDF file in PHP using fopen - did not work. I also know there is a generate_fdf operation within pdftk, but this does not allow me to set a specific value, at least not that I can see.
Does anybody know what an answer could be? I am pretty stuck. Unfortunately, I have no control over telling the client to scrap the full PDF (and instead use a webform).

With PDFTK you need two pieces to complete the puzzle. First you need FDF with the data, and secondly an original PDF with appropriately named form fields. PDFTK merges the data into the PDF.
To answer the FDF question first check out this excellent article, http://koivi.com/fill-pdf-form-fields/ that also includes a method to help generate the FDF.
Once you have the FDF with your data you can use PDFTK's fill_form function
You can keep the document editable by using passthru() instead of readfile($temp_file) and outputting to stdout instead of a file name by using the "output - " parameter. Also important is refraining from forcing a document disposition. If you try to force a file download rather than opening it in the browser you'll end up with an uneditable and unsaveable mess.
example:
header("Content-type:application/pdf");
passthru("pdftk ".$original." fill_form ".$fdf." output -");
die();
This will open it in the browser using the Reader plugin. You'll then be able to edit and print the filled form fields, but I still haven't figured out a way to save a copy with the changes. I'm currently chasing Adobe sales for other options.
As noted elsewhere, do not use PDFTK's option "flatten".
The original PDF must not include any passwords.

If I understand you correctly, you just don't want to use the FDF library in particular?
Have you tried TCPDF? See http://www.tcpdf.org/
If you want to use an existing PDF file as a template and only add some values, use the extending FPDI which extends TCPDF: http://www.setasign.de/products/pdf-php-solutions/fpdi/

Related

Approach to generate generate pdf file from html page?

I studied wkhtmltopdf, tcpdf mechanism to generate pdf files. wkhtmltopdf where you directly pass a .html file and it gives you the pdf where in tcpdf you need to code entire pdf.
my case is I'm having a pdf form template Which I've converted into html so user can fill that form and after i fill that template with user entered values then I'll give an option to user to download the html (user filled) file as PDF document, so template will have user entered data next to that labels.
so first
PDF template >> convert to .HTML page >> process with php echoing >> convert it back with user input to a PDF file.
I'm confused here which approach I should use.
Install wkhtmltopdf on server and use it to pass .html page
problem: Everytime I need to save .html page on server and pass again it to wkhtmltopdf.
using TCPDF I need to write lots of code to create pdf exactly same as template PDF docs I'm having
and then using php echoing those user enterted values.
Which approach should i use If I'm expecting 1000+ users will be saving page as pdf at same time, approach which will be more easier and scalable in future.
First of all - I think you should go with the HTML form to PDF approach, so that's either wkhtmltopdf or a tool that already does this for you like PDFmyFORM.
In case you're expecting to go to 1000 saves concurrently then you definitely want to roll your own solution instead of going with an external service though.
There are patches in the wkhtmltopdf issue list that suggest caching (see this one) and you may also want to think about whether all these forms have to be generated as PDF again. You could use APC cache to somehow cache PDFs based on the same values being filled in. That could save you a bunch of time.
Other solutions you may want to look into are for example PhantomJS, which is a headless webkit browser too, but then based on JS - so that may reduce your server load alltogether...

Submit HTML form to PDF

We have a high-resolution PDF (for printing) which has some form fields on it. We would like to have an HTML form which submits to the PDF, which is then placed into the respective fields.
I found a solution on google: http://koivi.com/fill-pdf-form-fields/
However, with that solution you only get an FDF file... And the demo does not work for me, opening the FDF file simply downloads another FDF file.
Since this PDF will be available to the public we would like to keep it as simple as possible. If we must open our original PDF and import this FDF file, we need a different solution (which I'm not sure is what the FDF file is for, since it didn't work).
A related post talking about .net framework had the same idea, but there were only paid commercial solutions: From HTML form to PDF
The PHP solutions I have found so far are for creating a new PDF, which is not what I need. Our PDF is created with Adobe Illustrator (or a similar adobe product) and is high-res with embedded fonts, svg and image content.
The form elements are in place, we just need to get the data to there.
Update April 11, 2013:
Since posting this question I have been utilizing FPDF on multiple projects where I needed to accomplish this goal. Although it cannot seem to "merge" template PDFs with the provided data, it can create the PDF from scratch.
One example I have used, I had a high resolution PNG for printing (similar to initial question) which we had to write the customer's name and today's date clearly in the center. I simply made the background of the PDF using FPDF->Image() and write the text afterwards using FPDF->Text().
It was very simple after all, you will need to look up the paper sizes to determine the X,Y,W,H of the image and then base your text fields relative to those numbers.
There was even a Form Filling extension, but I couldn't get it to work.
It seems as though I should answer my own question, although Visions answer may be better (seems to be deleted?). I used Vasiliy Faronov's link which was a comment to my main question: https://stackoverflow.com/a/1890835/200445
Here I found how to install pdftk and run a command to merge (flatten) my FDF and PDF files. I still used the "hacky" way to generate an FDF using Koivi's FDF Generator but it works for the most part.
One caveat is that some characters, like single and double quotes are not inserted correctly. It may be an issue of escaping the fields, but I could not find an answer.
Regardless, my PDF form generator is working, but anyone with a similar issue should look for a better solution.
There are number of tools which are not paid like itextsharp. try the following https://web.archive.org/web/20211020001747/https://www.4guysfromrolla.com/articles/030211-1.aspx Hope this code will help you. I have tried it its worked for me. If you can pay then there are number of paid tools which convert the HtML to PDF like ABCPDF etc.This example is in Asp.net and i am sure if you can convert it in PHP it will work for you too.

Populate editable PDF form fields with php

I've got an existing PDF form that my office has created that has form fields for the user to fill out (electronically) and print. Before passing it to the user I would like to open the existing PDF and populate as much (if not all) of that data using php. I've looked into extensions like TCPDF and FPDI, but I'm unable to confirm that what I want to do is even possible by looking at the examples and documentation. Have any of you done this before?
I did see TCPDF::setFormDefaultProp, which looked promising...
Pdftk has a fill_form command. Since it doesn't have PHP bindings you'll have to install it on your server and invoke it with exec() et al but it's pretty easy. To use it you'll have to generate an FDF file, which Pdftk will do given the generate_fdf command, then plug the desired data into it. You can find some information on FDF files here and some example PHP code here and here. In truth I think you could just put the generated FDF file in your PDF script and, given the right escaping, fill the values like you would in any string, then pipe it back into pdftk fill_form.
You can use a commercial product like SetPDF http://www.setasign.com/

how to extract the table from database and open it into .pdf file using PHP?

I am trying to extract the table from database and trying to open it in .pdf-file, but i am facing problem in opening to it in .pdf-file , when i am trying it prompt a message which is given below,
"Acrobat could not open "Report.pdf" because it is either not a
supported file type or because the file has been damaged (for example,
it was sent as an eamil attachment and wasnt correctly decoded).
To create an adobe PDF document,go to the source application. then
print the document to Adobe PDF."
Generating a pdf file in php can be difficult. http://www.fpdf.org/ seems to be recommended, but i have never used it. Otherwise, please post some code as a lot of the PHP pdf functions are depreciated http://php.net/manual/en/ref.pdf.php
You can use fpdf to create pdf files..
See http://www.fpdf.org/
See this for simple tutorial on dpdf http://www.elated.com/articles/create-nice-looking-pdfs-php-fpdf/

How to print text to an existing pdf file?

I have created new PDFs many times using Zend and PHP. But Now I have a patient form in PDF format and I have to fill that form using my application. How can I print text on existing PDF file with already have some text. Is it possible ?
Thanks
I haven't done it, but it certainly seems possible. See Zend_Pdf::load(), for example. It seems like you ought to be able to load the PDF, manipulate it, and save then save it somewhere.
Last time I had to do this, Zend_Pdf wasn't around, and I ended up using fpdf/fpdi, which was ugly but worked fine.
Does the PDF contain form fields ? If so you can use FDF type functions.
See Filling PDF Forms with PHP - there should be a solution in there somewhere.

Categories