hadoob024 Posted October 28, 2010 Share Posted October 28, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/ Share on other sites More sharing options...
taquitosensei Posted October 28, 2010 Share Posted October 28, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/#findComment-1127620 Share on other sites More sharing options...
hadoob024 Posted October 28, 2010 Author Share Posted October 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/#findComment-1127629 Share on other sites More sharing options...
hadoob024 Posted October 28, 2010 Author Share Posted October 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/#findComment-1127655 Share on other sites More sharing options...
taquitosensei Posted October 28, 2010 Share Posted October 28, 2010 I forgot to mention this doesn't use Adobes fdf. The files for this are here. http://koivi.com/fill-pdf-form-fields/ There's also better documentation there. Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/#findComment-1127660 Share on other sites More sharing options...
hadoob024 Posted October 28, 2010 Author Share Posted October 28, 2010 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 [ "; Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/#findComment-1127685 Share on other sites More sharing options...
hadoob024 Posted October 28, 2010 Author Share Posted October 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/#findComment-1127708 Share on other sites More sharing options...
hadoob024 Posted October 28, 2010 Author Share Posted October 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/#findComment-1127785 Share on other sites More sharing options...
hadoob024 Posted October 29, 2010 Author Share Posted October 29, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217114-using-fdf-or-something-else-to-write-to-pdf-form/#findComment-1128166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.