How to generate a PDF contract using PHP directly?

Asked

Viewed 6,107 times

5

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

I need that the moment the user clicks the button GerarPDF the data entered by the user appear in the contract and this contract is generated in PDF.

You can generate the full contract directly from PHP using the data from the HTML form?

  • I always use html2pdf. With it I can generate PDF with HTML tags.

1 answer

7

To convert HTML to PDF, we already have another question on the website, in this show how to generate a PDF directly from PHP.

Using the class FPDF, which is written entirely in PHP and released for commercial use at no cost, 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 an accurate control of the final result.

Follow a well taught example, only with the essential:

gerarpdf.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') 
?>

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 a basic notion of how simple it is to use the FPDF, the official website has much more complete examples and tutorials, plus a number of extensions for barcodes, drawings, graphics, UTF-8 support, including detailing how to do the embed of custom fonts in PDF.

Important remarks:

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

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

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

Check out the manual on: http://www.fpdf.org

  • Follow a demo. I just put as a comment, because this link will break soon. http://ninja.net.br/fpdf_001.php?nome=Bacco&horas=120&data=7+de+Setembro+de+2014 . To test, copy the link and change the parameters, because the demo uses GET.

  • Very good @Bacco Thanks for the Force Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.