题
我正在使用HTML2PDF类生成PDF。在我的问题中,它为HTML代码生成了PDF,但没有给出对话框选项下载该PDF。请帮助我的Cose关注。
<?php
ob_start();
include(dirname(__FILE__).'/res/pdf_demo.php');
$content = ob_get_clean();
// conversion HTML => PDF
require_once(dirname(__FILE__).'/../html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('P','A4', 'fr', false, 'ISO-8859-15');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$html2pdf->Output('pdf_demo.pdf');
}
catch(HTML2PDF_exception $e) { echo $e; }
?>
解决方案
要从浏览器提供下载,您需要添加标题以进行附件...
header("Content-Disposition: attachment; filename=sample.pdf");
在页面开始时添加上述代码,然后继续进行HTML2PDF转换。
其他提示
从文档中,方法输出
/**
* Send the document to a given destination: string, local file or browser.
* Dest can be :
* I : send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
* D : send to the browser and force a file download with the name given by name.
* F : save to a local server file with the name given by name.
* S : return the document as a string. name is ignored.
* FI: equivalent to F + I option
* FD: equivalent to F + D option
* true => I
* false => S
*
更改这条线 $html2pdf->Output('pdf_demo.pdf');
到$html2pdf->Output('pdf_demo.pdf', 'D');
它将迫使浏览器自动下载PDF文件。
将PDF发送到带有特定名称的浏览器
$ html2pdf-> output('document_name.pdf');
$ html2pdf-> output('document_name.pdf',false);
$ html2pdf-> output('document_name.pdf','');
$ html2pdf-> output('document_name.pdf','i');
强迫浏览器下载带有特定名称的PDF文件
$ html2pdf-> output('document_name.pdf','d');
在服务器上写入PDF文件的内容
注意,必须谨慎使用在服务器上的文字。没有对文件的存在进行验证
$ html2pdf-> output('Directory/filename_xxxx.pdf','f');
检索PDF的内容,然后做任何您想做的事
$ content_pdf = $ html2pdf-> output('',true);
$ content_pdf = $ html2pdf-> output('','s');
不隶属于 StackOverflow