我是這個圖書館的新手。我在沒有作曲家的情況下安裝了它,一切都按照說明進行。是的,它是 wordpress,但我需要完全手動完成我的任務,而不是使用插件,而且我想學習那個庫,這似乎是件好事。這就是我安裝dompdf的方式。
require get_template_directory() . '/pdf-test.php';
require_once get_template_directory() . '/assets/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
ob_start();
require(get_template_directory().'/pdf-test.php');
$content_pdf = ob_get_clean();
$dompdf->loadHtml( $content_pdf);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$output= $dompdf->output();
$dompdf->stream();
pdf-test.php 必須是輸出檔案。現在很簡單,沒什么特別的
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<div class="test" style="font-family: Dejavu Sans, sans-serif;">
hello
</div>
<?php
我到底打算做什么?將 pdf-test.pdf 附加到用戶電子郵件中。會有動態內容,特別的,所以這就是插件對我沒有幫助的原因。
add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3);
function attach_terms_conditions_pdf_to_email ( $attachments , $id, $object ) {
$your_pdf_path = get_template_directory() . '/pdf-test.pdf';
$attachments[] = $your_pdf_path;
return $attachments;
}
在電子郵件中,用戶必須在瀏覽器中打開附加的 pdf 檔案。使用互聯網上的隨機 pdf 效果很好,所以這個想法還沒有死,現在我想創建自己的。
當我加載或重新加載頁面時,新的 pdf 檔案正在創建并下載為 document.pdf,如果我保存它,我會在瀏覽器中成功打開它,我會看到我的“你好”。
但這不是我需要的 - 我需要生成內容而不是新的 document.pdf 檔案,我需要將其生成為 pdf-test.pdf,我將附加到我需要的地方。它必須包含動態內容,但適用于所有內容。如何更改輸出方式并在瀏覽器中打開不為空的pdf-test.pdf?
uj5u.com熱心網友回復:
$dompdf->stream();輸出到瀏覽器,因此如果您不希望發生這種情況,請洗掉該行。
$dompdf->output();將 PDF 創建為字串,以便您可以將其保存為檔案:
$output = $dompdf->output();
file_put_contents(get_template_directory() . '/pdf-test.pdf', $output);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/432575.html
