【发布时间】:2014-09-23 10:10:50
【问题描述】:
我正在使用 dompdf 库将我的 html 内容转换为 pdf。
我正在使用 ajax 发布请求将 html 内容从视图发送到控制器。
在这个控制器中,我调用 dompdf helper 类将我的 html 转换为 pdf 文件并将其保存在客户端。
这是我的 ajax 请求:
$.ajax({
type: "POST",
url: "<?php echo $this->config->base_url('reports/exportPDF')?>",
data:
{
report : totalHtml
},
success: function (response) {
console.log(response);
},
error: function (err) {
//console.log(err);
}
});
这是我提交 html 内容的控制器代码:
$this->load->helper('url');
$this->load->helper(array('dompdf', 'file'));
$html = $this->input->post('report');
$data = pdf_create($html);
最后,这是我的 dompdf 助手库集成代码:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function pdf_create($html, $filename='', $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$htmlData = $dompdf->load_html($html);
$dompdf->set_paper("a4", "landscape" );
$dompdf->render();
$dompdf->stream("report.pdf");
}
?>
根据 dompdf 文档,应该会出现下载对话框,但我无法得到它。
我应该如何使用 dompdf 在客户端保存 pdf 文件?
提前致谢。
【问题讨论】:
-
通过 AJAX 做您想做的事情并非易事。相反,您应该正常提交到“reports/exportPDF”(如果您需要将内容发布到服务器,请创建一个隐藏表单)。
-
建议查看dompdf ajax 和pdf ajax 问题
-
但如果你真的想通过 AJAX 做到这一点:stackoverflow.com/q/16086162/264628
标签: ajax codeigniter dompdf