SwiftMailer附件pdf由Dompdf动态生成

首先寻求帮助:)

我在Symfony 4.4 Framework上工作,我让控制器生成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,但如果您有任何建议,它将无法正常工作:)

Thx

lele537 回答:SwiftMailer附件pdf由Dompdf动态生成

您可以使用Symfony Like的FileSystem组件=>

  1. 安装

    composer require symfony/filesystem

  2. 用法

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

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

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

文档:The Filesystem Component

本文链接:https://www.f2er.com/2542558.html

大家都在问