PDF form population with FPDM
Asked Answered
I

5

5

what I'm trying to accomplish is population of PDF form with PHP.
I tried many ways, I found that FPDM (FPDF) is working well when I create a new form, or use the form from source file they provided.
My problem is when I'm using already created PDF form, the form has restrictions such as Owner password, document is signed and certified. I used the app to remove those restrictions, some of them are left. In picture below you can see how my current PDF looks like.

enter image description here

That PDF also was compressed, and because FPDM was throwing the error that 'Object Stream' is not supported I decompressed it through PDFTK, so file went from 1.48 Mb to 6.78 Mb.

To get all form field names I used also PDFTK, so I have them in txt file.

There are two ways I can do by the instructions of FPDM:

First way is only to send an array field_name => value along with PDF I want to change and that's it. So when I use PDF described above I get error:

'FPDF-Merge Error: field form1[0].#subform[0].Line1_GivenName[0] not found'

Just to remind that I have all names and this name exists.

<?php
require('fpdm.php');

$fields = array(
        'form1[0].#subform[0].Line1_GivenName[0]' => 'my name'
    );
$pdf = new FPDM('test.pdf');
$pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-     1, true if UTF-8
$pdf->Merge();
$pdf->Output('new_pdf.pdf', 'F');
?>

The other way is that I create FDF file with createXFDF function and then use FPDM to merge FDF to PDF. This solution creates 'new_file.pdf' like I want but empty :)

function createXFDF($file, $info, $enc = 'UTF-8') {
$data = '<?xml version="1.0" encoding="'.$enc.'"?>' . "\n" .
    '<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">' . "\n" .
    '<fields>' . "\n";
foreach($info as $field => $val) {
    $data .= '<field name="' . $field . '">' . "\n";
    if(is_array($val)) {
        foreach( $val as $opt )
            $data .= '<value>' .
                htmlentities( $opt, ENT_COMPAT, $enc ) .
                '</value>' . "\n";
    } else {
        $data .= '<value>' .
            htmlentities( $val, ENT_COMPAT, $enc ) .
            '</value>' . "\n";
    }
    $data .= '</field>' . "\n";
}
$data .= '</fields>' . "\n" .
    '<ids original="' . md5( $file ) . '" modified="' .
        time() . '" />' . "\n" .
    '<f href="' . $file . '" />' . "\n" .
    '</xfdf>' . "\n";

return $data;
}


require('fpdm.php');

$pdf = new FPDM('test.pdf', 'posted-0.fdf');
$pdf->Merge();
$pdf->Output('new_file.pdf', 'F');

One more thing, if I try to open FDF file in Acrobat I get a message

'The file you are attempting to open contains comments or form data that are supposed to be placed on test.pdf. This document cannot be found. It may have been moved, or deleted. Would you like to browse to attempt to locate this document?'

but the file is there, not moved or deleted. When I find it manually the form populates.

If anyone has experience with this, any help or advice would help a lot.

Thank you in advance, Vukasin

EDIT: More info about the PDF file enter image description here

Illustrative answered 31/8, 2014 at 8:41 Comment(4)
That existing PDF, what are the entries for PDF Producer and the document creating software? (to be found in the Description tab of the Document properties).Seduction
What happens when you enter the absolute path of the target PDF in the XFDF? What happens when you follow the advice of Acrobat and locate the target PDF when opening the XFDF? What happens when you actively import the XFDF into the already open target PDF?Seduction
Max Wyss thank you for answering. Take a look at my edit of the question. ThxIllustrative
@MaxWyss I've placed the absolute path of the target PDF, 'new_pdf.pdf' file is created but empty. However, if I try to open FDF file, I'm taken to default browser where that file is opened and the data was present. Do you know any other way to merge FDF to PDF? Thank againIllustrative
S
7

After revealing the creator/producer info the problem is clear.

You do not have a real PDF form, but you have a XFA form (created by LiveCycle Designer), wrapped in a PDF wrapper so that Adobe Reader can display it.

XFA forms do not support (X)FDF. You have to import data using XML. You can try to export the data from a filled version, and then use this as a sample for creating the import XML.

Note that the XML export/import format XFA forms use is not the same as XFDF (which is simply an XML representation of FDF, the PDF-native forms data format).

Seduction answered 2/9, 2014 at 17:56 Comment(1)
Thanks for the answer, I will mark this as accepted because on someway answers on my question. I tried to remove xfa in PDFTK following this tutorial but still I can't make it work, the creator is still the same and I have the same problems as from above. Do you know how can I merge XML into XFA? Thanks againIllustrative
M
14

I have spent more than a complete day working through issues with FPDM, and was hard pressed to find someone who had similar issues.

The following format worked for me: PDF 1.4 (Acrobat 5). I had to actually go to Save As -> choose Adobe PDF Optimized, then click the Settings button. From there I had to choose the version from the drop-down/fly-out menu.

