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.

I've also seen MPDF and TCPDF mentioned in these forums, but they seem like tools for creating a PDF from scratch. I have PDF form already, and I need to populate various fields on this form. Is that possible?

Any other tips/suggestions? Thanks!

    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.

      a month later

      Hey

      I am using the website mentioned above and the toolkit. I am able to generate the fdf files - no problem there. However, i am not able to generate the pdf file.
      Actually, how do you know that you have generated it?!?!

      I would greatly appreciate any help or suggestions with respect to this problem, as I really need to make this working...

      Thank you very much in advance!

        Here's all I do:

        $fdf_file="CMS".$focus->caseid.".fdf";
        $pdf_file="CMS".$focus->caseid.".pdf";
        $fdf_dir=".\\pdfFiles\\";
        exec('pdftk cms1500.pdf fill_form '.$fdf_dir.$fdf_file.' output '.$fdf_dir.$pdf_file.' flatten');
        exec('del '.$fdf_dir.$fdf_file);

        The only way I got it working was to make sure that my PDF form file, pdftk.exe and libiconv2.dll are in the same directory as the file calling these commands (which in my case is index.php in the root directory) and this works like a charm. Where do you have your files located?

          Thank you for your answer!

          I have two questions - what does the 'CMS' stand for? And how do you actually understand that the pdf file has been generated, i mean, is it being saved somewhere or is it loading immediately?

          I have created a directory in the root and all the files are there....

            Sorry. The "CMS" is just part of our naming convention for the PDF files that we're generating.

            I know by two reasons. First, I browse (through Windows Explorer) to the directory that I defined where I want my PDF's to be saved to. This is specified by the "$fdf_dir.$pdf_file" part of my exec() command.

            Secondly, I call the following, so after the file is created, it's then opened up:

            header('Content-type: application/pdf');
            header("Content-Disposition: attachment; filename=".$fdf_dir.$pdf_file);
            ob_clean();
            flush();
            readfile($fdf_dir.$pdf_file);
            

            Hope that helps.

              5 days later

              Thank you again for your reply!

              It was useful. However, i am still having problems.
              Therefore, i have some questions:
              1. These headers - do they have to be in the same php file or should they be in a separate one.
              2. I have them in a separate one. When i am calling this php file in the browser it asks me to save the php file, which is pdf type. I am doing so, but when i try to open it with adobe, it says that it can't be opened. I guess, because it is supposed to be an attachment for a mail...
              3. The last question is - does it matter which version of adobe i've used to create the initial pdf template, as i have read in some places that the pdftk cannot manipulate with pdf files generated with versions higher than 7...

              I would greatly appreciate your response. Thank you in advance!!!!!!
              And wish a very nice day or night...

                1) Mine are in the same file and come immediately after the two exec() commands that I have.

                2) If you browse out to where you're saving the PDF, do you see that a PDF has been generated? I'm trying to see if this is an issue with creating the PDF versus being able to open it.

                3) Not sure. I don't know how the document was first created (was downloaded off the internet somewhere). We have Adobe Acrobat 8 Professional (version 8.1.3) installed. Don't know if it makes a difference, but I didn't have any problems with it.

                  Now it seems kind of working because after clicking on the submit button of my form, it shows a page where it says that the fdf has bee successfully created and also ..., well, i suppose is the pdf file but i guess there is some problem with the coding, as it looks like this:
                  %PDF-1.5 %&#65533;&#65533;&#65533;&#65533; 1 0 obj <<>>> endobj 2 0 obj <> endobj 3 0 obj <<<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 595.32 841.92] /Contents 4 0 R/Group<>/Tabs/S/StructParents 0>> endobj 4 0 obj <> stream x&#65533;&#851;K&#65533;&#65533;0&#65533;&#65533;&#65533;&#65533;&#65533;LI
                  and goes on..

                  Oh, and it gives me 2 errors that the headers have already been created, so it can't modify them...
                  I searched for this error and i found that it might be problem with empty spaces, but this is not the case...

                  Do you have any suggestions?

                  (and just to mention it again - i really appreciate the time your are spending on this case!!!!)

                    Oh, πŸ™‚ it does generates the pdf file!!!! πŸ™‚
                    However, it is empty.... πŸ™

                      Yeah, I had that problem too with the createFDF.php file. Here's what I have in that file now:

                      function createFDF($file,$info){
                          $data="%FDF-1.2\n%&#65533;&#65533;&#65533;&#65533;\n1 0 obj\n<< \n/FDF << /Fields [ ";
                          foreach($info as $field => $val){
                          	if(is_array($val)){
                              	$data.='<</T('.$field.')/V[';
                              	foreach($val as $opt)
                              		$data.='('.trim($opt).')';
                              	$data.=']>>';
                          	}else{
                              	$data.='<</T('.$field.')/V('.trim($val).')>>';
                          	}
                          }
                          $data.="] \n/F (".$file.") /ID [ <".md5(time()).">\n] >>".
                              " \n>> \nendobj\ntrailer\n".
                              "<<\n/Root 1 0 R \n\n>>\n%%EOF\n";
                          return $data;
                      }
                      

                      Is the FDF file that gets generated blank too?

                      edit: it's making those weird symbols for me too. i just replaced each of those funny characters with (I'm writing out the text because the characters get translated):

                      AmpersandPound65533Semicolon

                        I changed it the way you have done it, but still nothing. However, my fdf is not empty.....

                          And one more thing, in this line:

                          exec('del '.$fdf_dir.$fdf_file);

                          what does the 'del' stand for?

                            Hmmm. That is weird. What does your exec() line for the pdftk tool look like?

                            That second exec() call (with the "del" command) just deletes the fdf file once I get my pdf (since I have no use for the fdf file).

                              Additionally, make sure that your web server has rights to write files to your root directory (or wherever you're writing the PDF to).

                                2 years later

                                I have the same problems, when I implement code for converting fdf to pdf, the Result from my form become empty. Can i get your submit_form.php file? I really need it for my project and stuck at the same things like evelkova here

                                  Sure. I can send you what I have. It's on my work computer though, so I won't be able to get it until Monday. I'll post it then.

                                  Actually, I just checked. All the code for this is on the first page of this post. There's nothing else that I did to generate the PDF.

                                  Are you not getting a PDF, or are you getting a blank PDF? Does the FDF have your info in it?

                                    I have generate the fdf file using this resource http://koivi.com/fill-pdf-form-fields/,
                                    and I can open the file and the result is as I expected to be.
                                    But when I implemented the pdftk for converting the fdf to pdf, I cant open the file,
                                    and also when I using this code

                                    header('Content-type: application/pdf');
                                    header("Content-Disposition: attachment; filename=".$fdf_dir.$pdf_file);
                                    ob_clean();
                                    flush();
                                    readfile($fdf_dir.$pdf_file);

                                    The filename that it open not like it should be, It opened something else, There are possibilities that It because I'm wrong at pointing the file.
                                    But still, The pdf file can't be opened using acrobat, reader, or web browser

                                      narunaru;11007111 wrote:

                                      header('Content-type: application/pdf');
                                      header("Content-Disposition: attachment; filename=".$fdf_dir.$pdf_file);
                                      ob_clean();
                                      flush();
                                      readfile($fdf_dir.$pdf_file);

                                      The filename that it open not like it should be, It opened something else, There are possibilities that It because I'm wrong at pointing the file.
                                      But still, The pdf file can't be opened using acrobat, reader, or web browser

                                      So at least there is a PDF file being generated, correct? And you're calling something similar to the following, correct:

                                      exec('pdftk cms1500.pdf fill_form '.$fdf_dir.$fdf_file.' output '.$pdf_dir.$pdf_file.' flatten');

                                        yes, I use this code

                                         exec('pdftk cms1500.pdf fill_form '.$fdf_dir.$fdf_file.' output '.$pdf_dir.$pdf_file.' flatten');

                                        for converting the fdf to pdf like you do above, but the file can't be opened

                                          Write a Reply...