I have been working with pdftk to edit and fill pdf forms.
I have a problem with filling the data from a PHP array into the form.
Also I tried it using XFDF/FDF file, but still got an error.
In this when I send data using xfdf I get the following error -
Unhandled Java Exception in create_output(): java.lang.ClassCastException: pdftk.com.lowagie.text.pdf.PdfNull cannot be cast to pdftk.com.lowagie.text.pdf.PdfDictionary at 0x0059a84e (Unknown Source) at 0x0059ad42 (Unknown Source) at 0x005e9bd4 (Unknown Source) at 0x005ba4a4 (Unknown Source) at 0x005b2044 (Unknown Source) at 0x0059231e (Unknown Source) at 0x004723f1 (Unknown Source) at 0x00472045 (Unknown Source) at 0x004df3e2 (Unknown Source) at 0x004df38a (Unknown Source) at 0x00471e74 (Unknown Source)
<?php
require 'vendor/autoload.php';
use mikehaertl\pdftk\Pdf;
use mikehaertl\pdftk\FdfFile;
define("FORM_", "D:/wamp/www/pdftk/forms/");
$pdf = new Pdf(FORM_.'form.pdf', [
'command' => 'C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe',
'useExec' => true, // May help on Windows systems if execution fails
]);
$fields = array(
'Street_Address'=>'test address',
'Tax_ID' => 'test id'
);
$fdf = new FdfFile($fields);
$fdf->saveAs(FORM_.'data_fdf.fdf');
$pdf->fillForm(FORM_.'data_fdf.fdf')
->needAppearances()
->saveAs(FORM_.'filled.pdf');
if (!$pdf->saveAs('filled.pdf')) {
$error = $pdf->getError();
echo $error;
die;
}
$pdf = new Pdf(FORM_.'filled.pdf', [
'command' => 'C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe',
'useExec' => true, // May help on Windows systems if execution fails
]);
$pdf->send();
I have extracted the fields using " $pdf->getDataFields(); " and created an array named fields.
In this the data_fdf.fdf file get created.
This error is coming while saving the Filled.pdf. (echo $error)
Did I miss out something?
Or am I doing something wrong?