简体   繁体   English

您如何使用此PHP库?

[英]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): 我想根据Wordpress帖子中的内容动态创建PDF(以下略作简化),其中我要发送给DOMPDFdiv是“ recipe”( 不是整个帖子):

 <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. 以前从未使用过PHP库,而我似乎缺少如何做到这一点。 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. 您需要编写一个单独的脚本,当给出相应的配方ID时会生成PDF,然后从您当前的HTML页面链接到它。 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) 我对Wordpress不太熟悉,因此建议使用缓冲区输出HTML :( 未经测试)

recipe_pdf.php 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 配方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>
...

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. 在您正在查看的网站上,单击链接仅会调用dompdf.php脚本,并将相关HTML文件的路径作为输入。 The target of the link is the iframe called "preview". 链接的目标是称为“预览”的iframe。 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: 如果您使用的是jQuery,则可以选择以下食谱html:

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

and just create a form with actin link to your dompdf library page 并创建一个带有actin链接的表格到您的dompdf库页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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