I received the error: 'not compatible with fast web view' or similar. If in the PDF Optimized settings option you click 'clean up' on the left side you can untoggle fast web view.

Now I am receiving the error, PDF-Merge Error: field 'fieldname' not found. When I run it through pdftk hoping to resolve this, I receive the error: FPDF-Merge Error: Number of objects (35) differs with number of xrefs (36), something , pdf xref table is corrupted :(

To fix this issue, I had to download and install pdftk server utility on ubuntu

sudo apt-get install pdftk

After install, I ran this command to repair a PDF’s corrupted XREF table and stream lengths, if possible:

pdftk broken.pdf output fixed.pdf

When I open fixed.pdf it has no issues whatsoever and populates the fields correctly. Hallelujah this was the most annoying issue in the world. To summarize, I had to take the pdf and put it through the following steps:

  • Edit PDF to preference
  • Save As > .pdf optimized > Settings > Acrobat 5 > uncheck fast web under cleanup
  • Open file in pdftk and resave
  • Install command line pdftk via ubuntu
  • run command: pdftk broken.pdf output fixed.pdf

done.

Meshed answered 22/12, 2016 at 15:14 Comment(2)
This is only working solution for my 5h search and trying to rewrite library from scratch. You are a lifesaverRedfish
I'm using this solution on 06/2020. For those who are on Win, download PDFtk Server pdflabs.com/tools/pdftk-server Also, this proyect is now being updated here github.com/codeshell/fpdm Thanks, Kyle!Laubin
S
8

Thanks to Kyle's answer, I resolved a similar issue.

I have a pdf created in Adobe Acrobat Pro DC and need to populate its form fields from web input.

On my development machine, I created an fdf file from my web form data, and merged it into the pdf using pdftk (called from php with exec() ).

But I couldn't put that on the cloud linux webserver where my site is hosted because it requires deprecated libraries.

So I switched to FPDM and had the following errors:

'Fast Web View mode is not supported'. I fixed that by setting preferences in 'save as' in Adobe Acrobat Pro (save as -> pdf optimized -> settings -> clean up -> uncheck Fast Web View).

'Object streams are not supported' - again, fixed in the 'save as' preferences (clean up -> object compression options -> remove compression).

'Incremental updates are not supported' - again, fixed using 'save as' in Acrobat.

Then FPDM ran, but couldn't read any field names.

The One-Step Solution:

Take the original file pdf - with incremental updates, object compression and fast web view - and pass it through pdftk on Windows, exactly as Kyle describes.

> pdftk broken.pdf output fixed.pdf

Now FPDM populates the fields correctly from the fdf file.

Samara answered 16/1, 2018 at 23:2 Comment(3)
glad I could help, congratulations on your first contribution, welcome to stackoverflow!Meshed
was it slow? I'm running it and nothing seems to happen. No output errors or anything. Just blank but it doesn't go back to the cursor. Adding verbose to the end does not change anything for meJakob
nevermind. it's a Mac Sierra issue: #39751383Jakob
S
7

After revealing the creator/producer info the problem is clear.

You do not have a real PDF form, but you have a XFA form (created by LiveCycle Designer), wrapped in a PDF wrapper so that Adobe Reader can display it.

XFA forms do not support (X)FDF. You have to import data using XML. You can try to export the data from a filled version, and then use this as a sample for creating the import XML.

Note that the XML export/import format XFA forms use is not the same as XFDF (which is simply an XML representation of FDF, the PDF-native forms data format).

Seduction answered 2/9, 2014 at 17:56 Comment(1)
Thanks for the answer, I will mark this as accepted because on someway answers on my question. I tried to remove xfa in PDFTK following this tutorial but still I can't make it work, the creator is still the same and I have the same problems as from above. Do you know how can I merge XML into XFA? Thanks againIllustrative
T
0

I found a tool called Scribus after examining the template used in the fpdf example. You can use it to create pdf templates and the format created plays nice with fpdm. It isn't a complicated program and allows you to create form fields with permissions/parameters around them (like making a form field read-only after you populate data in it from an online form). For my application, I needed to have some fields pre-populated from values in a database that were non-editable, have other fields that were pre-populated, but still editable and some fields that were empty and required completion (force required). It was all possible using the template that Scribus has generated.

Trudytrue answered 10/1, 2019 at 15:48 Comment(0)
T
-2

MAGNIFICENT work!

The One-Step Solution:

Take the original file pdf - with incremental updates, object compression and fast web view - and pass it through pdftk on Windows, exactly as Kyle describes.

pdftk broken.pdf output fixed.pdf Now FPDM populates the fields correctly from the fdf file.

I created a PDF with Acrobat, then "fixed" it with pdftk, and FPDM the class merged the data perfectly...

Thies answered 16/11, 2019 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.