Populating a LiveCycle PDF with PHP and MySQL

This tutorial is officially defunct. It is only here for archival purposes. The main script has been consolidated into one PHP class—pdftk-php. Please see the updated tutorial.

I work in the Harold B. Lee Library Multimedia Lab where we check out digital video and still cameras, tripods, external hard drives, digital voice recorders, and let people use $8,000 Quad Core Intel Macs. Expensive stuff…

To insure a “you break it, you pay for it” system, we require all patrons to fill out a loan agreement that we then keep on file. We’ve been using this system for several years and now have more than a thousand forms—all completely unorganized and out of date. We have no way of knowing if a patron has graduated. We have no way of seeing if a patron has filled out a form previously.

So, I volunteered to fix the problem and move the entire loan agreement system to an online database. I had dabbled in LiveCycle and PHP but had never touched MySQL. So I decided to figure it all out.

I’m assuming you already have a server set up with PHP and MySQL. If not, you can download WAMP or MAMP and set up a local server on your computer for testing.

I’m also assuming you have some knowledge of HTML and PHP. If not, search for some PHP tutorials on Google and get a foundation there.

Adobe LiveCycle

Adobe’s LiveCycle PDF Form software works great creating fillable PDF forms and then gathering the form data electronically. Collecting the data is relatively easy since LiveCycle uses semi-open file formats for data storage. For example, if you have an e-mail submit button in your LiveCycle form, a specially formatted XML file will be e-mailed to whatever address you set in the button properties. LiveCycle can then input that XML and repopulate an empty form.

Adobe even sells the LiveCycle Enterprise Suite, which is basically a specialized server made for generating and repopulating PDFs from submitted data. The Enterprise Suite is extremely expensive though

Create form in LiveCycle

Unfortunately LiveCycle Designer does not work like the rest of the Adobe CS3 products. I spend most of my time in InDesign, and from a typographic point of view, Designer is pathetic and a little difficult to work with.

Creating the form is relatively straightforward, regardless of the limitations. Drag text boxes and image boxes from the Library panel to add static text and images. Drag text input boxes from the Library to make fillable fields. Actually laying out and designing the form is not the scope of this tutorial, so I won’t go any further with that.

Data Bindings

What concerns us most is data submission and population with form fields, so we need to set up our fields to work. If you click on a text field, you should have three tabs in the Object panel (if you don’t have an Object panel, go to Window > Object).

The Field tab lets you set some basic properties for the field, like whether or not the field can have multiple lines, the line length, the display pattern for phone numbers or other number patterns, the caption, and a plethora of other things. The Value tab allows you to do some scripting for validation of your fields. I tried getting this to work, but since I’m not a programmer at all and don’t really know Javascript, I gave up.

The Binding tab is the most important for our purposes. By default your field will have a basic name like TextField1. You should change the names of all your fields to a more canonical naming system, like FirstName, LastName, EMail, etc. You can also change the data patterns and formats for text, XHTML, or dates.

Submit Through HTTP

For my purposes, I wanted patrons to be able to fill out this PDF from anywhere and then submit it online, regardless of e-mail accounts. To best do this, drag an HTTP Submit Button from the Standard section of your Library. For this button to do anything, you need to set a URL that will receive the data in the form of HTTP POST.

Begin Setting Up Your Web Application

I made several different PHP files in the process of getting this all to work. First I wanted to verify what the HTTP POST values were before I started trying to process them. I adapted this code from Steve Tibbett, an actual Adobe guy.

Download text for dump.php

