Rectangle 27 3

If you render your HTML using a secondary PHP process (e.g. using exec()) the execution time and memory limits are eased. When rendering in this method you save the rendered PDF to a directory on the web site and redirect the user to the download (or even email them a link if you want to run a rendering queue). Generally I've found that this method does offer modest improvements in speed and memory use.

That doesn't, however, mean the render will perform significantly faster in your situation. Rendering tables with dompdf is resource intensive at present. What might work better, if you can, is to break the document into parts, render each of those parts separately (again using a secondary PHP process), combining the resulting collection of PDFs into a single file (using something like pdftk), then saving the PDF where the user can access it. I've seen a significant performance improvement using this method.

Or go with something like wkhtmltopdf (if you have shell access to your server and are willing to deal with the installation process).

Thanks BrianS, I'll take these into consideration. I briefly looked at wkhtmltopdf so I'll give that another look as well.

Good luck. If you decide to stick with dompdf and need an example I can produce some code (or point to something I previously wrote) if needed.

Thanks Brian, I'm going to look into spawning background processes and saving the resulting file to a directory the user can download the file from. Maybe send an email when the report generation is complete. Thanks for the advice!

This has been discussed on the forums before, see groups.google.com/d/topic/dompdf/tvYDvZQ6u0E/discussion. I have some (fairly old) code I wrote that renders HTML-based test packets to PDF in batch, one page at a time, combined afterwards with pdftk, see here: eclecticgeek.com/code/dompdf.txt

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

pdf generation - php and dompdf - how to generate a large pdf without ...

php pdf-generation dompdf
Rectangle 27 124

After some investigation and general hair-pulling the solution seems to be HTML2PDF. DOMPDF did a terrible job with tables, borders and even moderately complex layout and htmldoc seems reasonably robust but is almost completely CSS-ignorant and I don't want to go back to doing HTML layout without CSS just for that program.

HTML2PDF looked the most promising but I kept having this weird error about null reference arguments to node_type. I finally found the solution to this. Basically, PHP 5.1.x worked fine with regex replaces (preg_replace_*) on strings of any size. PHP 5.2.1 introduced a php.ini config directive called pcre.backtrack_limit. What this config parameter does is limits the string length for which matching is done. Why this was introduced I don't know. The default value was chosen as 100,000. Why such a low value? Again, no idea.

What's horrifying about this is that when the limit is exceeded, the replace just silently fails. At least if an error had been raised and logged you'd have some indication of what happened, why and what to change to fix it. But no.

So I have a 70k HTML file to turn into PDF. It requires the following php.ini settings:

Now the astute reader may have noticed that my HTML file is smaller than 100k. The only reason I can guess as to why I hit this problem is that html2pdf does a conversion into xhtml as part of the process. Perhaps that took me over (although nearly 50% bloat seems odd). Whatever the case, the above worked.

Now, html2pdf is a resource hog. My 70k file takes approximately 5 minutes and at least 500-600M of RAM to create a 35 page PDF file. Not quick enough (by far) for a real-time download unfortunately and the memory usage puts the memory usage ratio in the order of 1000-to-1 (600M of RAM for a 70k file), which is utterly ridiculous.

@cletus can you plz tell me about the stable version of HTML2PDF. The above link example has deprecated

Grim typo becomes more grim: Memory usage ratio is on the order of 10,000-to-1 lol

Convert HTML + CSS to PDF with PHP? - Stack Overflow

php html css pdf pdf-generation
Rectangle 27 123

After some investigation and general hair-pulling the solution seems to be HTML2PDF. DOMPDF did a terrible job with tables, borders and even moderately complex layout and htmldoc seems reasonably robust but is almost completely CSS-ignorant and I don't want to go back to doing HTML layout without CSS just for that program.

After some investigation and general hair-pulling the solution seems to be HTML2PDF. DOMPDF did a terrible job with tables, borders and even moderately complex layout and htmldoc seems reasonably robust but is almost completely CSS-ignorant and I don't want to go back to doing HTML layout without CSS just for that program.

