5

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.

Nick Pickering
  • 3,095
  • 3
  • 29
  • 50
  • Does the error occur on the same line every time? For problems like this it can help to see the HTML. Also, a 500 error would be something thrown by PHP. If you can get the error it would help. Look in your error log or modify your settings so errors are sent to the browser. – BrianS Mar 25 '13 at 21:12
  • 3
    I actually had a similar problem with DomPDF. DomPDF has a lot of problems with complex layouts. In my particular case, DomPDF was crashing because of some CSS rules I had applied to a table (I believe the issue was a fixed height). My suggestion for debugging is to start with the most basic document you possibly can and gradually add CSS rules and complexity until you discover what the issue is. – Liv Mar 26 '13 at 13:20
  • 1
    @NicholasPickering glad my comment was helpful. This release of DomPDF is _supposed_ to have support for background images, but given that the project has 168 open issues on GitHub, it wouldn't surprise me if you found bug number 169. DomPDF's CSS parser is very much a "work-in-progress", and the trouble is that instead of dropping CSS rules it thinks are invalid (or that it just can't understand), it crashes without much in the way of useful debugging information. – Liv Mar 26 '13 at 15:03
  • @NicholasPickering I can't see any reason a background image would cause that kind of problem. It may be a combination of factors. Any chance you can post the HTML that's causing a problem? – BrianS Mar 26 '13 at 15:16
  • @Liv dompdf does support background images. And it does drop invalid rules. Occasionally there's some syntax or invalid document structure that trips it up and it's always helpful to have that one extra bug report so we can fix it. – BrianS Mar 26 '13 at 15:22
  • @BrianS Good to know. I was basing my remark about invalid CSS rules based on [this open issue on GitHub](https://github.com/dompdf/dompdf/issues/133) and my own experience using DomPDF for projects. – Liv Mar 26 '13 at 15:39
  • @liv HA, that one is apparently still a problem in that the sample renders way off (large margin applied). So I guess I should say we're better than we were as far as discarding invalid styles, but there are still problems. – BrianS Mar 26 '13 at 16:41
  • @NicholasPickering could it be a memory overflow or timeout? I added my own image and I was able to render the document, though it took more than a few seconds. The larger the image, the more render time/memory it takes. Can you try a different, smaller image or a different format (like JPG)? – BrianS Mar 26 '13 at 17:10
  • @BrianS I converted the image to a JPG from a PNG, and it worked perfectly. PHP should be giving me a memory limit reached error, but its just throwing a generic 500 error. – Nick Pickering Mar 26 '13 at 19:57
  • 1
    @NicholasPickering JPG is a simpler format to work with so it typically requires less processing (plus you got the smaller file size). I'm not sure what's up with the generic error. If you're using Apache/FastCGI you might check the Apache error log for the site. The 500 error could indicate a segfault, which would show up there. – BrianS Mar 26 '13 at 20:17
  • can you send me html code i will generate pdf........ – Venkata Krishna Mar 29 '13 at 10:32
  • @Venkat Thanks, but I don't need that. I was almost able to finish what I needed to do but I just couldn't get DomPDFs memory footprint below the limit of the hosting account. We moved to a new hosting account with PHP v5.4 and higher `memory_limit`. This gave us much more breathing room. – Nick Pickering Mar 30 '13 at 04:06
  • Changing the PNG background to a medium quality jpg did it for me – Michael L Watson Jan 23 '14 at 06:47

4 Answers4

2

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.

  • Is this really a solution to his problem? – yakobom Jun 25 '17 at 13:52
  • within the 6 hours from my comment , meanwhile i was working on the same problem and what i found is actually 500 internal server error happens due to many issues on server. so i used this code in the starting of my php script . this code saves the actual error on a error log file. // Report all errors error_reporting(E_ALL); ini_set('display_errors', true); in my case i am getting an error of illegal string offset 'hex' dompdf style.php – Infoconic Technologies Jun 25 '17 at 19:49
  • how is this an answer tho – David Seek Mar 08 '19 at 23:19
1

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.

jaydeep
  • 96
  • 4
0

I have used DOMPDF to generate PDF from HTML Code in the past. And i have face same problem. After many days strugling i have come to know that dompdf is only meant to used for small documents like 3 to 4 pages that's the rough figure. Dompdf is good with CSS 2 based styling and it renders close to reality. But it did not support large pages conversion, also if you convert large pages pdf it will take too much time and get stuck or throw 500 error. I would suggest you to use TCPDF if you have simple rendering data in your html. Another option is to use wkhtmltopdf extension which can be install on linux server and convert just tell the url and it will convert exactly the same PDF for you.

Here are some links:

https://tcpdf.org/
https://wkhtmltopdf.org/
https://github.com/mikehaertl/phpwkhtmltopdf
https://stackoverflow.com/questions/5663814/how-do-i-get-wkhtmltopdf-to-execute-via-php
Bilal Ahmed
  • 119
  • 1
  • 11
0

Just my two cents (cannot comment in the OP since I have less than 50 rep, sorry). After several hours of suffering, checked the Apache logs(doh!) and to my surprise:

PHP Fatal error:  Uncaught exception 'Exception' with message 'The PHP GD extension 
is required, but is not installed.' in \\wwwroot\\app\\dompdf070\\lib\\Cpdf.php:4965

Enabling the GD extension solved the problem in my case.

You can do so in  PHP.INI uncommenting the line 

extension=php_gd2.dll