简体   繁体   中英

How do you use this PHP library?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM