Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting checkboxes and radiobuttons with FPDM in PDF form

Tags:

checkbox

php

pdf

I'm trying to fill out a PDF form through FPDM, like in this example: http://www.fpdf.org/en/script/script93.php

Filling out text, date, decimal, ... inputs works correctly, but I can't find out how to check checkboxes and grouped checkboxes (radio buttons). This are two problems:

  1. Checkboxes have a regular name, like "mycheck1". Setting the value in the PHP array to true or or "true" or "1" or 1 or "checked" or "on" or "yes" doesn't activate the checkboxes (case-sensitively tested too). Which value do I have to set to activate checkboxes?
  2. Grouped checkboxes (radio buttons) cannot even be found - the script throws the error "FPDF-Merge Error: field yourtype not found". What is the correct name? Adobe Acrobat Pro DC shows the name "yourtype" as the name for all checkboxes in the group. In the field-list it shows the grouped checkboxes as "yourtype#0", "yourtype#1", "yourtype#2", etc. But the script doesn't seem to be able to find any of this names. I also tried something like "yourtype[1]" without luck. And as soon as I can access them, do I have to set the value for one of them, or do I assign the (integer?) value just directly?

My example:

<?php
    require('fpdm.php');

    $fields = array(
    'forname' => 'Jon Dow',  // text input: works
    'mycheck1' => true,  // checkbox: doesn't work! Also tried "true", 1, "1", "yes", "on", ... (with case-sensitive)
    'yourtype' => true,  // grouped checkbox (radio button): doesn't even find input with that name.
    );

    $pdf = new FPDM('template.pdf');
    $pdf->Load($fields, true);
    $pdf->Merge();
    $pdf->Output();
?>

Edit: The FPDM class does not seem to support setting some types like checkboxes and dropdowns. In the file fpdm.php says the following in the header unter the ToDo section: extends filling to another form fields types (checkboxes,combos..)

like image 324
wizard Avatar asked May 03 '16 13:05

wizard


1 Answers

As you mentioned it's not supported in fpdm. I was looking for another library that allow doing this and I found this : php-pdftk

Code sample :

// Fill form with data array
$pdf = new Pdf('doc.pdf');
$pdf->fillForm([
        'CheckBox1' =>'Yes',
        'RadioButton'=>'Choice1',
        'name'=>'Wizard'
    ])
    ->needAppearances()
    ->saveAs('doc_filled.pdf');
like image 143
SwissFr Avatar answered Sep 23 '22 10:09

SwissFr