Formulaire en ligne interactif en PDF

Résolu/Fermé
xav - Modifié le 6 nov. 2007 à 21:22
 chteu - 8 oct. 2013 à 20:54
Bonjour,

Je cherche à créer tout comme cela existe sur certains sites un formulaire en PDF en ligne avec des champs interactif. Dans les champs les utilisateurs font leur saisie directement dans la page qui est au format PDF. Pouvez-vous éclairer ma lanterne afin de m'expliquer comment cela fonctionne? Merci d'avance.
A voir également:

18 réponses

bonjour,

Il est possible de creer un formulaire en pdf . avec des champs que l'on peut remplir.

Generalement l'option proposé dans ce cas est ensuite d'enregistrer ou d'imprimer le formulaire .

Par ailleurs , les fichiers pdf peuvent embarquer du javascript et donc de faire en sorte qu'un formulaire en pdf se voit doté d'un bouton envoyer. Le code javascript va se charger de recolter le nom et contenu des champs du formulaire et creer un lien vers une page web prédefinie a laquelle est ajouté les données du formulaire :
genre : http://monsite.com/getformpdf.php?champs1=nom&champs2=prenom
de l'autre coté il faut un script pour recuperer et traiter ces données passé dans l'url .

Pour creer ce formulaire pdf il vous faut un programme (généralement payant )capable de le faire et savoir s'en servir.

Pour ceux qui le php ne rebute pas , il y a des solutions gratuite de script qui permettent de generer des pdf en lignes , ils faut y integrer le javascript

