How do you use this PHP library?

安稳与你 提交于 2019-12-04 20:15:27

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

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.

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

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