Create a file called dump.php and paste this code into it (or remove the .txt extension). Set your HTTP Submit Button URL to dump.php (in my case it was http://localhost/PDFStuff/dump.php) and preview your PDF in Designer. Submit your data and you’ll see all the variables and the raw post data. This step is only to verify that the HTTP POST variables actually match up with your Designer field names.

Now that we know that the HTTP POST variables are actually working, we need to save them to a database. You’ll need to first set up a MySQL table or database. Since I had never done this, I followed some tutorials here, which were extremely helpful in understanding how to actually use MySQL.

Use PHPmyadmin and create a new PDFStuff database with a username and password and then create a table in it with the following command (or use the GUI form in PHPmyadmin to create the table—either way works):

CREATE TABLE `PDF_Loans` (
`id` int(4) NOT NULL auto_increment primary key,
`FirstName` varchar(65) NOT NULL default '',
`LastName` varchar(65) NOT NULL default '',
`EMail` varchar(65) NOT NULL default '',
) TYPE=MyISAM AUTO_INCREMENT=0 ;

You now have a table in your database where we can store our PDF form variables. Paste this code into a file called insert.php and change the PHP variables as necessary, both for your MySQL connection information and your HTTP POST variables (here I just use FirstName, LastName, and Email).

Download text for insert.php

Change the URL of the HTTP POST button to insert.php (again, in my case it’s http://localhost/PDFStuff/insert.php) and try submitting some data with your PDF form. It should work.

Viewing Data in the Database

To actually see your submitted data, create a new php file called view.php and paste this code in, changing it as necessary.

Download text for view.php

This page will take all the data from your database and display it in a table. You should see one record—the one you just added. There is also a column for a link to view the PDF, although the link is blank for now. Add several more through LiveCycle to make sure it’s working.

PDF Madness

If you’ve already had PHP/MySQL experience, all of that was easy. Now comes the tricky part—repopulating the PDF form from the MySQL.

To get this to work, we need to convert the MySQL data into an FDF file, or the Adobe file format for storing form data. We then need to infuse the FDF file into our empty PDF form and allow the user to download it.

Fortunately, someone else figured out the bulk of this, at www.pdfhacks.com. Download pdftk and forge_fdf.php place them in your main site directory. Forge_fdf.php will take your data and transform it into an FDF file while fdftk will insert that FDF into your PDF. All you need to do is add some variables.

Before creating your variables, you need to discover the real names for all of your fields. If you made your PDF in Acrobat, the field names should be identical, but if you used Designer, the official code-based names will be much longer. Fdftk can discover those names for you and dump them in a text file.

Place your empty PDF form in your main site folder. Open up a command prompt or terminal and run this command in the site folder, changing file names as necessary:

 $ pdftk form.pdf dump_data_fields > form.pdf.fields

Open up the newly created form.pdf.fields file in Notepad and you’ll see the automatic fdftk output, which will look something like this:

FieldType: Text
FieldName: form1[0].#subform[0].#area[0].FirstName[0]
FieldNameAlt: First Name:
FieldFlags: 2
FieldJustification: Left

The FieldName in this case is long and hairy, but we’ll need that full name for the data insertion to work.

Dynamic Data Insertion

Now we’re ready to put all the pieces together. Make a file called viewpdf.php and paste this code in, changing as necessary:

Download text for viewpdf.php

If you don’t want the form flattened (i.e. you want to maintain the form fields), take out the – flatten command.

Notice how the long names had to go in to the $fdf_data_strings array.

To populate the PDF from your view.php table, you need to pass those long field names into viewpdf.php. Technically you would need to change the links in view.php to include the row id number for each row, so the PDF is generated using only the information from that row. Fortunately, we can have PHP and MySQL write all those links dynamically.

We originally set up the database so that every time you insert a record, an auto-id number would be assigned. We can reference that id number to view the PDF for that specific row entry.

You pass the id variable into the viewpdf.php file by adding ?id=1 onto the URL (for example, viewing the PDF for record number three would be viewpdf.php?id=3).

PHP and MySQL can automatically generate that messy link for every record in the table. Just replace <a href="#">View PDF</a> in view.php with <a href="viewpdf.php?id=<?php echo $rows['id']; ?>">View PDF</a>

Open up view.php and see your dynamically generated table. You should have dynamic links that point to viewpdf.php?id=whatever. If you click on one of the links, the code in viewpdf.php will be processed for that row and a PDF will be generated and flattened and downloaded.

Voila!

I was only able to get this to work with text fields, although it is possible to do this with check boxes and other form elements. You’ll have to consult the fdftk documentation to see what varialbes need to be set for other form elements.

This is a bare bones implementation of PDF population. In real life this is implemented a lot better—i.e. I have my view.php file accessible only after logging in and all the pages are styled with CSS to look nicer.

Hopefully this all made sense. If you want to view the original tutorials I used for this, visit:

  • MacTech Tutorial—Explains how to do this using an HTML submission form rather than a PDF form. This was the basis for my tutorial.
  • MacTech Example—Working example of a PDF being populated by HTML.
  • PHPeasy—Basic PHP/MySQL tutorials.


  • Hello all...


    This tutorial is now defunct and officially archived. Visit the updated tutorial from now on.


    Comments on this post are now closed.

  • Darrell
    Downloaded the latest pdftk-php tarball - still problems will post in http://www.andrewheiss.com/blog/2009/06/19/pdft... .... Should have put the comment there originally...
  • Yes I was using the code as is. I haven't tried it since Andrew modified one of the functions to be public. However, when PDFTK worked the rest of it worked fine for me. I was using ubuntu. All forms created in livecycle 8. As stated livecycle dynamic forms never worked due to a limitation with pdftk.

    Are you able to generate the FDF or XFDF ok? This is what I tried.

    Open the FDF/XFDF with acrobat... It will then ask you to associate with a form or pdf. Navigate to your livecycle form. Your livecycle form should open up with the FDF data. If it doesn't then it probably means your FDF/XFDF was not created properly.

    Once you get that you can create a script that will do this for you. (The url will associate the FDF with the pdf) A.H. example worked well for static forms but because of the limitation of pdftk we couldn't use it.


    For example a url like this
    http://localhost/fine_form.pdf#FDF=http://local...

    See http://books.google.com/books?id=HRFtXWtbAdwC&a...
  • Darrell
    RE: Problems Repopulating LiveCycle Forms – Acrobat Forms Work Fine

    Thanks Jeremy, unfortunately all of my forms have been created in LiveCycle Designer... I hate the thought of converting them all (some of them are very complex)..

    Hedgehog, thank you for the links... I looked at the samples and I see the forms for the static and dynamic pages you created, however I don't see the coding of the $FDF_DATA_STRINGS that you used in order to successfully repopulate the LiveCycle Form. Are you just using the FDF_DATA_STRING examples in andrews php file?

    I am sure my problem probably stems from an improperly formated $FDF_DATA STRING that is naming the form field... Here is what I am experiencing (the name of my form is ShowOrders.pdf):

    When I run this command:
    pdftk ShowOrders.pdf dump_data_fields > form-fields.txt

    It generates the form-fields.txt report just fine, but when I open and view the file, the Field name contains a string that looks like this:

    FieldName: form1[0].#subform[0].Table1[0].Row2[0].ShowName[0]

    I have tried this "as is" to format the $FDF_DATA_STRING as follows:

    $fdf_data_strings= array('form1[0].#subform[0].Table1[0].Row1[0].ShowName[0]' => $pdf_ShowName);

    When I try to download the pdf file, it gives me a 500 internal error webpage.

    I am assuming the extra after each subheading in the DOM path is incorrect, so I have formatted the field by changing it to:

    $fdf_data_strings= array('form1[0].#subform[0].Table1[0].Row1[0.ShowName[0]' => $pdf_ShowName);

    This still results in a 500 Internal error.

    Lastly, rather than finding the fields using pdftk, I open the form directly in LiveCycle Designer and open the Script Editor Window, the heading in the window indicating the field name reports this:

    form1.#subform[0].Table1.Row1.ShowName

    (some of the [0] are not listed in the name as reported by LCD)... I have also tried this in FDF_DATA_STRING and it still gives the same result.

    Again, if I use Andrew's form made in Acrobat and use the Acrobat formatted FDF_DATA_STRING, it works just fine???

    I am running Fedora11 with pdftk-1.41-19.fc11.i586, httpd-2.2.11-8.i586 (apache), php-5.2.9-2.fc11.i586 and mysql-5.1.35-1.fc11.i586... All forms were created in LiveCycle 8.0 and are all saved as Acrobat 7 (static) pdf forms... So static forms are perfectly fine for me...

    I know it has to be something simple I am missing? Any thoughts?

    (PS - previous post error... I meant to say I modified the $FDF_DATA_STRING in the download.php file NOT the pdftk-php.php file)
  • I have gotten the STATIC livecycle forms to work with this code. However, PDFTK doesn't work the dynamic livecycle forms. There might be a very easy solution to all of this.

    All you have to to-do pass the xfdf or xdf to the pdf via url... This means you don't have to worry about all the code and you don't have to launch pdftk. (no more passthru command)

    I posted an example of this here
    http://www.andrewheiss.com/blog/2009/06/19/pdft...
  • Jeremy
    Darrell...I've ran into the same thing, just used acrobat to make the forms instead. Guess it might be nice to make them in livecycle, though :)
  • Darrell
    Problems Repopulating LiveCycle Forms - Acrobat Forms Work Fine
    Hello Andrew, I am having problems repopulating LiveCycle forms (tried static and dynamic for LC8 and LC7)... I have tried all of the above suggestions in this forum to no avail. The forms made with Acrobat 8 work fine... I think it is how the form fields from LiveCycle are being formed in the FDF_DATA_STRINGS that are causing my problems...

    I have taken your example form and converted it into a LiveCycleForm, run the command pdftk example.pdf dump_data_fields to get the actual field names and made the necessary changes to the FDF_DATA_STRING in the pdftk-php.php file... When trying to repopulate the form, it always comes up with a 500 Internal Server error (which is exactly what I get with my LiveCycle Forms)... Changing back the FDF_DATA_STRING to the acrobat format and pointing to the example.pdf made with Acrobat again will run perfectly fine...

    Is it possible for you to post a working example of a LiveCycle form that is repopulated with the data using the LiveCycle Form Names in the FDF_DATA_STRING... It would help out tremendously.. Thank you... Darrell
  • @niku Yep, it should work with LiveCycle forms just fine.
  • Hi Rik, I had the same issue it is an issue with pdftk. PDFTK won't open a livecyle dynamic form

    See for a potential work around
    http://www.andrewheiss.com/blog/2009/06/19/pdft...

    You can double check this by seeing if PDFTK will output for pdf form fields.

    Place your empty PDF form in your main site folder. Open up a command prompt or terminal and run this command in the site folder, changing file names as necessary:

    pdftk form.pdf dump_data_fields > form.pdf.fields
  • Rik
    Hi Andrew. Thanks for the great tutorial.

    I have hit a snag with this however. If i save my form in LiveCycle as a static form, everything works great. However, if i save it as a dynamic form, the fields do not fill in when opened.

    I need my form to be a dynamic form so I can set the fields to autofit in height.

    Any solution to this?
  • Scott
    Hi Andrew,

    Excellent work here! I tried this unsuccessfully about a year ago and I was thrilled to see that you recently updated your work on this. I think I'm pretty close, but here are my issues:

    I've been able to install your example code on both a Windows Server 2003 box with WAMP Server and on a Linux box. In both cases I am able to submit and record/view the results in the database.

    Problem 1:
    On the Windows server the output is creating temporary files (example is "fdf3f.tmp") in the "example" folder and Acrobat gives me the "unsupported file type or file has been damaged" when it tries to open the file.

    Problem 2:
    On the Linux box I am getting white screened when I click on the download link and there are no files being written to the "example" folder at all.

    Line 71: passthru("/var/www/vhosts/domain.com/httpdocs/heiss/heiss/example $pdf_original fill_form $fdf_fn output - flatten");

    I have tried the suggested correction steps of changing write permissions on the folders, along with commenting out the "unlink" command in pdftk-php.

    I'm not a very good coder and I was hoping that there is something very simple here that I'm missing.

    Great work and a great site here!

    I thank you in advance for your time.

    Scott
  • Sorry about the delay!

    For Windows:
    Your FDF is getting created, which is easy since Windows file permissions are a lot more permissive. The problem seems to be actually running pdftk.

    Make sure pdftk.exe is in the example folder, or the path in passthru() leads to wherever you have it stored. Make a new php file with one command in it:

    passthru("pdftk.exe --help")

    Open that page in your web browser and you should see the manual page for pdftk. If not, there's a problem with passthru finding pdftk. Adjust the path, add the .exe if necessary, and try again until the manual page shows up. That should fix your windows problem.

    With your Linux box, the main issue is probably permissions, since it's a lot more restrictive than Windows. Make sure the folder where you're writing the temporary fdf file is writable (use "ls -l" to see the permissions from the terminal--use an FTP program if it's a remote server to do it graphically).

    The script will try to write the fdf file to the path defined in tempnam(), near line 54 of pdftk-php.php. Try creating a folder named tmp in the example folder, give it write permissions for every user, and change tempnam to tempnam("./tmp", "fdf");

    So, your problem with Windows is (probably) running pdftk--change the path to pdftk in passthru(). Your problem with Linux is (probably) permissions--make sure you can write to the temporary folder.

    Good luck!
  • Scott
    In the case of the Windows Server, I can get Acrobat to open the temporary file and display the correct results after browsing to "example.pdf".
  • To anyone subscribed to this thread via e-mail:

    Here's an update on the project: http://www.andrewheiss.com/blog/2009/06/19/pdft...

    Check it out...

    Thanks!
  • Sly
    Is there any I can email you my code it still does not work for me can you have a quick look please.
  • Sure. It's my name at gmail.com
  • Sly
    sorry I am trying to insert a block of code and it just takes the last line
  • Sly
  • Sly
    I have added to classes row1 and row2 can you tell me where I should insert your code with these two class names. Sorry I am a rookie.













    View PDF
  • $rowshade = ($i % 2) ? "" : " class=\"shade\"";
    is an example of PHP ternay syntax. It basically says "If $i is divisible by two, $rowshade equals whatever comes between the ? and the : and if it's not divisible by 2, it equals whatever comes between the : and the ;

    So, if you want to alternate between the classes "row1" and "row2" you'll need to use this:
    $rowshade = ($i % 2) ? "row1" : "row2";

    You then echo $rowshade as the class of each <tr>:
    echo "<tr class=\"$rowshade\">";
  • Sly
    Hey I dont want to bother you is there an easy way to have the view.php file come up with alternating row colours I have tried loops if's and I just cant get it to work here is a copy of the code I have so far:




    View TOPS Rental Agreements










    Agreement Number
    Licensee
    First Name
    Last Name
    Phone
    Email
    Facility Rented
    Date Rented
    VIEW PDF















    View PDF
  • You'll need to use the while loop that goes through the mysql results to add a css class to the odd or even rows. Something like this:


    $i = 0;
    while ($whatever = mysql_fetch_array($results)) {
    $rowshade = ($i % 2) ? "" : " class=\"shade\""; // the % is the key operator
    echo "<tr" . $rowshade . ">";
    echo "whatever";
    echo "</tr>"
    $i++;
    }
  • Arun David
    Hi Andrew,

    I am using a different method to populate the PDF forms.Probably you can point me in the right direction..

    We develop forms in Designer Version 7.0 and enable the forms with Arco Pro version 8.Now the problem is that we are using iText API to populate these forms with data from another application.

    If we enable the forms we are unable to populate the data but if we do not enable the forms we are able to populate the data.There are some properties that are set when the forms are enabled and when not enabled and i have listed them below



    Before Enabling the following properties set.

    /Type, /StructTreeRoot, /MarkInfo, /AcroForm, /Pages, /Names, /Metadata

    After enabling(both version 8 following properties set.

    /NeedsRendering, /Type, /StructTreeRoot, /MarkInfo, /AcroForm, /Pages, /Perms, /Names, /Metadata


    After enabling /NeedsRendering , /Perms properties are set extra. Please explain more on the properties set during desing or enabling form.



    Does these properties hinder in reading the forms.Please Help
  • I'm not sure what exactly you are referring to--I've never "enabled" forms in Acrobat for a PDF already created in LiveCycle...
  • Kanca
    It still has all of the $_POST variables under the first one. I ended up trying it with Adobe Reader 9, and it works perfectly. Anything below 9, and it combines all of the $_POST variables.
  • Simon
    btw:

    client : XP with firefox
    server : linux, apache, php5, mysql
  • Simon
    Hi everyone,
    First : awesome posting!
    As it seems, I concocted something similar with the use of createXFDF. My workflow goes as follows : user input data into HTML form -> Submits -> fields are saved in database and in fdf-file -> email is sent to another party with rendered/flattened pdf (not linked fdf).

    This worked so far very well with text. Now I need to act on a 'company name' field in the HTML and insert the corresponding company logo (saved in a directory on the webserver where all processing happens) into the pdf that gets sent via Email.

    I almost lost all my hair, pulling it. How can this be achieved? I have http://phpdig.net/ref/rn20re278.html but I don't think this will work. (as is doesn't).

    PLEASE, someone help me ;-)
  • Hmmm... I've never seen images used as fdf form fields, so I have no idea... maybe someone else has run in to this?
  • Kanca
    I found this page while doing some research...the dump file showed me what was wrong. I have several form elements in my PDF, and it is set up to HTTP submit using a button to a specific url with the dump file you posted in your original post.

    The submitted variables are:

    DateTimeField1: RadioButtonList=TextField1=TextField2=CheckBox1=0
    testCook:
    parent:
    MOODLEID_: %E2%C8%13E%BDt%A3%1A%F2F%F5%29%F1

    The problem is that all the variables after the first one are actually part of the first one, not separate. Have you run across this issue, and, if so, how do I fix it? Thanks.
  • It may be that the dump.php file is just outputting the variables slightly wrong. Make a page named dump1.php with this in it:


    <?php
    print_r($_POST);
    ??>


    See what that outputs.
  • RON
    Hello Andrew. I have a call centre. The outboudncalling system uses mysql database. Currently I am using Adobe Interactive Pdf as a sales script to answer pre-qualifing questions for my prospects.

    The problem is; when i get someone that is interested in my clients services I have to print the contact details and notes to pdf from the server and send to my client. Then I have to update that same contact info in the inreractive pdf witch has all pre-qualifying question, I do a save as in the clients folder and reset the fomr for the next call. I then send both of these pdf's to the client.
    That to much labour :( and cuts into my productivity.
    On the client end they have to manually import the info into their CRM (database).

    Since I do marketing and have a fully funtional call centre perhaps we can make a business to business alliance to develop and resell software solutions. If you know a solution to this problem I would also love to speak with you. My phone number is 905-615-7744 ext 3 and i can be reached monday to saturday 9am to 9pm

    I beleive we can form a profitable relationship based on you designing software solutions weather custom or for resale and I can market. We have every target market for any product and can reach 50-100 Key Decision Makers per day
  • Hi Andrew, I tried posting a few times but it still hasn't gone through.

    my FDF file looks ok. I think this message board took the info about due to the code. Image of my fdf file

    http://www.pathway2curis.com/james/PDF_DEMO/5-2...

    What is interesting is the when I try to link to my FDF file directly it gives me a 403 error (maybe a .htaccess issue)

    http://www.pathway2curis.com/james/PDF_DEMO/fdf...

    What I'm trying to-do is have a user login. They select some information this gets pushed to a unique FDF file and then when they select a certain PDF file it will grab this FDF file and place the information inside the pdf. There will be about 200-300 pdfs so the FDF file has to stay on the server until they log off or generate new query information for the FDF file.

    Does this make sense.
  • Does the FDF file work in the PDF if you manually open the FDF with Acrobat?

    Does it work if you manually inject the FDF into the PDF via the command line and pdftk?

    To make the FDF file stay on the server while you serve all the PDFs you'll just need to comment out the line that says unlink(), which deletes the temporary FDF. Unlink it when you are done serving everything.
  • Sly
    sorry output - flatten with
  • Sly
    Which command do you suggest I use.
    passthru( 'pdftk TOPS_Rental_Agreement.pdf fill_form '. $fdf_fn.
    ' output - fill_form' );
    what woudl I replace output - fill_form with
  • In theory all you need to do is take off flatten:

    pdftk LoanAgreement.pdf fill_form '. $fdf_fn.' output -

    Leave the -. It makes the pdf output as stdout rather than make a file.

    Some versions of PDFs, though, don't work when flattened by pdftk. If it doesn't work, try backsaving your PDF as 1.5 rather than the newer 1.7 or 1.8
  • Sly
    thank you for the quick reply
  • Sly
    Is there a simple way to edit the pdf after it is downloaded? I am looking for the user to be able to do a quick search and edit the pdf once it is downloaded and republish to the database.
  • If you don't flatten the file you will still have the button for submitting, so the user could easily make changes and resubmit - you'd just have to set up the backend to handle updates
  • Hi Andrew,

    I think this form took out some of the FDF info

    http://www.pathway2curis.com/james/PDF_DEMO/fdf...
    Here is my install
    http://www.pathway2curis.com/james/PDF_DEMO

    What i'm trying to-do after I figure this out is create a unique FDF file once a user logins in an then pass the FDF information to each pdf once they click on the PDF. THere are probably about 200pdfs... Any ideas as the best way to-do this?
  • I also wanted to add that the FDF file is formed..

    However, when I try to download it says that the file is damaged and can't be opened. I'm using your all of your examples including the pdf and database..


    ---FDF File

    %FDF-1.2
    %âãÏÓ

    1 0 obj
    <<
    /FDF << /Fields [ <>
    <>
    <>
    <>
    <>
    <>
    <>
    <>
    <>
    <>
    <>
    <>
    ]
    >>
    >>
    endobj
    trailer
    <<
  • This FDF file looks malformed, or non existent... Do you get the error on the PDF or the FDF?
  • niku
    hi andrew

    does it really work with livecycle pdf forms I tried to study this blog but couldnt make out what is the end result
  • HI Andrew,
    Thank you very much. This is great. Is there anyway to get around the passthru() command? I have a site hosted and not sure if they are going to turn this function on...

    Anyway around this?


    [25-May-2009 17:43:55] PHP Warning: passthru() has been disabled for security reasons in .....

    Thank you,
  • No, sadly I haven't found a way to get around passthru() with pdftk. It's a command line program that has to run as such, so unless we can figure out a way to run a terminal command with PHP without passthru(), we're stuck...
  • John Back
    Legend :) After innumerable sites telling me the various iterations of pdf pre (FDF/XFDF) and post XML form data just don't work anymore because of changes to acrobat/designer etc, I read your post and realise the form names aren't the same as the import tokens required. Much appreciated - now I can write automated pdf and convert our paperwork to webpage/database storage. Thank you so much.
  • Andrew Cano
    Hello Andrew,

    i have beed trying the updated script and keep getting this error:

    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /public_html/application/pdftk-php.php on line 53

    I haven't modified the pdftk-php.php script but everything else seems to be working. i was able to fill a pdf from the example on this page but i need to fill checkboxes too so i am trying the updated scripts

    thanks for any help you can give
    Andrew
  • Rob Hecker
    Hi Andrew.

    Actually this script does connect the FDF with the PDF. My users won't even have to know what a FDF file is. It's true that the FDF is not embedded in the PDF, but when the FDF file is called up, it opens in the appropriate PDF without the user knowing what's going on.

    I wish I could show you my system, but it's restricted to my client's staff.

    I wanted to mention this solution here because there may be others who can't use PDFTK because they are not able to install it on their webserver.
  • The only problem I see with that PHP solution is that it doesn't quite connect the FDF with the PDF. The script ends up serving an FDF file that the user then has to manually populate on their own with Acrobat or Reader.

    In my case I was trying to serve the actual finished PDF so that the user could immediately see and use the PDF without having to have Acrobat, just in case the user doesn't have a more robust PDF viewer that can handle form data (like Preview on a Mac)
  • Rob Hecker
    The hurdle I run into is that PHPTK must be installed on the server, and since I am on a UNIX commercial shared hosting, I cannot install the software.

    On my Apache test server under Windows, everything works just fine.

    So I can get the data from the PDF to the MySQL database, and I can get in out of the database into a nicely formed fdf file, but I can't get the fdf and the PDF template to connect. I need staff on various platforms to be able to do this from the website. It seems like such a small step! Since it appears that I have to abandon the idea of using PDFTK, I wonder if there is a PHP script out there that will connect the fdf with the pdf. Any thoughts?
  • Sadly I haven't found any PHP-only solution to inject a PDF with an FDF--it seems like you need a robust program like pdftk to actually do it.
  • Rob Hecker
    I have discovered a purely PHP solution that does this.
    http://koivi.com/fill-pdf-form-fields/

    My forms have about 30 fields, including radio buttons and checkboxes, and they populate just fine.

    The code is very simple and requires very little modification. For me this is a perfect solution because it does not require PDFTK.
  • Jeremy
    I have the IT guy at work seeing if cmd.exe has the correct permissions. While I wait...i noticed that all of the sites I've able to get this to work on run with FastCGI and the server at work we have ISAPI on, could this be an issue as well?

    Thank you very much for your help!
  • I have no idea about FastCGI or any other server modules--I'm a total newb when it comes to actual server configuration (and I've never actually used IIS)

    Sorry!
  • Jeremy
    A couple of other questions...

    Is there something that could need to be installed on the server which isn't? The server i installed on my localhost was indigoampp but this cant be used at my work as they have their own microsoft server. Perhaps there is something which needs to be installed in order for pdftk.exe to work. At the moment it looks like every file and folder in my program have every access that is available by the way.

    Thank you so much in advance for any type of help you can offer! thank you!
  • The "Unable to fork" error happens in PHP under Windows and IIS when there is a problem with program permissions. Check out these two sites for more information:

    http://www.iis-aid.com/articles/trouble_shootin...

    http://www.somacon.com/p255.php

    Thankfully it's just a server configuration problem and not a problem with pdftk.exe or anything else.
  • Jeremy
    Please help...

    the fdf does make when i get comment out the unlink command...but i always get the error when i run the program on our windows server...on my localhost with apache installed it works fine...

    i just cant figure this out. the error i get when the pdf downloads and i open it in notepad is this:


    Warning: system() [function.system]: Unable to fork [pdftk apps/ismie_physician.pdf fill_form C:\Inetpub\prefill\fdf5010.tmp output - flatten] in C:\Inetpub\prefill\pdftk-php.php on line 71


    please help :(
  • UPDATE:
    Well I have PDFTK and gotten real field names but when I click on download I get plain white screen nothing on it no errors...?
  • Unfortunately it means you still have some errors.

    The best way to check to see if everything is actually working is to manually inject the fdf into the pdf at the command line.

    Comment out line 74 in pdftk-php.php (the line that says unlink( $fdf_fn ); and run the script again.

    It should make a randomly-named file in the main folder of the script. If there is no new file, the fdf isn't getting made at all, probably because of problems with permissions.

    Line 58, $fdf_fn = tempnam(".", "fdf"); specifies where the fdf is made, (the . means the current directory). Try specifying a different folder or make sure the folder where pdftk-php.php is has write permissions.

    Once the fdf file is made correctly, try running pdfk from the command line manually, typing out pdftk your_original_pdf.pdf fill_form newly_created_randomly_named-fdf output test.pdf flatten

    Open that new pdf. If it doesn't work, the fdf wasn't well formed or was corrupted. It should work, though.

    Try debugging the system like that and let me know what happens.
  • Crap Link didnt show up go to this site and you can click on the images to make them full sized maybe you can see how it is acting remotely. Again man thanks for all of your time and effort into this issue.

    http://rs-sunconst.com/copper/thumbnails.php?al...
  • Andrew

    Thanks for the info I had downloaded the latest and but even when I set your sample up I can enter name and email into database and can view the list of submitted info but get a corrupt PDF while downloading its got to be something i am missing. The server is Unix running apache and
    Apache version 2.2.11 (Unix)
    PHP version 5.2.9
    MySQL version 5.0.75-community-
    log

    I am so close but getting so frustrated with this project. I know it is possible But why dont it work.... ? I took some screen shots of the errors I am getting and if you have a sec to look at it they can be found here.
  • Andrew thanks for the lesson I had been kicking this dead horse for 6 months now and finally got the pdf form to submit to the database I have a lot more fields than your sample had but was able to get the form to post to the database finally. My problem now is there are some fields that are check boxes and PDFtk doesnt output the form.pdf.fields file you speak of. is there a work around for this error? The fields really need to be check boxes even if they are skipped in the re-population process. Can you help?

    Thanks in advance for your time devoted to this issue. I am sure there are alot of folks out there that really needed this tutorial.
  • Robert,

    Sorry about the delay!

    I recently updated this whole system to make it work more reliably overall, and work better with checkboxes. I've released it online at Google Code at http://code.google.com/p/pdftk-php/

    Try installing that version of the system--you should find commented code explaining how to use checkboxes.

    Let me know how that goes...
  • I finally got the insert.php to submit to the database the problem i am having now is to repopulate the form My form has some check boxes in it and pdftk says input errors nothing done. Is there a work around for this issue? I have came so far I have a PDF form that submits alot of data in one shot to the database and it finally works fine.... Please man can you help with the repopulating of the form?

    The main part of my insert.php looks like this ( not complete for security reasons.

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    error_reporting(E_ERROR);
    //Save data from the HTTP POST values submitted from the PDF
    $date=$_REQUEST["date"];
    $jobnamee=$_REQUEST["jobname"];
    $jobnumber=$_REQUEST["jobnumber"];
    $weather=$_REQUEST["weather"];
    $equiprented=$_REQUEST["equiprented"];
    $equipowned=$_REQUEST["equipowned"];
    $item1=$_REQUEST["item1"];
    $item2=$_REQUEST["item2"];
    $item3=$_REQUEST["item3"];
    $item4=$_REQUEST["item4"];
    $item5=$_REQUEST["item5"];
    $item6=$_REQUEST["item6"];
    $item7=$_REQUEST["item7"];
    $item8=$_REQUEST["item8"];
    $item9=$_REQUEST["item9"];
    $item10=$_REQUEST["item10"];
    $downtime1=$_REQUEST["downtime1"];
    $downtime2=$_REQUEST["downtime2"];
    $downtime3=$_REQUEST["downtime3"];
    $signuture=$_REQUEST["signuture"];
    $date2=$_REQUEST["date2"];
    $location=$_REQUEST["location"];
    $visualtest=$_REQUEST["visualtest"];
    $manualtest=$_REQUEST["manualtest"];
    $classification=$_REQUEST["classification"];
    $sloping=$_REQUEST["sloping"];
    $trenchbox=$_REQUEST["trenchbox"];
    $engineered=$_REQUEST["engineered"];
    $utilitiesoverhead=$_REQUEST["utilitiesoverhead"];
    $utilitiesunderground=$_REQUEST["utilitiesunderground"];
    $laddersramps-$_REQUEST["laddersramps"];
    $airtestconfined=$_REQUEST["airtestconfined"];
    $spoilspile=$_REQUEST["spoilspile"];
    $ppe=$_REQUEST["ppe"];
    $currentlocates=$_REQUEST["currentlocates"];
    $problems1=$_REQUEST["problems1"];
    $problems2=$_REQUEST["problems2"];
    $problems3=$_REQUEST["problems3"];
    $problems4=$_REQUEST["problems4"];
    $signuture1=$_REQUEST["signuture1"];


    // Insert all the data from above into the table in the database
    $sql="INSERT INTO $tbl_name (date,jobname,jobnumber,weather,equiprented,equipowned,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,downtime1,downtime2,downtime3,signuture,date2,location,visualtest,manualtest,classification,sloping,trenchbox,engineered,utilitiesoverhead,utilitiesunderground,laddersramps,airtestconfined,spoilspile,ppe,currentlocates,problems1,problems2,problems3,problems4,signuture1)VALUES('$date','$jobname','$jobnumber','$weather','$equiprented','$equipowned','$item1','$item2','$item3','$item4','$item5','$item6','$item7','$item6','$item7','$item8','$item9','$item10','$downtime1','$downtime2','$downtime3','$signuture','$date2','$location','$visualtest','$manualtest','$classification','$sloping','$trenchbox','$engineered','$utilitiesoverhead','utilitiesunderground','$laddersramps','$airtestconfined','$spoilspile','$ppe','$currentlocates','$problems1','$problems2','$problems3','problems4','$signuture1')";
    $result=mysql_query($sql);

    // If it worked, say so...
    // This will change later on so it won't have the like to view all users, obviously...that's just for testing purposes
    if($result){
    echo "Data Submission was Successful";
    }

    else {
    echo " DOH we have an ERROR";
    }

    // Close connection to the database
    mysql_close();
    //

    ?>

    Thanks in advance for your help so far and for later this tutorial finally made me get it to work. you can contact me via email or drop me a message on the contact me tab of my office located at http://rs-sunconst.com
  • Dint work for me your script. It does not post the variables.
  • Try installing the new version of the script, found at http://code.google.com/p/pdftk-php/

    It is a lot more reliable...
  • Eddie
    Thanks for repairing the links so quickly.

    So, I made a small form in LiveCycle Designer 7 and used the dump.php file from above and I am getting this junk out.

    testform%5b0%5d.answers%5b0%5d.CheckBox1%5b0%5d=0&testform%5b0%5d.answers%5b0%5d.HTTPSubmitButton2%5b0%5d=

    I'm sure that I made a mistake in the form, or haven't configured it quite right, but darned if I can find where to fix it. Have you seen this output, if so, where did I go wrong?

    Thanks for the assistance!
  • Yeah, it's a problem with WordPress and how it handles quotes... Make sure they are all straight quotes rather than curly typographer's quotes
  • Eddie I had seen that error

    check your quotes in the dump.php that is where my error was
  • I found this tutorial and it seems to be just the thing for me, however, the links to your php text files seem to be broken. When I follow them I get an Error 404, not found.

    Could you either repair the links, let me know what I did wrong, or otherwise provide the text files, please.

    Thanks,
    Eddie
  • Sorry about that! The links should be fine now!
  • Jagadeesh
    Hi, Andrew,

    I seen above script . nice tutorial but i have one doubt

    Is it possible to generate pdf document to folder with using same code ?

    or

    is it possible to attach pdf document with mail function in php with using same code ???

    Is there any solution Please give me suggestion

    Thanks

    Jagadeesh
  • Yes, it is possible to e-mail the pdf. You just need to make some adjustments to the passthru() command. Instead of typing -, which outputs the pdf as standard output, type a path to a temporary folder. It should make a PDF there that you can then attach using the PHP mail() function.

    For example:
    Instead of "pdftk ... fdf output - flatten," use "pdftk ... output /temp flatten"

    That will put a lot of strain on server bandwidth, though. I'd recommend setting up a script that serves the PDF and just send a link to that script in the e-mail, unless you really want to push PDFs around like that.
  • Larry
    Hi, Andrew,

    I think when you create a PDF form with livecycle, you can use forge_fdf.php. This script will create a fdf input. But the form created in livecycle will take xfdf input instead. Can you tell me how you make the PDF form take fdf input?

    Thanks

    Larry
  • The PDF should still be able to handle the fdf file - it's kind of like HTML and XHTML; xfdf is XML-based FDF

    FDF should be backwards compatible in a LiveCycle form. I've never had problems with plain vanilla FDF files in Acrobat or LiveCycle...

    Check these sites for more information:
    http://lukerymarz.blogspot.com/2008/10/adobe-xf...
    http://www.michaelnolan.co.uk/2007/03/22/pdf-fo...

    I might be wrong though... let me know what you find out...
blog comments powered by Disqus