tower
tower

Reputation: 81

PHP import PDF form and output PDF form

I have a fillable PDF form, I need to write some numbers on PDF using PHP (not necessarily into form fields, but just on PDF) and then output PDF again with all fillable fields that original document had.

FPDI and FPDF are doing the job, but PDF fillable fields will be lost during the process.

Here is suggested mpdf, most of mpdf examples I found are about making new PDF files from HTML. There is mpdf manual, but I'm not sure what functions should solve my problem.

Code below does the job, but as said all the PDF form fields will dissapear.

include('FPDF-master/fpdf.php'); 
include('FPDI1_6_1/fpdi.php');
//include('mpdf60/mpdf.php');

$pdf = new FPDI();

$pdf->AddPage(); 
$pdf->setSourceFile('myfillableform.pdf'); 
$tplIdx = $pdf->importPage(1); 
$pdf->useTemplate($tplIdx, 0, 0); 

$pdf->SetFont('Times', 'B'); 
$pdf->SetTextColor(0,0,0); 
$pdf->SetXY(140, 45);
$pdf->Write(0, $someNumber); 
$pdf->Output("newfillableform_with_number.pdf", 'F'); 

Upvotes: 2

Views: 3873

Answers (2)

tower
tower

Reputation: 81

I managed to solve my problem using FPDM. FPDM allows to fill in PDF form fields (including read only fields).

So I just added another field to my PDF document with Acrobat Pro, made that field read only and using FPDM I can write my number or text into that read only field, can output PDF and all the other fields in my document remain fillable as I needed.

require('fpdm/fpdm.php');
$fields = array(
    'field'    => "$someNumber"
);
$pdf = new FPDM('myfillableform.pdf');
$pdf->Load($fields, false);
$pdf->Merge();
$pdf->Output("newfillableform_with_number.pdf", 'F'); 

Upvotes: 6

Jan Slabon
Jan Slabon

Reputation: 5058

FPDI cannot handle any dynamic content such as form fields or any other annotation as documented in its FAQ.

If you want to add new content to an existing PDF you should take a look at the SetaPDF-Stamper component (not free!).

Upvotes: 1

Related Questions