2

Those PDFs are the same code and same format and the difference is the text language. It can render first PDF, but do not success on second PDF. I have a test on the dompdf with the following code which cause error 500. Also, I use the Codeigniter framework. Is that a bug of dompdf or my bug?

$lang_array = array("chi","eng");
$i = 0;
foreach($lang_array as $lang)
{
    $html = "<!DOCTYPE html><html><body>M</body></html>";
    $this->dompdf->load_html($html);
    $this->dompdf->set_paper("A4", 'portrait');
    $this->dompdf->render();
    $output = $this->dompdf->output();
    file_put_contents($i.".pdf", $output);
    $i++;
}

2 Answers 2

5

As you've seen you can't reuse the existing dompdf instance. Once a render has been done there's a lot of internal objects created that dompdf currently doesn't have a method of removing.

But that's not to say you can't render more than one document. You'll just have to re-instantiate dompdf. Typically you would do something like the following:

foreach ($htmldocs as $index => $html) {
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->render();
  $output = $dompdf->output();
  file_put_contents($index . '.pdf', $output);
  unset($dompdf);
}

I've found in the past that you can achieve better performance by isolating the dompdf rendering component to a separate script and spawning a new process to perform the render. So your foreach would be something like:

foreach ($htmldocs as $index => $html) {
  file_put_contents($index . '.html', $html);
  exec('php dompdf.php ' . $index . '.html');
}

And dompdf.php would be something like:

$dompdf = new DOMPDF();
$dompdf->load_html_file($argv[1]);
$dompdf->render();
file_put_contents(str_replace('.html', '.pdf', $argv[1]), $dompdf->output());

How you accomplish this in Codeigniter depends on your implementation. You're referencing a class property (from the controller instance, perhaps?) so I suspect you may have to rewrite things a bit. If you add more of your implementation details we can guide you further.

Sign up to request clarification or add additional context in comments.

Comments

0
 $html = "<!DOCTYPE html><html><body>";
 $html .= "<div>DATA 1 (chi)</div>"; 
 $html .= "<div>DATA 1 (eng)</div>";
 $html .= "</body></html>";

 $this->dompdf->load_html($html)
 $dompdf->dompdf->set_paper("A4", 'portrait');
 $dompdf->render();
 $dompdf->stream("sample.pdf");

Try to put content on both div which fill whole A4 page. This separate the pdf into two parts. Page-1 and page-2

2 Comments

i need to export 2 PDF not one.
You min 2 different windows of pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.