how to convert multiple html files with Dompdf?

心已入冬 提交于 2019-12-31 02:08:43

问题


What I am Doing:

I am using a PHP library called Dompdf to convert my HTML file into PDF. I have successful converted a single HTML file to PDF.

What I am trying to do:

Now I have multiple reports I want to convert them into PDF file.

What I am doing:

I have a example here in which I'm a getting a form from database and another one by direct from view.

Code:

$result['patients'] = $this->reports_model->get_xray_reports($id); // getting form from database

require_once "dompdf/dompdf_config.inc.php"; // include dompdf
$dompdf = new DOMPDF();
$dompdf->set_paper("A4");
$html = $this->load->view('wilcare/form_set_page_15',"",true);// loading direct from view
//this file is not converting
$html = $this->load->view('wilcare/report1',$result,true);//loading from db

$dompdf->load_html($html)

$dompdf->render();
$dompdf->stream("hello.pdf",array('Attachment'=>0));

Problem

It is only converting one file.

I have also tried this:

  $html = $this->load->view('wilcare/form_set_page_15',"",true);
  $html1 = $this->load->view('wilcare/report1',$result,true);

  $dompdf->load_html($html && $html1);

回答1:


You want to create on PDF file from two HTML files with DOMPDF.

This is not possible. DOMPDF supports the creation of one PDF file from one HTML document.

Internally DOMPDF uses DOMDocument to represent the HTML document. So if you want to put two HTML files into DOMPDF you have to merge these first.

DOMPDF HTML merge behavior

Testing did reveal that you can concatenate mutlitple <head>...</head><body>...</body> sections (each of these pairs will start a new page in DOMPDF) but they need to be inside the same <html> document element (DOMPDF v0.6.1).

If you merge elements inside the <body> element then a single page is created.

If you append multiple <html> documents just after another (as suggested by Simo), the only the first document is put into the PDF.

I add two PHP examples:

  1. The first one shows how to merge two HTML documents into a single body which DOMPDF renders as one page (PDF)
  2. The second shows how to merge them into the two <head>...</head><body>...</body> which DOMPDF renders as two pages (PDF)

PHP Example Code HTML document merging DOMPDF single body

E.g. you want to append the body contents of the second HTML document to the end of the body of the first one (all objects of type DOMDocument). :

$doc1 = create_document(1);
$doc2 = create_document(2);

$doc1Body = $doc1->getElementsByTagName('body')->item(0);
$doc2Body = $doc2->getElementsByTagName('body')->item(0);
foreach ($doc2Body->childNodes as $child) {
    $import = $doc1->importNode($child, true);
    if ($import) {
        $doc1Body->appendChild($import);
    }
}
$doc1->saveHTMLFile('php://output');

Exemplary HTML Output (of this full Example Code):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Document 1</title>
</head>
<body>
<h1>Document 1</h1>
<p>Paragraph 1.1</p>
<p>Paragraph 1.2</p>
<h1>Document 2</h1>
<p>Paragraph 2.1</p>
<p>Paragraph 2.2</p>
</body>
</html>

Creates this PDF: http://tinyurl.com/nrv78br (one page)

PHP Example Code HTML document merging DOMPDF multiple pages

Here the example with a new page on the second HTML document:

foreach ($doc2->documentElement->childNodes as $child) {
    $import = $doc1->importNode($child, true);
    if ($import) {
        $doc1->documentElement->appendChild($import);
    }
}
$html = $doc1->saveHTML();

Exemplary HTML Output:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Document 1</title></head><body>
<h1>Document 1</h1>
<p>Paragraph 1.1</p>
<p>Paragraph 1.2</p>
</body><head><title>Document 2</title></head><body>
<h1>Document 2</h1>
<p>Paragraph 2.1</p>
<p>Paragraph 2.2</p>
</body></html>

Creates this PDF: http://tinyurl.com/oe4odmy (two pages)


Depending on what you need with your documents, you might need to do additional steps, like merging the title and CSS rules (which hopefully are portable).




回答2:


You need to merge the two files :

//this file is not converting
$html .= $this->load->view('wilcare/report1',$result,true);


来源:https://stackoverflow.com/questions/27657525/how-to-convert-multiple-html-files-with-dompdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!