如何修复:domPDF生成的输出结果?

时间:2016-06-20 04:13:02

标签: php dompdf

嗨,这就是我对domPDF的看法。这是我的最终结果,但显然我不想要那样,我希望我的页面以PDF格式打印。

我需要在用户浏览器中下载文件,而不是在localhost中下载。

enter image description here

这是我的javascript:

$("#submit").click(function(){          
        $.post("converter/converter.php", {strHtml: $(".container-   fluid").html()}, function(data, status){
                alert("Data: " + data);
        });
    });

这是我的php:

<?php
    require_once '../dompdf/autoload.inc.php';
    use Dompdf\Dompdf;

    $html = $_POST['strHtml'];

    $dompdf = new Dompdf();

    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $dompdf->output('test.pdf', 'F');
 ?>

这是我想用pdf显示的内容:

enter image description here

2 个答案:

答案 0 :(得分:0)

如果要将生成的pdf文件保存到磁盘。你不需要file_put_content()。但是你将两个参数传递给->output()方法,如下所示:

$dompdf->output($path_to_new_file, 'F');

答案 1 :(得分:0)

如果您要提交浏览器中存在的页面,您必须捕获内容(如您所做),然后,不是通过AJAX请求提交,而是将该内容推送到表单并提交该表单。

$("#submit").click(
  function() {
    $('#strHtml').val($(".container-fluid").html());
    $('#makePdfForm').submit();
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class=".container-fluid">
  <p>Some stuff here, rendered dynamically on the client</p>
</div>

<form action="converter/converter.php" method="post" id="makePdfForm">
  <input name="strHtml" id="strHtml" type="hidden" value="">
  <button type="button" id="submit">Make PDF</button>
</form>

然后你的PHP变化很小:

<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$html = $_POST['strHtml'];
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream('test.pdf');
?>