dompdf php to pdf

3

I'm using DomPDF to generate the pdf. The question is that I can not convert my html to pdf. My html is generated by several php lines with variables and etc ...

I have tried to pass a variable with everything to DomPDF::loadhtml , but since there are so many lines of code, I can not.

Is there any way to generate the final html and pass to the variable?

// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Get the generated PDF file contents
$pdf = $dompdf->output();

// Output the generated PDF to Browser
$dompdf->stream();
    
asked by anonymous 19.01.2016 / 12:13

2 answers

2

Hello, you can use this way:

$html = file_get_contents('http://www.seusite.com.br/seu_html_gigante.php');

require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();
$dompdf->stream();

header('Content-type: application/pdf; charset=utf-8');
echo $pdf;

If you have problems with session or something, consider using this other way:

ob_start();

include_once ("seu_html_gigante.php");

$html = ob_get_contents();

ob_end_clean()

require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();
$dompdf->stream();

header('Content-type: application/pdf; charset=utf-8');
echo $pdf;
    
19.01.2016 / 13:02
1

Yes, there is. The way I know of "get the final html" is to give an include and capture it in the output buffer.

Example:

ob_start();

include 'seu_html_gigante.php';

$dompdf->loadHtml(ob_get_clean());
    
19.01.2016 / 12:14