-4

I need to generate 270 PDF and i use DOMPDF. The problem is that after generate 20 PDF i get a Internal Server Error. I set the time limit to zero,too. What would be the best way to generate many pdf?

Community
  • 1
  • 1
Eliana
  • 395
  • 3
  • 10
  • 20
  • Perhaps one at a time? – Mark Baker Jun 06 '16 at 12:08
  • @MarkBaker i generate the pdf into while.. and into table there are 270 records – Eliana Jun 06 '16 at 12:09
  • 1
    A "500 Internal Server Error" status code (or a blank page) means that your script is throwing an error but you haven't configured PHP to display error messages. That's something you need to address before you go further because it's hard to code properly without the aid of error messages. The error reporting thumb rule is to show in development and log in production. As a starting point, I suggest you edit the system-wide `php.ini` file in the computer where you develop and tweak the `error_reporting` and `display_errors` directives ([details here](http://stackoverflow.com/a/5680885/13508)). – Álvaro González Jun 06 '16 at 12:10
  • don't run by the browser. And probably the problem is the memory. – cmnardi Jun 06 '16 at 12:11
  • @ÁlvaroGonzález i have already set the error reporting and i have only notice.. – Eliana Jun 06 '16 at 12:13
  • 2
    @Caio the script will be executed by a cronjob , but for now I 'm running through a browser – Eliana Jun 06 '16 at 12:17
  • Assuming those notice you mention are actually irrelevant to the issue...Trust me, the error message can always be read. In rare cases it'll be a complete PHP crash but you'll still see something in web server logs or even Windows Event Viewer (if applies). – Álvaro González Jun 06 '16 at 12:17
  • Don't test cron job task with the browser, do it in the command line. The browser will introduce lots of bug not present on final environment and will mask others. – Álvaro González Jun 06 '16 at 12:18
  • 1
    @Eliana so try to run in the colose... php script.php and see if works What is the notice that you have?? – cmnardi Jun 06 '16 at 12:24
  • I'll post an answer, but I just wanted to point out that what typically helps on SO and other forums is to a) post what you have tried and b) if you have any errors post the details – BrianS Jun 06 '16 at 14:12
  • @ÁlvaroGonzález I see the log file, and there are anything in addition to the notice. So, if it was a timeout problem or other, with the cronjob I should not have problems, right? – Eliana Jun 06 '16 at 14:15
  • In command-line PHP, `max_execution_time` is zero (unlimited) by default. But if that was the issue, you should really get a clear error message in your browser :-? – Álvaro González Jun 06 '16 at 14:23

2 Answers2

3

To generate a large number of documents using Dompdf I would spawn an external process for each render. This can be both quicker and less resource intensive (namely around memory usage) [ref].

The steps I've used in the past for a similar task were:

  1. Generate each HTML document for render (optionally save to a file).
  2. Spawn a process to render that HTML to PDF.
  3. If HTML was saved to a file, delete the HTML.

So step 2 would be something like this:

foreach ($htmldocs as $htmldoc) {
  exec("php dompdf.php $htmldoc");
}

And dompdf.php would be a script that instantiates dompdf and renders the HTML file.

$dompdf = new Dompdf();
$dompdf->load_html_file($argv[1]);
$dompdf->render();
$filename = basename($argv[1], '.html').'.pdf';
file_put_contents($filename, $dompdf->output());

Bonus info: When I've done something similar I was actually creating a largish document with easily defined pages. As a fourth step in that process I used pdftk to combine the rendered PDF documents into a single PDF.

$exec_cmd = 'pdftk';
for ($html_doc = 1; $html_doc <= count($html_docs); $html_doc++) {
    $exec_cmd .= ' ' . TEMP_DIR . '/' . $filenames[$html_doc] . '.pdf';
}
$exec_cmd .= ' cat output ' . FILESTORE_DIR . '/' . $packet_gen_info['packet_code'] . '.pdf';
exec($exec_cmd, $pdftk_output, $pdftk_return_code);
BrianS
  • 13,284
  • 15
  • 62
  • 125
1

You should look at wkhtmltopdf. It's a great tool and I've been using it to generate thousands of pdfs with thousand of pages without any problem.

Syntax is easy as can be:

wkhtmltopdf http://google.com google.pdf

This will make a pdf from google homepage.

cb0
  • 8,415
  • 9
  • 52
  • 80
  • It's worth noting that wkhtmltopdf runs an actual (headless) browser so it's neither lightweight nor particularly fast. It might not be a good alternative for batch processing, esp. if the job doesn't involve complex layouts. – Álvaro González Jun 06 '16 at 12:23
  • Sure it really depends on the data to be converted. Complex layouts might not look good. You could also use `tcpdf` for manually layouting the pages. This way you get more power over the final result, but it also takes a bit more effort to produce/code it. It might also be faster if you have a runtime crtical application. – cb0 Jun 06 '16 at 12:27
  • @cb0 i generate the output dynamically... so i have a variable $output = "my html" and i need to save pdf into folder. In this case i can this: wkhtmltopdf $output /folder/mypdf.pdf or not? – Eliana Jun 06 '16 at 13:00
  • Have a look at this awnser: http://stackoverflow.com/a/10314171/85737 from Michael Härtl. He describe how to use it with php and without a html file but html code in a variable. – cb0 Jun 06 '16 at 13:03
  • 1
    @Eliana I wouldn't pass complex strings as command line parameters, it's better to use standard input or temporary files. Whatever, don't just rewrite your complete application without even having a clue of what's wrong now. – Álvaro González Jun 06 '16 at 14:12