首页 > 解决方案 > 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 但如果您有任何建议,它不起作用:)

谢谢

标签: symfonydompdfmailer

解决方案


你可以像使用 Symfony 的 FileSystem 组件一样 =>

  1. 安装

    composer require symfony/filesystem

  2. 用法

    use Symfony\Component\Filesystem\Filesystem;
    
    $filesystem = new Filesystem();
    
  3. 删除文件

    $filesystem->remove(['/path/to/file']);
    

在您的情况下,您可以保存文件 pdf 然后发送,如果发送了邮件,则删除文件

文档:文件系统组件


推荐阅读