HTML2PDF looked the most promising but I kept having this weird error about null reference arguments to node_type. I finally found the solution to this. Basically, PHP 5.1.x worked fine with regex replaces (preg_replace_*) on strings of any size. PHP 5.2.1 introduced a php.ini config directive called pcre.backtrack_limit. What this config parameter does is limits the string length for which matching is done. Why this was introduced I don't know. The default value was chosen as 100,000. Why such a low value? Again, no idea.

HTML2PDF looked the most promising but I kept having this weird error about null reference arguments to node_type. I finally found the solution to this. Basically, PHP 5.1.x worked fine with regex replaces (preg_replace_*) on strings of any size. PHP 5.2.1 introduced a php.ini config directive called pcre.backtrack_limit. What this config parameter does is limits the string length for which matching is done. Why this was introduced I don't know. The default value was chosen as 100,000. Why such a low value? Again, no idea.

What's horrifying about this is that when the limit is exceeded, the replace just silently fails. At least if an error had been raised and logged you'd have some indication of what happened, why and what to change to fix it. But no.

What's horrifying about this is that when the limit is exceeded, the replace just silently fails. At least if an error had been raised and logged you'd have some indication of what happened, why and what to change to fix it. But no.

So I have a 70k HTML file to turn into PDF. It requires the following php.ini settings:

So I have a 70k HTML file to turn into PDF. It requires the following php.ini settings:

Now the astute reader may have noticed that my HTML file is smaller than 100k. The only reason I can guess as to why I hit this problem is that html2pdf does a conversion into xhtml as part of the process. Perhaps that took me over (although nearly 50% bloat seems odd). Whatever the case, the above worked.

Now the astute reader may have noticed that my HTML file is smaller than 100k. The only reason I can guess as to why I hit this problem is that html2pdf does a conversion into xhtml as part of the process. Perhaps that took me over (although nearly 50% bloat seems odd). Whatever the case, the above worked.

Now, html2pdf is a resource hog. My 70k file takes approximately 5 minutes and at least 500-600M of RAM to create a 35 page PDF file. Not quick enough (by far) for a real-time download unfortunately and the memory usage puts the memory usage ratio in the order of 1000-to-1 (600M of RAM for a 70k file), which is utterly ridiculous.

Now, html2pdf is a resource hog. My 70k file takes approximately 5 minutes and at least 500-600M of RAM to create a 35 page PDF file. Not quick enough (by far) for a real-time download unfortunately and the memory usage puts the memory usage ratio in the order of 1000-to-1 (600M of RAM for a 70k file), which is utterly ridiculous.

me too "HTML2PDF" is the best php code that I ever test!

@cletus can you plz tell me about the stable version of HTML2PDF. The above link example has deprecated

@cletus can you plz tell me about the stable version of HTML2PDF. The above link example has deprecated

Grim typo becomes more grim: Memory usage ratio is on the order of 10,000-to-1 lol

Convert HTML + CSS to PDF with PHP? - Stack Overflow

php html css pdf pdf-generation
Rectangle 27 123

After some investigation and general hair-pulling the solution seems to be HTML2PDF. DOMPDF did a terrible job with tables, borders and even moderately complex layout and htmldoc seems reasonably robust but is almost completely CSS-ignorant and I don't want to go back to doing HTML layout without CSS just for that program.

HTML2PDF looked the most promising but I kept having this weird error about null reference arguments to node_type. I finally found the solution to this. Basically, PHP 5.1.x worked fine with regex replaces (preg_replace_*) on strings of any size. PHP 5.2.1 introduced a php.ini config directive called pcre.backtrack_limit. What this config parameter does is limits the string length for which matching is done. Why this was introduced I don't know. The default value was chosen as 100,000. Why such a low value? Again, no idea.

What's horrifying about this is that when the limit is exceeded, the replace just silently fails. At least if an error had been raised and logged you'd have some indication of what happened, why and what to change to fix it. But no.

So I have a 70k HTML file to turn into PDF. It requires the following php.ini settings:

Now the astute reader may have noticed that my HTML file is smaller than 100k. The only reason I can guess as to why I hit this problem is that html2pdf does a conversion into xhtml as part of the process. Perhaps that took me over (although nearly 50% bloat seems odd). Whatever the case, the above worked.

Now, html2pdf is a resource hog. My 70k file takes approximately 5 minutes and at least 500-600M of RAM to create a 35 page PDF file. Not quick enough (by far) for a real-time download unfortunately and the memory usage puts the memory usage ratio in the order of 1000-to-1 (600M of RAM for a 70k file), which is utterly ridiculous.

