Jump to content

Using FDF (or something else) to write to PDF form


hadoob024

Recommended Posts

I tried doing some searches but couldn't find much. I basically have a PDF form. I need to be able to fill out the various form fields using values returned from a query that I run in SugarCRM.  I have my query running properly, I just need to know how to actually write to this PDF form. I've used ezPDF stuff before, but that seems to write information using hard-coded coordinates on a page. I need to be able to accurately write to a form field. Anyone done this before?

 

I've seen something called FDF http://www.verypdf.com/pdfform/fdf.htm

 

I've also seen http://www.pdfforsugar.com

 

This one is closer to what I'm looking to do, but it's not going to accomplish what I need. It currently only gathers all the information to populate the PDF form from one record from one module. The PDF form that I need to populate from Sugar gets its information from several modules. I run a query that joins several modules and it’s from this resultset that I want to populate the PDF form. Oh well. Guess this tool is a no go and will have to figure out something else.

 

Any other tips/suggestions? Thanks!

Link to comment
Share on other sites

fdf sounds like it's what you're after. You can populate your PDF from an array with the fields of the pdf in it.

 

require_once 'createFDF.php';
$data=array("formfieldinpdf1"=>$valuefromsugar1,"formfieldfrompdf2"=>$valuefromsugar2,"etc"=>"etc"); 
$fdf_file=strtotime(date("Y-m-d H:i:s")).".fdf";
$fdf_dir=dirname(__FILE__).'/fdf';
$pdf_doc="path to your pdf"; 
$fdf_data=createFDF($pdf_doc,$data);
if($fp=fopen($fdf_dir.'/'.$fdf_file,'w')){
        fwrite($fp,$fdf_data,strlen($fdf_data));
        echo $fdf_file,' written successfully.';
}else{
        die('Unable to create file: '.$fdf_dir.'/'.$fdf_file);
}
fclose($fp);

Link to comment
Share on other sites

Really?  It's that simple???  So basically, I have to build out my MASSIVE array that links up the form fields in my PDF with the values returned by my query? 

 

So basically just go to http://www.adobe.com/devnet/acrobat/fdftoolkit.html, download the toolkit, copy it over to the server, and I should be up and running?  Cool.  Thanks man!

 

Have you used this before?  Any caveats I should be aware of?

Link to comment
Share on other sites

fdf sounds like it's what you're after. You can populate your PDF from an array with the fields of the pdf in it.

 

Quick follow-up.  I went and downloaded the Acrobat Forms Data Format (FDF) Toolkit for Windows from http://www.adobe.com/devnet/acrobat/fdftoolkit.html.  Where are the files that I need in order to add this functionality for PHP?  I went through the whole zip file but can't seem to find any PHP files in it (especially createFDF.php or any other PHP files).  Not sure where to go from here.

Link to comment
Share on other sites

Wow!  Cool!  Just downloaded the zip.  OK.  So it looks like the only file I need out of this zip file in order to implement the code you have is that "createFDF.php", correct?  Will prob run through the tutorial they have on that site.  Thanks again!  Will let you know if I run into any issues.

 

Quick follow-up.  Just opened the "createFDF.php" file.  Looks like there are some weird characters in there.  What should this line actually read (my PHP editor is having some issues with some special characters):

 

$data="%FDF-1.2\n%????\n1 0 obj\n<< \n/FDF << /Fields [ ";

 

 

What should those "?" symbols read?  I don't think that they're supposed to be questions marks.  Just looked at the source file on their website, and even it looks weird:

 

$data="%FDF-1.2\n%����\n1 0 obj\n<< \n/FDF << /Fields [ ";

Link to comment
Share on other sites

OK.  Been playing around with this for a little bit (tried with some sample garbage data into the PDF).  I had to modify some of the code from that file, so mine now looks like:

 

        require_once 'createFDF.php';
                
        $data=array("patientName"=>"TESTING PATIENT NAME","18"=>"MY TEST ADDRESS"); 
        $fdf_file=strtotime(date("Y-m-d H:i:s")).".fdf";
        //$fdf_dir=dirname(__FILE__).'\fdf';
        $fdf_dir="C:\\Users\\User1\\Desktop";
        $pdf_doc="C:\\Users\\User1\\Desktop\\1500.pdf"; 
        $fdf_data=createFDF($pdf_doc,$data);
        if($fp=fopen($fdf_dir.'\\'.$fdf_file,'w')){
                fwrite($fp,$fdf_data,strlen($fdf_data));
                echo $fdf_file,' written successfully.';
        }else{
                die('Unable to create file: '.$fdf_dir.'\\'.$fdf_file);
        }
        fclose($fp);

 

This does seem to work, but I have two follow-ups:

 

1)  This code ends up creating a FDF file, but I need a PDF.  Is this possible?

 

2)  When opening up the FDF file, I get the following message:

 

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

 

Any thoughts on these issues?

Link to comment
Share on other sites

OK.  Making some headway with this.  Just wondering though how to select a checkbox if I have one?

 

For example, I have a checkbox for gender.  How do I select a value?  I've tried:

 

$data['15'] = $gender;

 

and

 

$data['15'] = "checked";

 

and

 

$data['15'] = "selected";

 

 

But none of them seem to work.  Any thoughts?

Link to comment
Share on other sites

Well.  Finally figured this out.  Pretty simple too.  First of all, you'll need to go to:

 

http://koivi.com/fill-pdf-form-fields/

 

 

This tool is SUPER simple for populating the form fields of a PDF file with an array of whatever data you want.  This tool, however, saves the file as a FDF file.  You'll then need Pdftk tool from:

 

http://www.pdflabs.com/docs/install-pdftk/

 

 

This tool will then take your FDF file and convert it to PDF.  So in your PHP code, all you need to call is something like:

exec('pdftk My_Form.pdf fill_form YOUR_FDF_FILE.fdf output YOUR_PDF_FILE.pdf flatten');

 

That's all there is to it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.