Pdftk edit and fill forms

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)

Please guide me through this.

Hi gaurissl22 welcome to the forum

I’m a bit confused, you’re using PHP and Java together?

I use Java for desktop apps using the CLI, and though AFAIK Java and PHP can work together, I’ve never bothered to try and use PHP for PHP

Are you sure you’ve set up everything correctly?

Hello,

Thanks for your response.
No, Im not using java.

Following is my code:

<?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?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.