[英]How do you use this PHP library?
我想根据Wordpress帖子中的内容动态创建PDF(以下略作简化),其中我要发送给DOMPDF的div
是“ 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>
以前从未使用过PHP库,而我似乎缺少如何做到这一点。 文档在这里 。 在此先感谢任何愿意提供帮助的人。
您需要编写一个单独的脚本,当给出相应的配方ID时会生成PDF,然后从您当前的HTML页面链接到它。 您似乎正在努力,因为您试图在一页上完成所有操作。
我对Wordpress不太熟悉,因此建议使用缓冲区输出HTML :( 未经测试)
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>
...
在您正在查看的网站上,单击链接仅会调用dompdf.php脚本,并将相关HTML文件的路径作为输入。 链接的目标是称为“预览”的iframe。 如果您正在考虑的话,它实际上并没有在页面内部进行任何转换。
如果您使用的是jQuery,则可以选择以下食谱html:
$('.recipe').html();
并创建一个带有actin链接的表格到您的dompdf库页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.