簡體   English   中英

您如何使用此PHP庫?

[英]How do you use this PHP library?

我想根據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> 

以前從未使用過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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM