Dompdf 创建新的输出 pdf 文档,我需要在一个文件中输出内容,如何解决?
我是这个图书馆的新手。我在没有作曲家的情况下安装了它,全部按照说明进行。 是的,它是 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?
I'm new with this library. I installed it without composer, do all by instructions.
Yes, it's wordpress, but I need to do my task exactly manually, not with plugins, and I want to learn that library, it seems to be a good thing.
That's how I install 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 has to be the output file. Now it's simple, nothing special
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<div class="test" style="font-family: Dejavu Sans, sans-serif;">
hello
</div>
<?php
What at all I plan to do? To attach pdf-test.pdf to user emails. There will be dynamic content, special, so that's why plugins wouldn't help me.
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;
}
In emails users have to open that attached pdf-file in browser. With random pdf from the Internet it works great, so the idea is not dead, now I want to create my own.
When I load or reload the page, the new pdf-file is creating and downloading as document.pdf, and if I save it I successfully open it in the browser, I see my 'hello'.
But that's not what I need - I need to generate the content not to new document.pdf file, I need to generate it into pdf-test.pdf, that I will attach where I need. It has to be with dynamic content, but one for all.
How to change the output way and to open not empty pdf-test.pdf in browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$dompdf->stream();
输出到浏览器,因此如果您不希望发生这种情况,请删除该行。$dompdf->output();
将 PDF 创建为字符串,以便您可以将其另存为文件:$dompdf->stream();
outputs to browser so if you don't want that happening remove that line.$dompdf->output();
creates the PDF as a string so you could save it as a file: