How to manipulate PDF files having scripts? - php

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

Related

PHP: Fill PDF form

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).

Pimcore how to create PDF and send via email

Anybody can give us a hint how to treat this best, if this can be achieved with „standards“ of pimcore?
Render a PDF with dynamic content and merge it with a PDF which has legal information.
Then send final pdf via email.
The PDF should be able to designed with HTML
You can use the Web2Print Documents to create and render the first PDF with dynamic content. To modify the PDF afterwards, you can hook into the PDF generation process (Pimcore 4 example):
\Pimcore::getEventManager()->attach("document.print.postPdfGeneration", function (\Zend_EventManager_Event $e) {
$document = $e->getTarget();
$pdf = $e->getParam("pdf");
Once you got that PDF, you can merge it with other PDFs like described here: Merge PDF files with PHP
Sending it via email should be easy at this point.

FPDF and PHP: Help merging PDF forms with text fields without losing text data?

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();

PHP not auto-populating data in PDF

I've been using the advice given in this question:
Merge FDF data into a PDF file using PHP
to determine how I can pre-populate a pdf form with data pulled from a database but not getting the data to file. I'm running php 5.3.15 on Mountain Lion and I'm using the following link to combine the pdf with the fdf on a table page that I'd like to allow the user to click and take them to a populated pdf file:
something.pdf#FDF=generatePdf.php
The generatePdf.php looks like this:
<?php
$pdf_form_url= "";
require_once('forge_fdf.php');
$fdf_data_names= array(); // none of these in this example
$fdf_data_strings= array(); // none of these in this example
$fdf_data_strings['form1[0].#subform[0].q1a_LastName'] = "Smith";
$fields_hidden= array();
$fields_readonly= array();
// set this to retry the previous state
$retry_b= false;
header( 'content-type: application/vnd.fdf' );
echo forge_fdf( $pdf_form_url,
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly );
?>
and the FDF file looks like this when I run it through the pdftk command line:
FieldType: Text
FieldName: form1[0].#subform[0].q1a_LastName[0]
FieldNameAlt: Enter Family Name
FieldFlags: 0
FieldJustification: Left
FieldMaxLength: 35
Still, when I run it the form doesn't auto populate. I've tried many variations including just using the q1a_LastName by itself and I still end up with an empty form. The forge_fdf.php is in the same directory.
Update:
I've also attempted the conventional method outlined in the Merge FDF data into a PDF question that I've linked to using PHP, meaning I didn't just direct the user to a pdf file but also attmepted to have the user download the pdf. In that particular case, I would receive a message saying the pdf was empty. I've checked the created raw FDF file and it appears to be populating with data. In either case, I'm not getting what I need.
After much trial and error I realized that the best way to autopopulate to a url linking to a pdf is to define the $pdf_form_url variable for the code above. This code will echo out the details of the fdf file. If you explicitly specify what the pdf url is, php and your browser will take the necessary steps to link you to a the pdf in your web browser with the embedded field information coming from the fdf output.
So I stumbled upon this thread and couldn't get it to work even when I populate the $pdf_form_url variable. Here's my code:
require_once('forge_fdf.php');
// leave this blank if we're associating the FDF w/ the PDF via URL
$pdf_form_url= "http://test.pdf";
// $pdf_form_url= "";
// default data; these two arrays must ultimately list all of the fields
// you desire to alter, even if you just want to set the 'hidden' flag;
//
//
$fdf_data_names= array(); // none of these in this example
$fdf_data_strings= array(); // none of these in this example
$fdf_data_strings['Address, Row 1']="XXXX Balboa Arms Drive #XXX, San Diego CA 01234";
$fields_hidden= array();
$fields_readonly= array();
// set this to retry the previous state
$retry_b= false;
header( 'content-type: application/vnd.fdf' );
echo forge_fdf( $pdf_form_url,
$fdf_data_strings,
$fdf_data_names,
$fields_hidden,
$fields_readonly );
And here's the form field I want to update using pdftk dump_data_fields:
FieldType: Text
FieldName: Address, Row 1
FieldNameAlt: (Address, <Row 1>)
FieldFlags: 0
FieldJustification: Center

Populating PDF form fields with PHP

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

Categories