@cletus can you plz tell me about the stable version of HTML2PDF. The above link example has deprecated

Grim typo becomes more grim: Memory usage ratio is on the order of 10,000-to-1 lol

Convert HTML + CSS to PDF with PHP? - Stack Overflow

php html css pdf pdf-generation
Rectangle 27 4

The Zend Framework's Zend_Pdf is really good. It's on par with pdflib in terms of control of output and complexity and is more portable because its a pure php solution. That said, its slower and uses more memory than pdflib. Pecl modules are always more efficient than a php solution.

DOMPdf is the easiest way to make a pdf quickly. Like Mike said, feed it html and it outputs a pdf. Under the hood, it has the option to use either r&ospdf or pdflib as the rendering engine.

Zend PDF has significant (performance issues if you embed PNGs with transparencies)[framework.zend.com/issues/browse/ZF-3392]. Also Zend PDF is a very bad tool for changing meta-data on 3rd party PDFs because it cannot handle anything close to the full PDF standard and it crashes. However, if you do not need these features Zend PDF works pretty well and has good UTF-8 support.

Which one is the best PDF-API for PHP? - Stack Overflow

php pdf pdf-generation
Rectangle 27 4

The Zend Framework's Zend_Pdf is really good. It's on par with pdflib in terms of control of output and complexity and is more portable because its a pure php solution. That said, its slower and uses more memory than pdflib. Pecl modules are always more efficient than a php solution.

DOMPdf is the easiest way to make a pdf quickly. Like Mike said, feed it html and it outputs a pdf. Under the hood, it has the option to use either r&ospdf or pdflib as the rendering engine.

Zend PDF has significant (performance issues if you embed PNGs with transparencies)[framework.zend.com/issues/browse/ZF-3392]. Also Zend PDF is a very bad tool for changing meta-data on 3rd party PDFs because it cannot handle anything close to the full PDF standard and it crashes. However, if you do not need these features Zend PDF works pretty well and has good UTF-8 support.

Which one is the best PDF-API for PHP? - Stack Overflow

php pdf pdf-generation
Rectangle 27 0

This should work for you in this case.

ini_set("memory_limit", 64MB);
$html = generatePDF($data_to_pdf);
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('a4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();

file_put_contents('pdf/' . $tmp_name . '.pdf', $pdf);

php - Create PDF file with BIG html CODE - Stack Overflow

php memory pdf-generation dompdf memory-limit
Rectangle 27 0

<?php
/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2015 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    ##VERSION##, ##DATE##
 */

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

date_default_timezone_set('Europe/London');


/** PHPExcel_IOFactory */
require_once ('/Library/WebServer/Documents/library/excel/PHPExcel/IOFactory.php');


//  Change these values to select the Rendering library that you wish to use
//      and its directory location on your server
//$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
//$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
//$rendererLibrary = 'tcPDF5.9';
//$rendererLibrary = 'mPDF5.4';
$rendererLibrary = 'dompdf';
$rendererLibraryPath = '/Library/WebServer/Documents/library/'.$rendererLibrary;

$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
    ->setLastModifiedBy("Maarten Balliauw")
    ->setTitle("PDF Test Document")
    ->setSubject("PDF Test Document")
    ->setDescription("Test document for PDF, generated using PHP classes.")
    ->setKeywords("pdf php")
    ->setCategory("Test result file");

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment; filename="swap.pdf');
       header('Cache-Control: max-age=0');
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', 'Hello')
    ->setCellValue('B2', 'world!')
    ->setCellValue('C1', 'Hello')
    ->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A4', 'Miscellaneous glyphs')
    ->setCellValue('A5', '');

echo date('H:i:s') , " Hide grid lines" , EOL;
$objPHPExcel->getActiveSheet()->setShowGridLines(true);

echo date('H:i:s') , " Set orientation to landscape" , EOL;
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);


echo date('H:i:s') , " Write to PDF format using {$rendererName}" , EOL;

if (!PHPExcel_Settings::setPdfRenderer(
        $rendererName,
        $rendererLibraryPath
    )) {
    die(
        'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
        EOL .
        'at the top of this script as appropriate for your directory structure'
    );
}


