How to generate PDF with little memory on the server?

Asked

Viewed 653 times

7

I’m passing a table from my comic to PDF and I’m using the TCPDF.

Only that first I have to pass my table to HTML and only then I can pass to PDF, which entails use of a lot memory and I have few server resources (256M for PHP at most).

How can I pass a table that can have thousands of records to PDF with a maximum of 256M memory in PHP?

  • 1

    I only used mpdf in a project. It met my needs!

  • Difficult has a correct answer, without taking charge test. I do not believe that there will be answers that consider all options and are not just personal opinions. My suggestion: Test all these components and write down the numbers.

  • My suggestion, who will answer do the load tests and post the results :D

2 answers

2

I have had the same problem. I solved using Snappy that generates PDF in a different way, using little memory.

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'

1

I always use the MPDF, in terms of memory you can increase the maximum size of memory allocation in PHP with the ini_set("memory_limit","120M"); or the size I find necessary, in terms of performance I can not make a comparison with these other classes because I have not used others in any projects my.

  • I can’t increase the size of the memory, I said above in the question.

Browser other questions tagged

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