如何转换多个html文件与Dompdf? [英] how to convert multiple html files with Dompdf?

查看:161
本文介绍了如何转换多个html文件与Dompdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么:



我使用的PHP库是 Dompdf 将我的HTML文件转换为PDF。
我已经将一个HTML文件成功转换为PDF。



我想做什么:



现在我有多个报告要转换为PDF文件。



我在做什么:





代码: / p>

  $ result ['patients'] = $ this-> reports_model-> get_xray_reports($ id); //从数据库获取表单

require_oncedompdf / dompdf_config.inc.php; // include dompdf
$ dompdf = new DOMPDF();
$ dompdf-> set_paper(A4);
$ html = $ this-> load-> view('wilcare / form_set_page_15',,true); //从视图加载直接
//此文件不转换
$ html = $ this-> load-> view('wilcare / report1',$ result,true); //从db加载b

$ dompdf-> load_html

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



问题



文件。



我也试过了:

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

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


解决方案

您要从两个HTML文件与DOMPDF。



这是不可能的。 DOMPDF支持从一个HTML文档创建一个PDF文件。



内部DOMPDF使用 DOMDocument 表示HTML文档。因此,如果你想把两个HTML文件放入DOMPDF,你必须首先合并这些文件。



DOMPDF HTML合并行为



测试显示您可以连接mutlitple < head> ...< / head>< body> ...< / body> 的这些对将在DOMPDF中启动一个新页面),但是它们需要在同一个< html> 文档元素(DOMPDF v0.6.1)。



如果您在< body> 元素中合并元素,那么将创建一个页面。



如果将多个< html> 文档附加在一个文件之后(如Simo建议的),则只有第一个文档被放入PDF。 / p>

我添加了两个PHP示例:


  1. 第一个示例如何合并两个HTML文档合并为一个单一主体,DOMPDF呈现为一个页面( PDF

  2. 第二个显示如何将它们合并到两个< head> ...< / head>< body> ...< / body> 呈现为两页( PDF



PHP示例代码HTML文档合并DOMPDF单一实体



您要将第二个HTML文档的正文内容附加到第一个HTML文档的正文结尾(所有类型为 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');

示例HTML输出(完整的示例代码):

 <!DOCTYPE html PUBLIC // DTD HTML 4.0 Transitional // ENhttp://www.w3.org/TR/REC-html40/loose.dtd\"> 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>文件1< / title>
< / head>
< body>
< h1>文件1< / h1>
< p>第1.1段< / p>
< p>第1.2段< / p>
< h1>文件2< / h1>
< p>第2.1段< / p>
< p>第2.2段< / p>
< / body>
< / html>

创建此PDF: http://tinyurl.com/nrv78br (一页)



PHP示例代码HTML文档合并DOMPDF多页



这里是第二个HTML文档上有一个新页面的示例:

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

示例HTML输出:

 <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.0 Transitional // ENhttp://www.w3.org/TR/REC-html40/loose.dtd\"> ; 
< html>< head>< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>< title>文件1< / title> ; / head>< body>
< h1>文件1< / h1>
< p>第1.1段< / p>
< p>第1.2段< / p>
< / body>< head>< title>文件2< / title>< / head>< body&
< h1>文件2< / h1>
< p>第2.1段< / p>
< p>第2.2段< / p>
< / body>< / html>

创建此PDF: http://tinyurl.com/oe4odmy (两页)






您需要使用您的文档,您可能需要执行其他步骤,例如合并标题和CSS规则(希望是可移植的)。


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

解决方案

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

这篇关于如何转换多个html文件与Dompdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