Memory problem with PDF but not Excel

Topics: Developer Forum
Apr 21, 2011 at 9:16 PM

There have been a lot of posts on this subject, but none that I have seen have addressed the problem I have. My code pulls data from a database table and generates an Excel file with just over 3000 rows, with 12 columns. Not a huge file - it uses around 100MB of memory to generate this, but if I try to generate the same file as a PDF, it uses over 1 GB of memory and still doesn't complete generating the file. Both files are created by the same code with a few variations. Hacking around with it I am able to get half of the PDF generated with the 1 GB of memory. This seems to be way out of whack, compared to other things I've read in these discussions. So far I've tried the serialized caching, but still got the same result. What would explain the huge amount of memory required to generate the PDF for such a relatively small file? And what would account for the huge difference in memory usage between the Excel and the PDF?

I really like the application and want to use it but don't think I should have to use 2 GB plus to generate this file.

Coordinator
Apr 21, 2011 at 10:00 PM

PDF is a perennial problem, and probably causes me more grief than any other feature of PHPExcel.

When we generate a PDF, we generate HTML first and then feed that HTML to the third-party tcPDF library that actually renders the PDF. We know that there are a lot of problems with this, not least that fact that tcPDF is horrendously slow. I have been looking at alternatives, and this thread discusses some of the pros and cons of tcPDF and a couple of the other libraries that can be used to generate PDF files from HTML (specifically DomPDF and mPDF). Other alternatives include HTML2PDF, or command line libraries like  WKHTMLTOPDF. It might seem sensible to skip the HTML, and generate the PDF directly, but I simply don't have the time to build a writer for that.

Apr 22, 2011 at 5:01 PM

If you have the HTML and say mPDF, you can just send the HTML to mPDF yourself, which is actually pretty easy.

Apr 24, 2011 at 7:33 PM

Thanks for your responses. I went back to using FPDF to generate the PDF. I had originally used that and got a very professional rendition of the file, but when I came across PHPExcel, I really liked the package (very easy to use) and it generated a great Excel file, and I found out it generated PDF, I got real excited. For the application I'm building, I'm creating a number of reports that will display on line in HTML with the option of generating Excel or PDF versions, and if I could generate both of those with one package, it would save a lot of extra coding. But currently that is not to be and I have solutions that work, so I'll stick with those. I do really like the PHPExcel package and if I ever hear that the PDF generating aspect of it is sorted out, I will be very happy.

Coordinator
Apr 24, 2011 at 7:53 PM

Unfortunately, there are limits to what one person can do working in his spare time.

If you feel inclined to write a PDF writer that uses FPDF for PHPExcel, I'd be more than happy to incorporate it.

Apr 24, 2011 at 8:12 PM
That would be interesting to do. I understand your plight about working in your spare time,because I have the same limitations at this particular point in time. Perhaps a bit down the road.