Create PDF from a LARGE HTML file with DOMPDF

Asked

Viewed 5,082 times

1

I am trying to convert a 40mb html PDF file using DOMPDF. Even with 3600 seconds timeout, it does not generate the file, nor if I put it to as 1x cron task a day or run on 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 content from my 40 mega file.

I wonder if someone has some more efficient conversion class, that works well with large files or if Dompdf itself has this support

1 answer

2


I’ve had the same problem. I couldn’t find a solution with DOMPDF. With Snappy does not happen problem with file size, 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);

At this link I made the folder available with the files if I can’t download. Extract the file and put the folder vendor and the folder wkhtmltox at the root of your project.

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

  • sorry ignorance but I have a server with Cpanel that hosts my client’s website and I need to generate the pdf, but I’m not able to use it. By any chance could you hand me the zip of the files for use ? I downloaded the git libraries and downloads from the site but neither autoload.php file exists

  • I edited the answer and put the files.

  • very good , congratulations and thanks for the help

Browser other questions tagged

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