【问题标题】:unable to save pdf file on client side usng dompdf无法在客户端保存 pdf 文件 usng dompdf
【发布时间】: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 ajaxpdf ajax 问题
  • 但如果你真的想通过 AJAX 做到这一点:stackoverflow.com/q/16086162/264628

标签: ajax codeigniter dompdf


【解决方案1】:

当您发送 ajax 调用时会发生什么? 文件在页面内打开或有错误? 我通常会向打开一个新窗口的视图发送响应,在该窗口中我将标题设置为强制浏览器打开保存对话框的二进制文件

$this->output->set_header('Content-Type: application/octet-stream');

【讨论】:

  • 我在 ajax 成功函数中得到 pdf 响应,没有错误。此外,当我尝试将文件保存在服务器上时,它工作得很好。但我想要下载 pdf 的对话框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
相关资源
最近更新 更多