$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setSheetIndex(0);
//$objWriter->save('phpexcel_pdf.pdf');
$objWriter->save('php://output');
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo date('H:i:s') , " File written to " , str_replace('.php', '_'.$rendererName.'.pdf', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
// Echo memory usage
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;


// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

// Echo done
echo date('H:i:s') , " Done writing files" , EOL;
echo 'File has been created in ' , getcwd() , EOL;

    enter code here

pdf generation - phpExcel writer using tcpdf shows binary contents on ...

pdf-generation phpexcel tcpdf
Rectangle 27 0

No, 64M is not sufficient hence the "Allowed memory"-error. You have to find the php.ini of the CLI and change it there. The alternative is to change the setting via command line - like php -d memory_limit=256M /path/to/your/php-file. A segmentation fault occurs when a program tries to access invalid memory. It's likely a problem with the PHP-interpreter itself. It would be hard to find the reason, even with shell-access.

php - DomPDF > Allowed memory exhausted - Stack Overflow

php symfony-1.4 dompdf php-5.2
Rectangle 27 0

If you're using dompdf 0.6 beta, the memory error is the result of an infinite loop that dompdf enters when rendering tables. This is a known issue that I haven't been able to resolve.

(The error you see is pdf PHP Fatal error: Allowed memory size of 268435456 bytes exhausted)

php - dompdf memory issues - Stack Overflow

php memory pdf dompdf memory-leaks
Rectangle 27 0

DOMPDF will take a lot of memory depending on the HTML you are trying to convert. Propably more than 24M. First try with the highest you can give for memory. Also make sure you have the latest version.

If this doesn't work, you might have to find another solution for your problem.

I did both. Increase to 24MB. And I also have latest version. My html has many lines, but I am using bigger html information. Maybe table lines are too much. Lines around 20, and columns around 30.

Well I am not sure. I look at phpinfo. I move the memory up to 64MB and it worked! thanks!

php - DOMPDF - Allowed memory size of bytes exhausted - Stack Overflow

php dompdf
Rectangle 27 0

As indicated by cletus, the quickest solution for you with DOMPDF is probably going to be rendering each report in a separate process. You can write a master script that calls a child script (using exec) which performs the actual rendering. As you can see in this discussion on the DOMPDF support group, it does seem to have the potential to provide a bit of a boost in performance.

It's difficult to say what's going on otherwise regarding memory usage without some kind of example that demonstrates the problem. I don't believe there is much optimization of DOMPDF and the underlaying CPDF rendering engine for multiple instances in a single script. So the font is probably being loaded into memory each time, even though it could use a static variable to cache that data.

php - dompdf memory issues - Stack Overflow

php memory pdf dompdf memory-leaks
Rectangle 27 0

Your issue appears to be related to the PHP memory limit. You could try increasing the memory limit to prevent issues in the future.

Some image operations can be costly in terms of system memory. dompdf has to do a lot more processing with PNG to support the full feature set. The KB size of the image isn't the determining factor because the memory footprint has more to do with the dimensions of the image.

Codeigniter dompdf image not rendering - Stack Overflow

codeigniter dompdf
Rectangle 27 0

Then, divide the html, render each part into a separate file, and then merge them afterwards..

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

php - Create PDF file with BIG html CODE - Stack Overflow

php memory pdf-generation dompdf memory-limit
Rectangle 27 0

Second, the quickest solution is probably to print each report in a separate process to solve any memory leak problems. If this is running from the command line have the outer loop be something like a shell script that will start a process for each report. If it's run from the Web fork a process for each script if you're on an OS that can do that.

php - dompdf memory issues - Stack Overflow

php memory pdf dompdf memory-leaks
Rectangle 27 0

No, 64M is not sufficient hence the "Allowed memory"-error. You have to find the php.ini of the CLI and change it there. The alternative is to change the setting via command line - like php -d memory_limit=256M /path/to/your/php-file. A segmentation fault occurs when a program tries to access invalid memory. It's likely a problem with the PHP-interpreter itself. It would be hard to find the reason, even with shell-access.

php - DomPDF > Allowed memory exhausted - Stack Overflow

php symfony-1.4 dompdf php-5.2