您如何使用此PHP库?

您如何使用此PHP库?

问题描述:

我想根据Wordpress帖子(以下略作简化)中的内容动态创建PDF,在其中我想发送给div称为食谱". google.com/p/dompdf/"rel =" nofollow> DOMPDF (不是整个帖子的 ):

I'd like to create PDFs dynamically from content within a Wordpress post (slightly simplified below), where a div called "recipe" is what I want to send to DOMPDF (not the whole post):

<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库,而我似乎只是想知道如何做到这一点. 此处的文档.在此先感谢任何愿意提供帮助的人.

Never worked with a PHP library before and I just seem to be missing how to do this. Documentation here. Thanks in advance to anyone willing to help out.

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

You need to write a separate script that generates a PDF when given a corresponding recipe ID, and then link to it from your current HTML page. It looks like you're struggling because you're trying to do it all on one page.

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

I'm not particularly familiar with Wordpress, so I'm going to suggest using the buffer to output HTML: (untested)

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>
...