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/
Related
I have a PDF form and i want to import this PDF Form in PHP and then populate all fields from database then create new PDF.
is that possible?
If yes then please guide me how can i inert data in already make PDF forms.
Here is the PDF form file which i want to Import.
Download PDF
If you do take into account your working time etc. you might actually consider commercial server-side form filling tool, such as FDFMerge by Appligent (for simple filling, the Lite version would be sufficient). In this case, you won't need to noodle around the PDF, trying to interpret it, and attempting to write something back.
In this scenario, you would export an FDF from the database, which is rather easy to accomplish, as FDF is a simple text file. How it has to look can be found with ease as well, by taking the form, filling it out, and exporting the data as FDF. And then, the rest is "filling out the blanks".
For using libraries to do server-side filling, links have already been provided. Just a note: make sure that you will get a good and functional PDF, by relying on well-reputated libraries.
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.
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/
We currently use PDForm to grab a blank pdf file (no values, just form fields and text) and list the form fields. We then query our database for the values which match those field names and create a pdf file with the newly populated data which the user can download from our site. The thing is PDForm is about $5,000 per machine and we are migrating servers. We want an alternative which is actively supported and recommended by the community.
I know Zend is working on a PDF manipulation extension, but we need something quick. I have done testing with PDFtk but the last update for that project was in 2006 and it now seems dead. It would be fine as it is open source, however it seems to be causing errors with certain files that seem to be generated with PDFPenPro (our pdf form creator).
Another solution I thought up was why not just use iText and write a java wrapper which accepts command line input, so that PHP can call it with passthru() or exec(). There are other applications that will work should we completely rewrite our code but we do not want to do that.
What we need.
The ability for PHP to receive the PDF form field names.
PHP to then either create and FDF file (then merge it with the PDF) or send a string to a command line application which will populate the fields with values from our database.
The user can then download the newly created PDF file with the populated form fields.
Am I moving in the write direction by creating a java command line application that will use iText to parse and create the PDF files specified by PHP or does anyone know of any cost effective alternatives?
TCPDF seems to have the most robust feature set that I have seen so far.
Thanks, d2burke, for the tip on TCPDF. I'm not trying to do quite as much as the OP, but the software packages available to accomplish any kind of pdf generation are in the $2k to $3k range. TCPDF is php based, open source and the guy developing it is very supportive.
Always donate to these guys! Where in the world would web development be without it?
So since none of the above solutions would work since TCPDF doesn't work with forms the way we are wanting and since PDFlib converts the form fields to blocks we decided to create a command line wrapper for iText which will grab the form field names from the PDF and then populate them based on the database values.
I don't know if another product whose license costs range from $1k -> $3k could be considered "cost effective", but PDFlib work quite nicely. And if you don't need the PPS functionality, it does get cheaper.
First of all my apologies to all the people who think this question is a repeated one or they find a similar question to this.
I am working on a project in which I have an online form and some PDFs stored on the server.
Functionality
On the submit action I have to get the data from the form, fill it to the copy of PDF and finally download it.
Approach
I followed these steps to achieve this functionality:
Converted the pdfs to html with this http://www.pdfdownload.org/free-pdf-to-html.aspx online tool.
Embedded the html with form variables and regenerated the PDFs with this library / dompdf library.
Problem
The approach is a brute force one as the html generated are far away from the real ones. So lot of effort is wasted in adjusting the html.
The process is so slow and not reliable as most of the time I get memory error or some other issues.
I need to to automate this process. What I have found through searching is I should create an FDF file that contains my variable and pass it to the PDF using some library and then download it.
I am able to create the FDF file but missing any library in PHP (I found one in JAVA) that I can use to create the PDF and download it. One library that I found is pdf tool kit but that is a command line tool and I am not able to use it on the server at run time and download the PDF file.
Anybody having done this before please help.
(Sorry for this long post)
Thanks,
Madhup
Check out FPDI. It allows you to load some existing PDF, draw on it programatically, and output a new PDF. Which, if I read your question right, is what you're trying to do.
There's some example code here.