How do you use this PHP library?

十年热恋 提交于 2019-12-06 12:43:32

问题


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>

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.


回答1:


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.

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);

?>

Recipe HTML file

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



回答2:


On the website you're looking at, clicking a link simply invokes the dompdf.php script with the path to the HTML file in question as the input. The target of the link is the iframe called "preview". It's not actually doing any transformation within the page itself, if that's what you're thinking.




回答3:


if you are using jQuery you can select recipe html like this:

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

and just create a form with actin link to your dompdf library page



来源:https://stackoverflow.com/questions/4783241/how-do-you-use-this-php-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!