What is the most practical and quick way to generate a PDF report from a PHP? I have seen some examples but all very confusing and always using the same method.
What is the most practical and quick way to generate a PDF report from a PHP? I have seen some examples but all very confusing and always using the same method.
The way I find it quicker to generate PDF files in the PHP language is with the TCPDF ( link ) class that renders the code html and generates PDF, this solution is very simple compared to others because you do not have to write again to generate this format, you simply use your html code and ask the class to render in PDf.
Check out this link link , the documentation is quite rich.
I hope I have helped
The class DOMPDF is a good alternative for your purpose
It allows you to convert HTML to PDF by reading its DOM structure.
Below, we have an extremely simple example of its use
<?php
// Inclui a classe DOMPDF
include_once("dompdf_config.inc.php");
/**
* Gera um arquivo PDF lendo o conteúdo HTML proveniente de uma string
* @param string $titulo Define o título do arquivo PDF
* @param string $html A string de origem contendo a strutura HTML a ser convertida para PDF
* @param string $tipo Define o tipo do documento PDF (P = Retrato ou L = Paisagem
* @return bool Retorna se o arquivo foi gerado com sucesso ou não
*/
function toPDF($titulo, $html, $tipo = "P") {
$dompdf = new DOMPDF();
// Define internamente o tipo do documento
if ($tipo == "L") {
$dompdf->set_paper("legal", "landscape");
}
// Carrega o $html de entrada para a classe
$dompdf->load_html($html);
// "Renderiza" o conteúdo
$dompdf->render();
// Cria e obtém o PDF gerado
$pdf = $dompdf->output();
// Define o caminho onde será salvo
$arquivo = "arquivos/" . $titulo;
return ( file_put_contents($arquivo,$pdf) );
}
?>
How to use:
<?php
// Html de teste
$html = '
<!DOCTYPE html>
<html>
<head>
<title>Teste de HTML</title>
</head>
<body>
<div>Olá, mundo!</div>
</body>
</html>
';
// Tenta criar o arquivo PDF
toPDF("arquivo.pdf", $html);
?>