我正在使用mpdf,并通过mpdf :: writeHTML()将一堆HTML写入pdf对象。有什么方法可以直接将其转储回浏览器,而不是输出PDF?因此,不是创建PDF而是仅将其写为网页?
我想为用户提供PDF或网页的选项,而不是为每一行分支以产生echo或writeHTML,我想构建文档然后输出Web或PDF。
编辑添加:
类似这样的东西:
$mpdf = new mpdf();
$mpdf->writeHTML( "<p>Hello World</p>" );
$mpdf->addPage( 'L' );
$mpdf->writeHTML( "<p>Lorem ipsum egg foo yung.</p>" );
if( $_GET['format'] == 'pdf' ) {
$mpdf->output(); //spit out a PDF
} elseif ( $_GET['format'] == 'web' ) {
echo $mpdf->contents_as_html(); // write a web page
}
我目前正在将每一行写成一个巨大的字符串,然后将该字符串传递给mpdf :: writeHTML()或echo;但这不允许我使用各种mpdf函数,例如addPage(),bookmark()等。
您可以通过更改第二个参数来使用mPDF选择输出。
I = send the file inline to the browser.
F enter code here= save to a local file with the name given by $filename.
S = return the document as a string. $filename is ignored.
D = send to the browser and force a file download with the name given by $filename.
输出mPDF:
$mpdf->Output($filename, "I"); // Change "I" to your preferred output
如果选择在浏览器中输出文件,请确保将输出目标定位在空白页面上。否则,页眉和页脚可能会产生干扰。
Artikel mPDF输出:https://mpdf.github.io/reference/mpdf-functions/output.html
根据@CBroe的建议,这就是我所做的。调用writeHTML()
时,它将写入内部变量$this->strHTML
,然后执行其正常过程。如果对象强制转换为字符串,则返回$this->strHTML
class myPDF extends Mpdf {
private $strHtml = '';
public function writeHTML( $html, $mode = 0, $init = true, $close = true ) {
$this->strHtml .= $html . "\n";
return parent::writeHTML( $html, $mode, $init, $close );
}
public function __toString() {
return $this->strHtml;
}
}