html - 使用 DOMPDF 打开 pdf 页面

标签 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 时,?&pdf=1 包含在 htmlentities 查询字符串中,这看起来非常困惑,因为它应该是个性化页面以及 PDF。

最佳答案

您可以设置 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");
}
?>

关于html - 使用 DOMPDF 打开 pdf 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18702604/

相关文章:

html - 良好的编码习惯/模块化 HTML 结构

ios - 高效地迭代 subview 并返回过滤后的 UIView 子集

javascript - 折叠并展开所有 bootstrap 3 Accordion

php - 如何使用 Dompdf 转换多个 html 文件?

php - dompdf 和 img 标签,图像不会显示

python - 如何在 python 窗口中显示和管理 HTML

javascript - 如何在 Angular v1.2 中绑定(bind) html

css - 动态 Div 在鼠标悬停时更改宽度

jquery - 制作 9 个 div 的流畅 3x3 网格显示

php - 当我使用波斯文本时 DomPDF 输出被破坏