voici un exemple de test personnel sur une page exemple de 'TCPDF' qui reprend un formulaire , auquel le javascript est ajouté (fin de code quasi), la page qui reçoit le formulaire est test.php dans le même repertoire (voir code du script de test réduit au minimum plus bas .

[code]
<?php
//============================================================+
// File name : example_014.php
// Begin : 2008-03-04
// Last Update : 2008-05-28
//
// Description : Example 014 for TCPDF class
// Javascript Form and user rights
//
// Author: Nicola Asuni
//
// (c) Copyright:
// Nicola Asuni
// Tecnick.com s.r.l.
// Via Della Pace, 11
// 09044 Quartucciu (CA)
// ITALY
// www.tecnick.com
// info@tecnick.com
//============================================================+

/**
* Creates an example PDF TEST document using TCPDF
* @package com.tecnick.tcpdf
* @abstract TCPDF - Example: Javascript Form and user rights
* @author Nicola Asuni
* @copyright 2004-2008 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
* @link https://tcpdf.org/
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
* @since 2008-03-04
*/

require_once('../config/lang/eng.php');
require_once('../tcpdf.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Nicola Asuni");
$pdf->SetTitle("TCPDF Example 014");
$pdf->SetSubject("TCPDF Tutorial");
$pdf->SetKeywords("TCPDF, PDF, example, test, guide");

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

//initialize document
$pdf->AliasNbPages();

// add a page
$pdf->AddPage();

// ---------------------------------------------------------

// set font
$pdf->SetFont("dejavusans", "BI", 20);

// Release User's Rights For PDF reader.
// This is required to display and fill form fields on PDF Readers.
$pdf->setUserRights();

/*
Caution: the generated PDF works only with Acrobat Reader 5.1.
It is possible to create text fields, combo boxes, check boxes and buttons. Fields are created at the current position and are given a name. This name allows to manipulate them via JavaScript in order to perform some validation for instance.
Upon field creation, an associative array can be passed to set a number of properties, among which:
rect: Position and size of field on page.
borderStyle: Rectangle border appearance.
strokeColor: Color of bounding rectangle.
lineWidth: Width of the edge of the surrounding rectangle.
rotation: Rotation of field in 90-degree increments.
fillColor: Background color of field (gray, transparent, RGB, or CMYK).
userName: Short description of field that appears on mouse-over.
readonly: Whether the user may change the field contents.
doNotScroll: Whether text fields may scroll.
display: Whether visible or hidden on screen or in print.
textFont: Text font.
textColor: Text color.
textSize: Text size.
richText: Rich text.
richValue: Text.
comb: Text comb format.
multiline: Text multiline.
charLimit: Text limit to number of characters.
fileSelect: Text file selection format.
password: Text password format.
alignment: Text layout in text fields.
buttonAlignX: X alignment of icon on button face.
buttonAlignY: Y alignment of icon on button face.
buttonFitBounds: Relative scaling of an icon to fit inside a button face.
buttonScaleHow: Relative scaling of an icon to fit inside a button face.
buttonScaleWhen: Relative scaling of an icon to fit inside a button face.
highlight: Appearance of a button when pushed.
style: Glyph style for checkbox and radio buttons.
numItems: Number of items in a combo box or list box.
editable: Whether the user can type in a combo box.
multipleSelection: Whether multiple list box items may be selected.
Colors can be chosen in the following list (case sensitive): black white red green blue cyan magenta yellow dkGray gray ltGray or be in the form #RRGGBB.
*/

$pdf->Cell(0,5,'Subscription form',0,1,'C');
$pdf->Ln(10);
$pdf->SetFont('','',12);

//First name
$pdf->Cell(35,5,'First name:');
$pdf->TextField('firstname',50,5,array('strokeColor'=>'ltGray'));
$pdf->Ln(6);

//Last name
$pdf->Cell(35,5,'Last name:');
$pdf->TextField('lastname',50,5,array('strokeColor'=>'ltGray'));
$pdf->Ln(6);

//Gender
$pdf->Cell(35,5,'Gender:');
$pdf->ComboBox('gender',10,5,array('','M','F'),array('strokeColor'=>'ltGray'));
$pdf->Ln(6);

//Drink
$pdf->Cell(35,5,'Drink:');
$pdf->RadioButton('drink',5,false);
$pdf->Cell(35,5,'Water');
$pdf->Ln(6);
$pdf->Cell(35,5,'');
$pdf->RadioButton('drink',5,false);
$pdf->Cell(35,5,'Beer');
$pdf->Ln(6);
$pdf->Cell(35,5,'');
$pdf->RadioButton('drink',5,false);
$pdf->Cell(35,5,'Wine');
// set export values
$pdf->IncludeJS("fdrink.exportValues=[\"Water\", \"Beer\", \"Wine\"];\n");
// check the second radiobutton
$pdf->IncludeJS("fdrink.checkThisBox(1,true);\n");
$pdf->Ln(10);

//Gender
$pdf->Cell(35,5,'List:');
$pdf->ListBox('listbox',60,15,array('','item1','item2','item3','item4','item5','item6','item7'), array('multipleSelection'=>'true'));
$pdf->Ln(20);

//Adress
$pdf->Cell(35,5,'Address:');
$pdf->TextField('address',60,18,array('multiline'=>true,'strokeColor'=>'ltGray'));
$pdf->Ln(19);

//E-mail
$pdf->Cell(35,5,'E-mail:');
$pdf->TextField('email',50,5,array('strokeColor'=>'ltGray'));
$pdf->Ln(6);

//Newsletter
$pdf->Cell(35,5,'Receive our',0,1);
$pdf->Cell(35,5,'newsletter:');
$pdf->CheckBox('newsletter',5,true);
$pdf->Ln(10);

//Date of the day (determined and formatted by JS)
$pdf->Write(5,'Date: ');
$pdf->TextField('date',30,5);
$pdf->IncludeJS("getField('date').value=util.printd('dd/mm/yyyy',new Date());\n");
$pdf->Ln();
$pdf->Write(5,'Signature:');
$pdf->Ln(3);

//Button to validate and print
$pdf->SetX(95);
$pdf->Button('Envoyez',25,15,'Envoyez','EnvFormfdf()',array('textColor'=>'yellow','fillColor'=>'#FF5050'));

//Form validation functions
$pdf->IncludeJS("
function CheckField(name,message) {
var f = getField(name);
if(f.value == '') {
app.alert(message);
f.setFocus();
return false;
}

return true;

}

function envoifield(d) {
var f = getField(d);
if(f.value == '') {

return false;
}
return f.value;
}

function EnvFormfdf() {
this.submitForm.method='POST';
this.submitForm({cURL:'test.php?firstname='+envoifield('firstname')+'' , cSubmitAs:'fdf',bAnnotations:true});



//this.submitForm('test.php',TRUE, 'name');
//app.alert(firstname);

}

//this.EnvFormfdf()

function Print() {
//Validation
if(!CheckField('firstname','First name is mandatory'))
return;
if(!CheckField('lastname','Last name is mandatory'))
return;
if(!CheckField('gender','Gender is mandatory'))
return;
if(!CheckField('address','Address is mandatory'))
return;
//Print
EnvFormfdf();
//print();
}
");

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output("example_014.pdf", "I");

//============================================================+
// END OF FILE
//============================================================+
?>
/code
la page appelé a la soumission du formulaire pdf qui ne sert qu'a verifié que les données sont bien envoyé par la methode 'get' (par l'url). :
[code]
<?php
echo 'test sur firstname : <br>';
echo $_GET['firstname'] ;
?>
/code
25
ouf...
0
pas gratuit = pas cool
0