【问题标题】:How do you use this PHP library?你如何使用这个 PHP 库?
【发布时间】:2011-06-14 13:42:17
【问题描述】:

我想从 Wordpress 帖子中的内容动态创建 PDF(下面稍微简化),其中一个名为“recipe”的div 是我想要发送给DOMPDF 的内容(不是 整篇文章):

<div class="post">
    <div>
        <!-- some stuff I don't want in the PDF -->
    </div>
    <div class="recipe">
        <?php more_fields('recipe-name'); ?>
        <?php more_fields('recipe-method'); ?>
        <a href="<?php /*something here that calls DOMPDF, 
                         delivers the contents of #recipe, and causes
                         render and stream of PDF to browser.*/ ?>"
    class="print-recipe">Get a PDF of this recipe.</a>
    </div>
</div>

以前从未使用过 PHP 库,我似乎只是想念如何做到这一点。 Documentation here。提前感谢任何愿意提供帮助的人。

【问题讨论】:

    标签: php wordpress pdf dompdf


    【解决方案1】:

    您需要编写一个单独的脚本,在给定相应的配方 ID 时生成 PDF,然后从您当前的 HTML 页面链接到它。看起来您正在苦苦挣扎,因为您试图在一个页面上完成所有操作。

    我对Wordpress不是特别熟悉,所以我会建议使用缓冲区来输出HTML:(未测试)

    recipe_pdf.php

    <?
      /* wordpress initializers go here */
      require_once("dompdf_config.inc.php"); // or whatever your path is
    
      $id = $_GET['id']; //requested recipe ID
      /* fetch recipe data here */
    
      ob_start(); // begin buffering PDF content
    ?>
    
    
    **output recipe HTML to be used for PDF generation here**
    
    
    <?
    
      $html = ob_get_clean();  // grab buffered content and stop buffering
    
      //create and output the PDF as a stream (download dialog)
      $dompdf = new DOMPDF();
      $dompdf->load_html($html);
      $dompdf->render();
      $filename = "your-recipe-filename.pdf";
      $dompdf->stream($filename);
    
    ?>
    

    配方 HTML 文件

    ...
    <div class="recipe">
      <?php more_fields('recipe-name'); ?>
      <?php more_fields('recipe-method'); ?>
      <a href="recipe_pdf.php?id=<? // output recipe ID ?>">
        Get a PDF of this recipe.
      </a>
    </div>
    ...
    

    【讨论】:

    • 我支持这个解决方案。看起来很扎实,经过深思熟虑:)
    • @Jeriko 大家好,我想做同样的事情,只是我不像你们那样精通php!除了按原样复制和粘贴代码之外,我还需要进行哪些更改才能使其正常工作?
    • @Rob 我已经一年多没有写 PHP 了,所以我不确定我能提供多少帮助。您需要在require 调用中包含您自己的dompdf 路径,并在标记为fetch recipe data here 的地方进行对象获取(数据库访问)。除此之外,实际的 pdf 输出数据位于相关位置,您可以根据需要设置$filename。你有什么具体的问题吗?
    【解决方案2】:

    在您正在查看的网站上,单击链接只需调用 dompdf.php 脚本,并将相关 HTML 文件的路径作为输入。链接的目标是称为“预览”的 iframe。如果您是这样想的话,它实际上并没有在页面本身内进行任何转换。

    【讨论】:

      【解决方案3】:

      如果您使用的是 jQuery,您可以像这样选择配方 html:

      $('.recipe').html();
      

      然后创建一个带有actin 链接的表单,链接到您的 dompdf 库页面

      【讨论】:

      • 这个解决方案会让 JS 被停用的人无法下载 PDF。
      • 部分问题是如何将 .recipe 块发送到 dompdf 库。这是解决方案之一 :)
      猜你喜欢
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2017-12-09
      • 1970-01-01
      相关资源
      最近更新 更多