使用DOMPDF以pdf打开页面

时间:2013-09-09 16:17:55

标签: html css dompdf

我目前正在开发一个半动态页面,我使用一些GET功能来个性化它。我也回应了几个地方的日期。在本页底部,我想有一个按钮,让访问者可以选择以PDF格式下载/打开此页面。没有标题。我已经集成了DOMPDF,但是我无法让它正常工作并需要一些帮助。我在Stackoverflow上尝试过这里发现的一些东西,没有成功。

基本上,我需要在PDF中打印整页,但是在加载页面时不应该打开。但是按钮触发了。然后没有标题(一个特殊的div)。这可能吗?

<?php
    require_once("dompdf/dompdf_config.inc.php");

    $html =
        '<html><body>'.
        '<p>This is a test for '.
        '<?php echo htmlentities(substr(urldecode($_SERVER["QUERY_STRING"]), 1)); ?> </p>'.
        '<p>Thank you for reading.</p>'.
        '</body></html>';

    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    $dompdf->stream("the_inquiry.pdf");
    return true;
    ?>
<html>
<body>
<div class="shouldnotPrintToPDF">
Content.
</div>
<div class="shouldPrintToPDF">
Sensitive content.
<a href="the_inquiry.pdf">Open or save as PDF</a>
</div>
</body>
</html>

这基本上是我们的单页演示文稿。并且包含大量文本,所以我不会在这里介绍所有这些内容。但是这样,我必须在$ html =以及实际标签内写两次页面。并且PDF保存/打开选项从一开始就弹出,它不应该弹出。我还希望将echo htmlentities部分附加到实际的pdf名称..这可能吗? PDF打开并包含放入$ html =的内容。但不是由链接引发的。

更新: 当我完全按照你在这里执行的操作时,我收到错误“在此服务器上找不到请求的URL /inquiry.php&pdf=1。”我有一个页面,我试图在根级别打印pdf,但DOMPDF在/ dompdf ..我不知道这与它有什么关系吗?

更新: 当我编辑链接时,我会在新页面中获取所有这些信息,如下所示。

    [_parse_properties(margin:=1.2cm)(empty)_parse_properties]
[_parse_sections[section[_parse_properties(display:=-dompdf-page)
(counter-reset:=page)(empty)_parse_properties]#html#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]
_parse_sections][_parse_sections[section[_parse_properties(display:=block)
(empty)_parse_properties]#div##map##dt##isindex#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]
_parse_sections][_parse_sections[section[_parse_properties(page-break
-before:=avoid)(display:=block)(counter-increment:=page)
(empty)_parse_properties]#body#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(margin:=1em 
0)(empty)_parse_properties]#p##dl##multicol#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(margin-left:=40px)
(empty)_parse_properties]#dd#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(margin:=1em 
40px)(empty)_parse_properties]#blockquote#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(font-style:=italic)
(empty)_parse_properties]#address#section]
[section[_parse_properties(empty)_parse_properties]#empty#section]_parse_sections]
[_parse_sections[section[_parse_properties(display:=block)(text-align:=center)
(empty)_parse_properties]#center#section]

你知道它可能是由什么造成的吗?

突破:

当我激活DOMPDF_DPI时,它实际上以PDF格式打开,但现在所有文本都出现在PDF第二页的第一行。就像,所有文本都是相互叠加的。此外,当它打开PDF时,?&amp; pdf = 1包含在htmlentities查询字符串中,因为它应该是个性化页面以及PDF,所以它看起来非常混乱。

1 个答案:

答案 0 :(得分:1)

您可以设置dompdf来解析标准媒体类型(屏幕,打印,语音等)的CSS @media查询。默认情况下,dompdf会解析“屏幕”媒体类型样式,但您可以在配置文件中更改此样式。请参阅DOMPDF_DEFAULT_MEDIA_TYPE配置设置。然后,您只需要传递原始URL查询字符串,并附加一个变量,告诉页面呈现为PDF。如果您的原始网址类似于the_inquiry.php?name=Joe,那么您的PDF网址可能是the_inquiry.php?name=Joe&pdf=1

您的代码将类似于以下内容:

<?php
ob_start();
?>
<html>
<head>
  <style>
    @media print {
      .shouldnotPrintToPDF, .pdflink { display: none; }
    }
  </style>
</head>
<body>
  <div class="shouldnotPrintToPDF">
    Content.
  </div>
  <div class="shouldPrintToPDF">
    Sensitive content.
    <a href="<?php echo $_SERVER['PHP_SELF'] , '?' , http_build_query( $_GET ); ?>&amp;pdf=1" class="pdflink">Open or save as PDF</a>
  </div>
</body>
</html>
<?php
if ( isset( $_GET['pdf'] ) ) {
  require_once 'dompdf/dompdf_config.inc.php';
  $html = ob_get_clean();
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->render();
  $dompdf->stream("the_inquiry.pdf");
}
?>