symfony - SwiftMailer 附件 pdf 由 Dompdf 动态生成
问题描述
首先寻求帮助:)
我在 Symfony 4.4 框架上工作,我让控制器生成 PDF 并通过邮件附件发送,但我想直接在控制器上发送 pdf 而不是系统上的库存文件发送并删除它们。
$pdf = new Dompdf( $pdfOptions );
$html = $this->render('recommendations/synthesePdf.html.twig', [
'products' => $products,
'recommendations' => $recommendations,
'customer' => $customer,
'cultureTotal' => $cultureTotal
]);
$pdf->loadHtml( $html->getContent() );
$pdf->setPaper('A4', 'portrait');
$pdf->render();
//-- SEND PDF TO USER
$message = (new \Swift_Message('Nouvelle recommendation disponible'))
->setFrom('send@example.com')
//->setTo( $customer->getEmail() )
->setTo( '***' )
->setBody(
$this->renderView(
'emails/recommendation.html.twig', [
'identity' => $customer->getIdentity()
]
),
'text/html'
)
->attach( \Swift_Attachment::fromPath( $pdf->output() ) )
;
$mailer->send($message);
您好,使用 $pdf->output / $pdf->getHtml 但如果您有任何建议,它不起作用:)
谢谢
解决方案
你可以像使用 Symfony 的 FileSystem 组件一样 =>
安装
composer require symfony/filesystem
用法
use Symfony\Component\Filesystem\Filesystem; $filesystem = new Filesystem();
删除文件
$filesystem->remove(['/path/to/file']);
在您的情况下,您可以保存文件 pdf 然后发送,如果发送了邮件,则删除文件
文档:文件系统组件
推荐阅读
- php - 为什么只有一个项目显示在 foreach 循环之外?
- c# - c# ExecuteReader 要求命令有一个事务,当分配给命令的连接处于挂起的本地事务中时
- oauth - 错误 500 Cognito 'token endpoint' /oauth2/token
- c# - c# 使用getelementbyid,getelementsbytagname无法获取内容页面中的元素
- android - 如何从服务中获取当前的键盘语言?
- c# - 将数组数组转换为json
- pyspark - 在集群模式下通过 spark-submit 的 pyarrow 失败
- multi-tenant - SSDT - 如何更改每个构建配置的数据库对象名称
- java - java.lang.NoClassDefFoundError 突然出现在以前可以工作的代码上 - Eclipse
- python - Python 闭包中的共享变量和本地化变量