How to generate PDF contract using PHP directly?

5

I need to generate a contract, which will come from an HTML form, whose data will be processed by a PHP.

I need the user-entered data to appear in the contract and this contract is generated in PDF.

Is it possible to generate the entire contract directly from PHP, using the data coming from the HTML form?

    
asked by anonymous 07.09.2014 / 20:13

1 answer

7

To convert HTML to PDF, we already have another question on the site , in this display as generate a PDF directly from PHP .

Using the FPDF class, which is written entirely in PHP and released for free commercial use, a PDF can be created from scratch, without intermediate HTML, using very simple functions to write and insert graphic elements directly on the page, with this, having a precise control of the final result.

Here is a very didactic example, only with the essentials:

generatepdf.php :

<?php 
   require_once( 'fpdf.php' );

   $nome  = @$_POST['nome'];  // Sim, a supressão @ é perfeitamente válida neste exemplo
   $horas = @$_POST['horas']; // os parâmetros serão checados logo em seguida.
   $data  = @$_POST['data'];  // Num cenário onde seja comum o envio vazio, use isset()
   // Aqui você processa os parâmetros desejados, isto é só um exemplo.
   // Utilizei as variáveis do <form>, mas aqui você pode pegar o que
   // precisar de algum DB, ou mesmo misturar as técnicas.
   if( empty( $nome  ) ) $nome = 'Anônimo da Silva';
   if( empty( $horas ) ) $horas = 24;
   if( empty( $data  ) ) $data = '17 de agosto de 2014';

   // e finalmente, geramos o PDF:
   $pdf = new FPDF();

   $pdf->AddPage();
   $pdf->SetFont('Arial','B', 14);
   $pdf->SetXY( 10, 20 );
   $pdf->Cell( 190, 0, 'DECLARAÇÃO', 0, 0, 'C');

   $pdf->SetFont('Arial','', 12);
   $pdf->SetXY( 10, 30 );
   $pdf->MultiCell( 190, 6,
      "  Eu, $nome, declaro que adquiri de Alaor Ivan Souza ".
      "um pacote de créditos para acesso à internet com duração ".
      "de $horas horas, iniciando-se em $data.\n".
      "  Declaro ainda que estas informações provavelmente são ".
      "inverídicas e sem sentido, pois isto aqui é um mero teste."
   );
   $pdf->Output(); // Isto envia o PDF diretamente para o usuário.
                   // Para gerar como arquivo use Output('F','caminho_do_arquivo.pdf') 
?>

form.htm :

<form method="post" action="gerarpdf.php">
   <label for="nome">Nome:</label><br>
   <input type="text" id="nome" name="nome"><br>
   <label for="horas">Horas:</label><br>
   <input type="text" id="horas" name="horas"><br>
   <label for="data">Data por extenso:</label><br>
   <input type="text" id="data" name="data"><br>
   <br>
   <input type="submit" value="Gerar PDF"><br>
</form>

This code is just to give you a basic idea of how easy it is to use FPDF, the official site has much more examples and tutorials complete, as well as a number of extensions to barcodes, drawings, graphics, UTF-8 support, including detailing how to embed custom fonts in PDF.

Important remarks:

    For the accent to work right, you have to set the same encoding in the form and PHP, and if they are in UTF-8, use utf8_decode() to merge. This also applies to the strings of the code;

  • FPDF has a version on the site itself, with support for UTF-8 if needed;

  • In this demo I did not embed from any source, but it is very simple to do, and there are a lot of good examples on the site.

Check the manual at: link

    
07.09.2014 / 20:35