codeshell / fpdm

PDF form filling using FPDM Class written by FPDF author Olivier (+multifile-filling-fix, +checkboxes)
MIT License
117 stars 66 forks source link

Checkbox not working? #28

Open shuaixieca opened 4 years ago

shuaixieca commented 4 years ago

Hi, I tried the new feature for checkbox, but it is not working in my local laptop, here is my code below, I didn't get error and other text boxes are working, could someone let me know possible issue for me?

require_once '../fpdm-2.9.2/fpdm.php';

$fields = array( 'name' => 'My name', 'address' => 'My address', 'city' => 'My city', 'phone' => 'My phone number', 'checkbox1' => 'Yes', );

$fileName='template2';//test.pdf $pdf = new FPDM('../forms/IR/'.$fileName.'.pdf'); $pdf->useCheckboxParser = true; $pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8 $pdf->Merge();

$output="corp_IR.pdf"; $pdf->Output('I',$output);

MelvinNau commented 4 years ago

Same issue here.

Text for field is ok. But checkbox doesn't work

Robszyy commented 4 years ago

Did you tried like this ?

$fields = array( 'name' => 'My name', 'address' => 'My address', 'city' => 'My city', 'phone' => 'My phone number', 'checkbox1' => true );

It works fine for me.

nusamata commented 4 years ago

Did you tried like this ?

$fields = array( 'name' => 'My name', 'address' => 'My address', 'city' => 'My city', 'phone' => 'My phone number', 'checkbox1' => true );

It works fine for me.

Is you setting in properties checkbox?

asphub commented 2 years ago

Any progress on this issue. Still facing issues with checkboxes

Amal-p commented 2 years ago

any possible solution this issue. still now 2022

Amal-p commented 2 years ago

Did you tried like this ? $fields = array( 'name' => 'My name', 'address' => 'My address', 'city' => 'My city', 'phone' => 'My phone number', 'checkbox1' => true ); It works fine for me.

Is you setting in properties checkbox?

not work in my case

msiemaszko commented 2 years ago

Hi, I have the same issue. I dig into the code and see that parser 'parsePDFEntries' is not finding the 'checkbox_yes' and 'checkbox_no' values, or they are simply not in my PDF file. My PDF file was generated with AcrobarReaderPro and treated with pdftk. How to properly prepare a pdf file to correctly fill checkbox fields via fpdm?

GiovanniMounir commented 2 years ago

I had the same issue; turns out that the checkbox_no and checkbox_yes infos were not populated, which are used by FPDM to detect the right values for on/off states. I harcoded the states to Off and Yes to workaround the issue.

This the modified set_field_checkbox function:

fpdm/src/fpdm.php line 940

public function set_field_checkbox($name, $value)
{
    /* START HARDCODE THE checkbox_no AND checkbox_yes VALUES  */
    if (!isset($this->value_entries["$name"]["infos"]["checkbox_no"])) $this->value_entries["$name"]["infos"]["checkbox_no"] = "Off";
    if (!isset($this->value_entries["$name"]["infos"]["checkbox_yes"])) $this->value_entries["$name"]["infos"]["checkbox_yes"] = "Yes";
    /* END HARDCODE THE checkbox_no AND checkbox_yes VALUES  */
    //------------------------------------
    $offset_shift=0;
    $verbose_set=($this->verbose&&($this->verbose_level>1));
    //Get the line(s) of the misc field values
    if (isset($this->value_entries["$name"])) {
        if (isset($this->value_entries["$name"]["infos"]["checkbox_state_line"])
        && isset($this->value_entries["$name"]["infos"]["checkbox_no"])
        && isset($this->value_entries["$name"]["infos"]["checkbox_yes"])) {
            $field_checkbox_line=$this->value_entries["$name"]["infos"]["checkbox_state_line"];
            if ($field_checkbox_line) {
                if ($verbose_set) {
                    echo "<br>Change checkbox of the field $name at line $field_checkbox_line to value [$value]";
                }
                $state = $this->value_entries["$name"]["infos"]["checkbox_no"];
                if ($value) {
                    $state = $this->value_entries["$name"]["infos"]["checkbox_yes"];
                }
                $CurLine =$this->pdf_entries[$field_checkbox_line];
                $OldLen=strlen($CurLine);
                $CurLine = '/AS /'.$state;
                $NewLen=strlen($CurLine);
                $Shift=$NewLen-$OldLen;
                $this->shift=$this->shift+$Shift;
                //Saves
                $this->pdf_entries[$field_checkbox_line]=$CurLine;
                return $Shift;
            // $offset_shift=$this->_set_field_value($field_checkbox_line, $state);
            } else {
                if ($verbose_set) {
                    echo "<br>Change checkbox value aborted, parsed checkbox definition incomplete.";
                }
            }
        } else {
            if ($verbose_set) {
                echo "<br>Change checkbox value aborted, the field $name has no checkbox definition.";
            }
        }
    } else {
        $this->Error("set_field_checkbox failed as the field $name does not exist");
    }
    return $offset_shift;
}

The function will afterwards use the hardcoded checkbox_yes value ("Yes") when a value is provided, or checkbox_no ("Off") in case there is no value provided (i.e empty, false, etc)

Example usage:

$fields = array(
"CHECKBOX_FIELD_NAME" => true //or any value that is not empty/false/0/etc
);
$fpdm = new FPDM('fillable_pdf_example.pdf');
$fpdm->useCheckboxParser = true;
$fpdm->Load($fields, true);
$fpdm->Merge();
$fpdm->Output();