How can I reduce time to generate PDF dynamically using PHP? - php

I am generating PDF dynamically using html2ps PHP library. I want to decrease time of generating that PDF .I want to reduce that PDF generation time .Is there any way to reduce the time or optimize it?
Help me please.

Are you running this code on recent hardware?
While it may sound like avoiding the solution, on the Coding Horror Blog (whom happens to be the guys who made this site) they preach that you shouldn't spend time tweaking performance if your hardware is limited.
If you're doing this on a single core CPU (ex: Pentium 4), you are wasting your time worrying about what library to use or what code to change. Even the slowest Core 2 Duos and newer AMDs start at 2x faster than the best Pentium 4.
PS: I wasn't able to find the article on their site to link for you.
PPS: Most Pentium 4 motherboards support the 65nm Core 2 Duos.

One method of optimizing is to try another library. I use dompdf when I need to convert HTML to PDF, and I haven't found any need for optimizing, it's very fast, supports CSS properly and produces accurate results.

Reduce the complexity of the output.
Reduce the output quantity.
If the PDF generation is impacting other operations, delegate it to another process or server.

I'm sure you can avoid a lot of processing by skipping the HTML/CSS step(s) and go directly to PDF. Check FPDF or PDFLib

Related

phpExcel multiple workbooks memory peformance

I have a report thats generated using PHPexcel. Its quite a large report 8 workbooks/tabs in total. As you can imagine it uses up quite alot of memory and im starting to run into problems with it.
I was wondering what options would be best suited for me to reduce the memory load that it takes to generate this report.
The idea I had was to create the spreadsheet with 8 empty worksheets. Save the file then load the file with the
$objReader->setLoadSheetsOnly($sheetname);
Im not sure if im heading of in the right direction with this though?
The best option to start with would be to look at the cell caching options that are explicitly written to reduce memory usage. These are described in section 4.2.1 of the developer documentation

Optimizing Images via PHP

I would like to build a PHP script to optimize images similarly to how PunyPNG or Kraken.io optimizes images. Essentially, I would need to be able to take .jpeg, .png, and .gif images and reduce their file size as much as possible without losing quality (or with minimal quality loss).
These services offer APIs, but I would like to avoid unnecessary costs, and I do not want to be limited by a specific number of daily uses.
Can this be done with something like ImageMagick? Is it even possible, or is it far too complicated?
talking about re-sizing images, they were never an issue, there are couple of tools that help you do that in bulk. Since you specifically say PHP, I am expecting you would be using it for displaying it on a page. for the very same purpose I wrote this little script not so long ago, which might be of some help to you. Fork it here https://github.com/whizzzkid/phpimageresize
Spatie has a decent package that gets updated regularly, I've been using for a while without problems:
https://github.com/spatie/image-optimizer

Bulk template based pdf generation in PHP using pdftk

I am doing a bulk generation of pdf files based on templates and I ran into big performance issues pretty fast.
My current scenario is as follows:
get data to be filled from db
create fdf based on single data row and pdf form
write .fdf file to disk
merge the pdf with fdf using pdftk (fill_form with flatten command)
continue iterating over rows until all .pdf's are generated
all the generated files are merged together in the end and the single pdf is given to the client
I use passthru to give the raw output to the client (saves time writing file), but this is just a little performance improvements. The total operation time is about 50 seconds for 200 records and I would like to get down to at least 10 seconds in some way.
The ideal scenario would be operating all these pdfs in memory and not writing every single one of them to separate file but then the output would be impossible to do as I can't pass that kind of data to external tool like pdftk.
One other idea was to generate one big .fdf file with all those rows, but it looks like that is not allowed.
Am I missing something very trivial here?
I'm thanksfull for any advice.
PS. I know I could use some good library like pdflib but I am considering only open licensed libraries now.
EDIT:
I am up to figuring out the syntax to build an .fdf file with multiple pages using the same pdf as a template, spent few hours and couldn't find any good documentation.
After beeing faced with the same problem for a long time (wanted to generate my pdfs based on LaTeX) i finally decided to switch to another crude but effective technique:
i generate my pdfs in two steps: first i generate html with a template engine like twig or smarty. second i use mpdf to generate pdfs out of it. I tryed many other html2pdf frameworks and ended up using mpdf, it's very mature and is developed since a long time (frequent updates, rich functionality). the benefit using this technique: you can use css to design your documents (mpdf completely features css) - which comes along with the css benefit (http://www.csszengarden.com) and generate dynamic tables very easy.
Mpdf parses the html tables and looks for the theader, tfooter element and puts it on each page if your tables are bigger than one page size. Also you have the possibility to define page header and page footer elements with dynamic entities like page nr and so on.
i know, using this detour seems to be a workaround, but to be honest, no latex, pdf whatever engine is as strong and simple as html!
Try a different less complex library like fpdf (http://www.fpdf.org/)
I find it quite good and lite.
Always find libraries that are small and only do what you need them to do.
The bigger the library the more resources it consumes.
This won't help your multiple-page problem, but I notice that pdftk accepts the - character to mean 'read from standard input'.
You may be able to send the .fdf to the pdftk process via it's stdin, in order to avoid having to write them to disk.

Are there image processing libraries for PHP that'd allow manipulating images on a pixel by pixel basis like MATLAB?

I'm working on a project that might require me to compute the DCT of the image. Hence the question.
-> Is there a fast way of pixel-by-pixel proceccing images in PHP
I think it would be best to process the files using another language instead of PHP or library/application such as - ImageMagick php.net/manual/en/book.imagick.php.
I've never used this myself, so don't really know performance issues.
Also -> wideimage.sourceforge.net/
The GD functions, as described in the previous answer, are quite slow, and sometimes can take a huge chunk of the CPU for processing.
Take a look here for seperate benchmarks etc:
http://kore-nordmann.de/blog/comparison_of_php_image_libraries_update.html
GD has per-pixel operations, but they're not particularly fast: imagecolorat() and imagesetpixel()

Fastest PDF generation in PHP?

I'm attempting to generate some reports dynamically, very simple HTML tables with borders.
I've tried TCPDF and it renders up to 400 rows just fine but anything more than that (about 20 pages) it can't handle it. DOMPDF can't even do that.
These reports can be thousands of rows.
Any idea on a faster library or a better plan of attack?
Try php-wkhtml2x php extension. It uses popular web engine webkit(Chrome and Safari uses that)
I use the FPDF library, the output is fast and resource-efficient. Try it out...
http://www.fpdf.org/
I don't know if these methods are the fastest, but they can certainly handle more than 20 pages.
You could use latex in combination with php:
http://www.linuxjournal.com/article/7870
or Zend_Service_LiveDocx_MailMerge
http://www.phphatesme.com/blog/webentwicklung/pdf-erzeugung-mit-dem-zend-framework/
Try DocRaptor.com. It's a web-based app that converts html to pdf. Easy to use.
It's possible that depending on the report that PHP is not the right solution you and might consider another alternative language such as perl to accomplish this. I have no experience with other server-side languages but it is something to keep in mind. Definitely follow #Pekka's advice and determine the limits and work on adjusting those.
Fact
Thousands of rows can be handled by the php.
My assumption
Most probably you will be fetching data from db and saving to an array and then you will be looping to write the rows.
This will eat memory.
My suggestion
Try to write into the pdf at time of fetching from db. remove the step of storing into an array.
check execution time and memory allocated in php ini.
At last when you generate it , think PDF can handle it or not :-)
surely It will have huge size .

Categories