Privacy Policy Compila moduli pdf da form php - Daniele Carnovale

PHP17 Novembre 2015by Daniele Carnovale10Compila moduli Pdf da form php

Guida per la compilazione dei moduli di un file pdf in php senza l’ausilio di librerie esterne.


Macchina linux (Ubuntu 14.04 + LAMP)

se non presente installare il seguente software

sudo apt-get install pdftk

Immaginiamo di ricevere i campi da compilare da un form HTML in post.

Per ‘compilare’ il pdf da php, basta generare un file xfdf che servirà da input a pdftk per compilare i campi del pdf.

Per fare questo usiamo la  funzione createXFDF

E’ molto semplice da fare, basta passare alla funzione l’url del file
e un array associativo key:value.



———————————– file submit.php —————————————

// (Compila  moduli pdf da form php  )

// dati ricevuti da form html


// creo array con key:value

// da notare che la chiave deve identificare il textfield compilabile all'interno del file pdf

// se non si conoscono le key del file pdf basta eseguire il comando

//  pdftk modulo.pdf dump_data_fields

$data = array (
'Testo1' => $azienda,
'Testo2' => $citta,
'Testo3' => $via,
'Testo4' => $piva,
'Testo5' => $nome.'  '.$cognome

$pdf_file_url = 'http://localhost/registrazione/modulo.pdf';

//creo il file XFDF

$xfdf = createXFDF( $pdf_file_url, $data );

$result_directory = dirname(__FILE__) . '';
$xfdf_file = time() . '.xfdf';
$xfdf_file_path = $result_directory . '/' . $xfdf_file;

// scrivo il file XFDF

if( $fp = fopen( $xfdf_file_path, 'w' ) )
fwrite( $fp, $xfdf, strlen( $xfdf ) );

// path interno dello stesso file ($pdf_file_url = 'http://localhost/registrazione/modulo.pdf';)
$pdf_template_path = dirname( __FILE__ ) . '/modulo.pdf';
$pdftk = '/usr/bin/pdftk';
$pdf_name = substr( $xfdf_file_path, 0, -4 ) . 'pdf';
$command = "$pdftk $pdf_template_path fill_form $xfdf_file_path output $pdf_name flatten";

exec( $command, $output, $ret );

echo 'file compilato generato = '.$pdf_name;

function createXFDF( $file, $info, $enc='UTF-8' )
$data = '<?xml version="1.0" encoding="'.$enc.'"?>' . "\n" .
'<xfdf xmlns="" xml:space="preserve">' . "\n" .
'<fields>' . "\n";
foreach( $info as $field => $val )
$data .= '<field name="' . $field . '">' . "\n";
if( is_array( $val ) )
foreach( $val as $opt )
$data .= '<value>' .
htmlentities( $opt, ENT_COMPAT, $enc ) .
'</value>' . "\n";
$data .= '<value>' .
htmlentities( $val, ENT_COMPAT, $enc ) .
'</value>' . "\n";
$data .= '</field>' . "\n";
$data .= '</fields>' . "\n" .'<ids original="' . md5( $file ) . '" modified="' .time() . '" />' . "\n" .
'<f href="' . $file . '" />' . "\n" .'</xfdf>' . "\n";
return $data;

———————————- end submit.php —————————————

Le mie Considerazioni

Il file di esempio “submit.php” per la compilazione di moduli pdf da form è funzionante se rispettato l’ambiente di sviluppo.
Di fondamentale importanza è la libreria pdftk. Consiglio di installarla su ambiente Linux, l’ho testata su molte distribuzioni Debian e non ha problemi. Sconsiglio vivamente di provare il tutto su ambiente Windows o Mac, comandi e istruzioni potrebbero essere molto diversi.


  • konto internetowe

    14 Gennaio 2021 at 2:12

    I like the helpful info you provide in your articles.
    I’ll bookmark your weblog and check again here frequently.

    I am quite certain I’ll learn lots of new stuff right here!
    Good luck for the next!


  • top adult cam sites

    14 Gennaio 2021 at 4:51

    Thanks for one’s marvelous posting! I truly enjoyed reading it, you are a great author.

    I will ensure that I bookmark your blog and will eventually come back in the
    foreseeable future. I want to encourage one to continue your great posts, have a nice weekend!


  • Najlepsze Konto W Banku

    14 Gennaio 2021 at 7:18

    I have read a few good stuff here. Definitely worth bookmarking for
    revisiting. I surprise how much effort you put to create the sort of excellent informative website.


  • live adult cam

    14 Gennaio 2021 at 8:01

    Wow, wonderful blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your site is magnificent, as well as
    the content!


  • zovre lioptor

    16 Giugno 2021 at 17:33

    Greetings! Very helpful advice on this article! It is the little changes that make the biggest changes. Thanks a lot for sharing!


  • zortilonrel

    6 Luglio 2021 at 9:00

    Awesome blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your theme. Kudos


  • zortilonrel

    25 Luglio 2021 at 5:30

    After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.


  • MephaSalipax

    17 Settembre 2021 at 22:24

    Excellent blog here! Also your website rather a lot up fast! What host are you the use of? Can I get your associate hyperlink to your host? I wish my web site loaded up as fast as yours lol


Leave a Reply

Your email address will not be published. Required fields are marked *