Create PDF from a BIG HTML file with DOMPDF

1

I'm trying to convert a 40mb html file into PDF using DOMPDF. Even with a timeout of 3600 seconds, it does not generate the file, nor do I put it as a cron job 1x a day or run it on the command line.

My script

<?php
    require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

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

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

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

// Output the generated PDF to Browser
$dompdf->stream();
?>

where the variable $html is all contents of my 40 mega file.

I would like to know if someone has some more efficient conversion class, works well with large files or if DOMpdf itself has this support

    
asked by anonymous 05.09.2016 / 19:46

1 answer

2

I've had the same problem. I could not find a solution with DOMPDF. With Snappy the problem does not happen with the size of the file, it is very fast!

The library can be found here .

Installing via composer:

$ composer require knplabs/knp-snappy

Requires Wkhtmltopdf and Wkhtmltoimage applications available for Windows Linux and OS X available here

Including and using the library in your project:

require __DIR__ . '/vendor/autoload.php';
use Knp\Snappy\Pdf;

//adicione o caminho para o seu wkhtmltopdf como no exemplo abaixo
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

//configure a pasta temporária para salvar o arquivo
$snappy->generateFromHtml($html, '/tmp/arquivo.pdf');

//force o download do arquivo
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
readfile('/tmp/file.pdf);

In this link make the folder with the files available if you can not download. Extract the file and put the vendor folder and the wkhtmltox folder at the root of your project.

The path of wkhtmltopdf will be '__DIR__./wkhtmltox/bin/wkhtmltopdf'

    
05.09.2016 / 20:25