how to convert multiple html files with Dompdf? how to convert multiple html files with Dompdf? codeigniter codeigniter

how to convert multiple html files with Dompdf?


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).


You need to merge the two files :

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