【问题标题】:Open page as pdf with DOMPDF使用 DOMPDF 以 pdf 格式打开页面
【发布时间】:2013-09-13 04:48:08
【问题描述】:

我目前正在开发一个半动态页面,我在其中使用一些 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 时,?&pdf=1 包含在 htmlentities 查询字符串中,这看起来非常混乱,因为它应该是一个个性化页面以及 PDF。

【问题讨论】:

  • 显示您的代码。你到底尝试了什么?
  • 刚刚添加了一些信息,不能粘贴整个页面,因为它包含一些敏感信息。

标签: html css dompdf


【解决方案1】:

您可以设置 dompdf 来解析标准媒体类型(屏幕、打印、语音等)的 CSS @media 查询。默认情况下,dompdf 解析“屏幕”媒体类型样式,但您可以在配置文件中更改它。请参阅DOMPDF_DEFAULT_MEDIA_TYPE 配置设置。然后,您只需要传递带有附加变量的原始 URL 查询字符串,告诉页面呈现为 PDF。如果您的原始 URL 类似于 the_inquiry.php?name=Joe,那么您的 PDF URL 可能是 the_inquiry.php?name=Joe&amp;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");
}
?>

【讨论】:

  • 当我完全按照您在此处执行的操作时,我收到错误消息“在此服务器上找不到请求的 URL /inquiry.php&pdf=1。”我有我试图在根级别以 pdf 格式打印的页面,但 DOMPDF 在 /dompdf 中。我不知道这是否与它有关?
  • @John-EilifEliassen 啊,对不起。我忘记了“?”在 URL 中,它应该出现 inquiry.php?&amp;pdf=1。我更新了代码示例。
  • 你能看看我的更新,让我知道我做错了什么吗?
  • @John-EilifEliassen 您是否将 $_dompdf_debug 设置为 true?如果是这样,请确保禁用所有调试选项。
  • 嗨@BrianS,什么调试?我在 dompdf_config.custom.inc.php 中找到(DEBUG_LAYOUT、DEBUGCSS、DEBUGKEEPTEMP、DEBUGPNG)。有些是真的,有些是假的。在第 357 行的 dompdf_config.inc.php 中,我有这个: global $_dompdf_debug;$_dompdf_debug = false;所以我想我已将调试设置为 false。
猜你喜欢
  • 2015-07-03
  • 2012-05-12
  • 1970-01-01
  • 2021-10-30
  • 2013-04-22
  • 2018-08-27
  • 2023-03-19
  • 2014-07-31
  • 2019-12-03
相关资源
最近更新 更多