Search code examples
phppdfencodingfpdfpdftk

FPDF FPDM - Merge Error due to PDFTK


firstly :It's my first Question here then sry if the code synthax isn't easy to read, Don't find a way to make it clean.

I'm trying to fill my big PDF template, which I made with Adobe Creator.

FPDM works fine with the template.php which is include in the fpdm lib but this error appear when I change the PDF template to mine, after been pass throught pdftk.

FPDF-Merge Error: Cannot find current Value (/v) for annotation object 288, Something goes wrong during parsing (uncompress your pdf and retry) or your pdf is corrupted.

I don't find any subject about this issue on the internet.

The error comes from my PDF Template, cause of his encoded way. I saw after opening my PDF in my Text Editor that the object 288 (and others) haven't got /v attribute, as the error suggest. Then I suppose that the encode way isn't the one which are waiting for by FPDM. Before to use pdftk it the error was : "PDF is empty" something like that.


    define('DRUPAL_ROOT', getcwd());

    include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    // #1 shouldn't be necessary
    require_once 'fpdf/src/fpdf.php';
    require_once 'fpdf/examples/form_filling/fpdm.php';

    // To be used after test win
    $sid = '58';
    $node = new stdClass();
    $node->nid = '270'; 
    $temp_submission = webform_menu_submission_load($sid, $node->nid);

    // NOM is the first field id of my Template PDF.
    $fields = array(
        'NOM' => 'My name'
    );

    $pdf = new FPDM('Dossier_de_candidature_2ndaire_2014_adaptev1_fpdf.pdf');
    $pdf->Load($fields, TRUE);
    $pdf->Merge();
    $pdf->Output('formulaire.pdf', 'D');

?>

Thanks you !


Solution

  • And I WIN !

    I finally chose to forget FPDF and directly make my own system, which is simply what FPDF do I think. Don't worry that isn't difficult at all.

    Have a look on this website, just understand how work fdf to fill pdf template (mapping) and.. do it :)

    http://doc.ubuntu-fr.org/pdftk

    STEP 1 : Create a FDF content in PhP

    /* A FDF content empy */
    $xfdf_head = '<?xml version="1.0" encoding="UTF-8"?><xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><fields>';
    $xml_data = '';
    $xfdf_end = '</fields></xfdf>';
    
    /* Generate all fields with field_key form webform and value form submission */
    foreach ($webform['#node']->webform['components'] as $key => $value) {
    
        if ($webform['#submission']->data[$key]['value'][0]){
            $valeur = $webform['#submission']->data[$key]['value'][0];
        } else {
            $valeur = '';
        }
    
        $xml_data .= '
            <field name="'.$webform['#node']->webform['components'][$key]['form_key'].'">
                <value>'.$valeur.'</value>
            </field>';
    }
    
    
    $FDF_content = $xfdf_head.$xml_data.$xfdf_end;
    

    STEP 2 : Create a FDF FILE and write your content in it

    $FDF_file = fopen('new_fdf.fdf', 'w');
    fwrite($FDF_file, $FDF_content);
    fclose($FDF_file);
    

    STEP 3 : Use PDFTK command in Php (check website upside) [fill these 2 var before..]

    $pdftk_command = '/usr/bin/pdftk '. $pdf_template .' fill_form new_fdf.fdf output '.$new_fillup_pdf;
    shell_exec($pdftk_command);
    

    STEP 4 : Force download or Open the new PDF in PHP. (I let you find how, that's easy)