Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DomPDF render of large HTML file results in 500 error

Tags:

php

dompdf

I'm using the most recent version of DomPDF (0.6.0 beta 3).

PHP Version is 5.2.17.

Unfortunately, I'm on a shared hosting account and upgrading PHP and similar actions are unavailable. I also don't have access to the apache error logs, which I don't think would be very helpful anyways.

I've set the memory_limit of PHP to 2048M and the max_execution_time to 999. Although I don't know if they've taken effect. I think they actually don't matter because I feel like DomPDF isn't timing out or running out of memory. The 500 error seems to occur just 10 seconds after requesting the page, when I attempt to generate a PDF with fewer pages the script takes longer (12-15 seconds) but ends successfully.

I have an HTML file that's 72.3kb large, with only one small HTML table. (In fact, removing the page with the table does nothing to help the problem.) The PDF should result in about 35 pages.

After lots and lots of digging I found the error occurs on the 30th - 31st page, on the following line of code in page_frame_reflower.cls.php inside the reflow function:

  // Render the page
  $this->_frame->get_renderer()->render($child);

When echoing memory_get_usage on each loop(page) the value stays pretty consistent, under 5% of available memory, it seems.

Using get_class on $this->_frame->get_renderer() I was able to determine that the problem function is Renderer::render().

Unfortunately, there's no easy way for me to continue debugging with my current methods. The method Renderer::render() won't let me add the counter variable to the parameters because it needs to be compatible with it's parents function.

Anybody got any help for me, do you see anything I'm missing - can you point me in the right direction to fixing these 500 errors?

Edit: I've removed all uses of inline PHP from the HTML document, and it didn't seem to change anything.

Edit 2: The document was too large to completely run through Tidy, but up to 50000 bytes had no errors.

Edit 3: PHP Error logging has been turned on. I can generate errors by calling wrong method names and other things. Those errors appear on the screen. In fact, even when I cause an infinite loop before the problem method, I get PHP maximum execution time reached errors printed to the screen. However the problem area shows a 500 Internal Server Error page.

Edit 4: I stripped out all of my CSS and the PDF was generated without styling. Now we're getting somewhere. I'll slowly add the CSS back in and I can narrow down what the problem code. Thanks, @Liv.

Edit 5: I narrowed down the CSS to the bud of the problem: the h1 rule. I was using a background image on the h1 tags. So from my original problem, all I had to do was remove the background images from the h1 tag to get the PDF to work properly. Is handling a lot of elements with background images a problem for DomPDF?

Edit 6: Here's the HTML file: http://www.pickering-mcaloon.com/wp-content/uploads/2013/03/test2.html I had to strip out the content (wasn't mine). I tested this version of the HTML with DomPDF and got a 500 server error. My header image is 25.1 kb. It should be noted that the footer of every page also uses a background image and it works fine. In this copy of the HTML, removing 1 "Chapter" section permitted DomPDF to finish the generating the PDF.

Edit 7: When I first ran into the problem I had used memory_get_usage and at the top of the loop where Renderer::render() is called, each time it showed a slightly different usage, but under 6% of the max, which was set to 2048M. I don't believe this problem is a timeout error or a memory error, because I can produce these types of errors before the call to render and get the PHP errors printed to the screen. Also, the 500 error appears after about 10 seconds of runtime - When the PDF works it takes about 15-20 seconds. So a timeout is unlikely. Memory errors, perhaps but if so why not print the error as it does if the memory overflows before the call to render.

Edit 8: I converted the image I was using as a background image for the h1 tags to a JPG from a PNG, which reduced its size from 25.1kb to 2.6kb and the PDF generated properly. This is likely a memory issue. PHP should generate an error and show it to me, but it doesn't.

like image 330
Nick Pickering Avatar asked Oct 22 '22 14:10

Nick Pickering


2 Answers

The same problem i was facing and still working on it. I am using Dompdf and trying to convert an HTML page of only 33KB but i get an 500 internal server error.

I have multiple external style sheets in my html page under head section including bootstrap css. But When i remove the bootstrap css , dompdf works fine.

removing this line may help you --> link type="text/css" rel="stylesheet" href="/bootstrap.css"

But i do not know what should be done in order to include Bootstrap.css to dompdf and why bootstrap style is causing error for dompdf.

like image 78
Infoconic Technologies Avatar answered Oct 30 '22 20:10

Infoconic Technologies


Generally this issue occurred when memory limit lower than the whenever required memory.

There's is the increase these, for ex: ini_set("memory_limit", "800M"); ini_set("max_execution_time", "800");

Note : Do this before you instantiate the class, $dompdf = new DOMPDF();

here is set the memory_limit OR max_execution_time whatever you required.

like image 36
jaydeep Avatar answered Oct 30 '22 19:10

jaydeep