I have a PDF document which contains form with couple of form fields. I need to do following:
Load that PDF document
Itearate through all the form fields of that document and fill it up with the data
Download filled flatten document
I have seen few of libraries, but most of them are relying on the PDFtk. But the case is that I don't have the possibility to install PDFtk on the server.
Is there any library which allows such action and is "pure" php so I don't have to install anything on server?
You can use the SetaPDF-FormFiller component for this task (not free! And I'm related to the company behind). Let $data be your array of data, a simple usage example would be:
// Load that PDF document
$document = SetaPDF_Core_Document::loadByFilename('path/to.pdf');
$formFiller = new SetaPDF_FormFiller($document);
// Itearate through all the form fields of that document and fill it up with the data
$fields = $formFiller->getFields();
foreach ($fields->getNames() as $fieldName) {
$field = $fields->get($fieldName);
$field->setValue($data[$fieldName]);
}
// flatten document
$fields->flatten();
// Download filled flatten document
$writer = new SetaPDF_Core_Writer_Http('result.pdf');
$document->setWriter($writer);
$document->save()->finish();
It is all done in pure PHP and requires only common PHP extensions to be enabled (see system requirements).
Related
We have a project where we merge different pdfs to create a catalog.
Right now it's running on myokyawhtun/pdfmerger, which runs fine, but it does not keep links set in acrobat.
We have tried different libraries we found (pure PHP, we cannot install or call applications from the command line via shell-exec or similar on this webspace, so no gs), even if we just import the pdf-files via fpdi and resave them, the hyperlinks get lost.
Is there any (pure PHP) library out there which can retain links inside the files? Or are there some special settings that we missed?
We have tried:
setasign/fpdi
iio/libmergepdf
jurosh/pdf-merge
Example code for the current lib (myokyawhtun/pdfmerger):
require('vendor/myokyawhtun/pdfmerger/tcpdf/tcpdf.php');
require('vendor/myokyawhtun/pdfmerger/tcpdf/tcpdi.php');
require('vendor/myokyawhtun/pdfmerger/PDFMerger.php');
$pdf = new \PDFMerger\PDFMerger;
foreach($sourcePdfs as $file)
{
$pdf->addPDF($pdfDir.'/source/'.$file);
}
$pdf->merge('download', 'Download.pdf');
All the mentioned libraries use FPDI under the hood, which simply does not support content outside of a pages content stream, such as links or any other annotation type.
We (author of FPDI) also offer non-free products which work on another level and which allow you keep all annotations including links and also forms when you concatenate the documents. This is possible with the SetaPDF-Merger component:
$merger = new SetaPDF_Merger();
foreach($sourcePdfs as $file) {
$merger->addFile($pdfDir . '/source/' . $file);
}
$merger->merge();
$document = $merger->getDocument();
$document->setWriter(new SetaPDF_Core_Writer_Http('Download.pdf'));
$document->save()->finish();
I have a PDF file with a form where you can enter some data and everything will be recalculated. The file has also some text fields I want to prefill with my own data from PHP.
I played with PDFtk. Unfortunately it is not working on my hosting provider and I can't install it there. And also even if it works, the scripting functionality of that form is gone after saving by PDFtk.
Do you know any good alternative to PDFtk or a method how to edit PDF files without loosing the embedded scripts?
The SetaPDF-FormFiller component (a non-free product of us) allows you to fill in PDF forms and also XFA forms with PHP:
// create a document instance
$writer = new SetaPDF_Core_Writer_Http('filled-form.pdf');
$document = SetaPDF_Core_Document::loadByFilename('form.pdf', $writer);
// create a form filler instance
$formFiller = new SetaPDF_FormFiller($document);
// get access to the fields
$fields = $formFiller->getFields();
// fill in the fields
$fields['name']->setValue('blackhatmario');
$fields['a']->setValue(123);
$fields['b']->setValue(321);
// ...
// save the filled document
$document->save()->finish();
In view to calculations in form fields it may be required to force the calculation at opening time by a simple document level javascript:
this.calculateNow(); // in Doc context
I have PDF form files that I fill out dynamically with PHP using FPDM (the FPDF script). I can save them on my server no problem, and the text all looks fine in the PDF when I download and view in Acrobat.
My problem is: I'm trying to merge multiple PDF files together on the server so the user can download a single PDF document with several pages. I downloaded PDF Merger (http://pdfmerger.codeplex.com/) and got it merging the files together, but this causes the PDF form text to disappear.
Anyone know of a form-friendly PHP-based PDF merger that doesn't require installing anything (other than uploading libraries) to my server?
Code that works for merging but kills text in form boxes:
$pdfCombined= new PDFMerger;
$pdfCombined->addPDF('../forms/generated/16.pdf', 'all')
->addPDF('../forms/generated/19.pdf', 'all')
->merge('browser', 'mergedDoc.pdf');
The linked "PDF Merger" simply uses FPDI in the back. FPDI is not able to handle dynamic content as described here.
A pure PHP solution for merging PDF forms is the SetaPDF-Merger component (not free). An evaluation requires the installation of a Loader (Ioncube or Zend Guard). License owners will get access to the source code, so that no external library is needed. The usage is also that easy:
require_once("library/SetaPDF/Autoload.php");
// create a file writer
$writer = new SetaPDF_Core_Writer_Http("mergedDoc.pdf");
// create a new merger instance
$merger = new SetaPDF_Merger();
// add the files
$merger->addFile('../forms/generated/16.pdf');
$merger->addFile('../forms/generated/19.pdf');
// merge all files
$merger->merge();
// get the resulting document and set the writer instance
$document = $merger->getDocument();
$document->setWriter($writer);
// save the file and finish the writer
$document->save()->finish();
I am building an application to help assist in setting up PDF forms for my site. I have standard forms that I use, but need to change a few hidden fields in the PDF file each time I add the standard form to a new account.
Using PHP how can I update the value of a field in a PDF file, then save the file?
I am assuming I need to use some sort of PHP-PDF library, so a FREE one would be helpful.
(The forms are already programmed with form fields using Adobe Acrobat and have unique field names. All I need to do is update a couple of the existing fields using the field name as a key)
Example-
PDF File Location = www.mysite.com/accounts/john_smith/form.pdf
PDF Field to update (Field Name) = "account_directory"
PDF Field value to be set = "john_smith"
This is clunky but it gets the job done:
Use createXFDF.php and then use PDFTK twice.
Make sure your template pdf has extended features in Adobe Reader.
Create an XFDF file with the data you will use to populate the PDF - remember that field names in pdf must match those in the xfdf. The data is an array of field name, field value.
Use PDFTK with your template pdf and the XFDF to create a new intermediate PDF. Do NOT use FLATTEN - that will remove editability.
Now use PDFTK cat with your new intermediate PDF to create your final PDF - it somehow gets rid of the Adobe digital signature (Keep a pdf form editable after filling it with pdftk - see Marco's answer).
Sample code:
$data = array();
$field_name = "account_directory";
$field_value = "john_smith";
$data[$field_name] = $field_value;
/* Make $data as big as you need with as many field names/values
as you need to populate your pdf */
$pdf_template_url = 'http://yoursite.com/yourpath/yourtemplate.pdf';
include 'createXFDF.php';
$xfdf = createXFDF( $pdf_template_url, $data );
/* Set up the temporary file name for xfdf */
$filename = "temp_file.xfdf";
/* Create and write the XFDF for this application */
$directory = $_SERVER['DOCUMENT_ROOT']."/path_to_temp_files/";
$xfdf_file_path = $directory.$filename ; /* needed for PDFTK */
// Open the temp xfdf file and erase the contents if any
$fp = fopen($directory.$filename, "w");
// Write the data to the file
fwrite($fp, $xfdf);
// Close the xfdf file
fclose($fp);
/* Write the pdf for this application - Temporary, then Final */
$pdf_template_path = '/yourpath/yourtemplate.pdf';
$pdftk = '/path/to/pdftk'; /* location of PDFTK */
$temp_pdf_file_path = substr( $xfdf_file_path, 0, -4 ) . 'pdf';
$command = "$pdftk $pdf_template_path fill_form $xfdf_file_path output $temp_pdf_file_path";
/* Note that the created file is NOT flattened so that recipient
can edit form - but this is not enough to allow edit with
Adobe Reader: will also need to remove the signature with PDFTK cat */
exec( $command, $output, $ret );
/* Workaround to get editable final pdf */
$pdf_path_final = $directory. "your_final_filename.pdf" ;
$command2 = "$pdftk $temp_pdf_file_path cat output $pdf_path_final";
exec( $command2, $output2, $ret2 );
/* Your pdf is now saved
/* Remember to UNLINK any files you don't want to save - the .xfdf and the temporary pdf */
I am converting a classic ASP application to PHP.
In the Classic ASP app, we are programatically populating fields that have been created in PDF documents using a component called ASPpdf.
I need to reproduce this behavior in PHP, but need to know if PHP can populate PDF fields on its own or if any third party plug in is needed.
Is this functionlity posible in PHP with or without a plug in?
Thanks.
Note: I already have the PDFs created, I do not need to create the actual PDF. I need to grab a preexisting PDF with form fields, populate those form fields and then save that custom PDF.
This is the first google search result I got, is there a reason this doesn't work for you?
http://koivi.com/fill-pdf-form-fields/tutorial.php
UPDATE
After reading a little further, this generates an FDF (which Acrobat can read). To generate an actual PDF you'll need to use this: http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
The resolution to my problem was to use the same COM component I used in Classic ASP in my new PHP apps. The COM object give me tremendous control over PDF documents.
The component I use is AspPdf
<?php
$pdf = new COM("Persits.Pdf") or die("Unable to instantiate Word");
$pdfdoc = $pdf->OpenDocument( "pdf.pdf" );
$pdffont = $pdfdoc->Fonts("Helvetica-Bold");
$pdfdoc->Form->RemoveXFA();
$pdffield = $pdfdoc->Form->FindField("FirstName");
$pdffield->SetFieldValue("PHP Text", $pdffont);
$pdffile = $pdfdoc->save( "php.pdf" , false);
echo $pdf->version;
$pdf = null;
?>
It looks as though the pdftk PDF toolkit may be able to do this. Reading the manual:
fill_form < FDF data filename | XFDF data filename | - | PROMPT >
Fills the single input PDF’s form fields with the data from an FDF file, XFDF file or stdin. Enter the data filename after fill_form, or use - to pass the data via stdin, like so:
pdftk form.pdf fill_form data.fdf output form.filled.pdf