繁体   English   中英

在Symfony中同时渲染模板并返回响应

[英]Render template and return response at the same time in Symfony

我想生成一个PDF文件,并使用Swift Mailer和knp-snappy-bundle将其作为电子邮件附件发送。 问题在于它仅生成和下载PDF文件而不渲染模板。 我希望它生成PDF并同时渲染模板。

码:

public function viewPostAction() {
  $html = "Just a sample text to produce the PDF";

  return new Response(
    $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
    200,
    array(
      'Content-Type'          => 'application/pdf',
      'Content-Disposition'   => 'attachment; filename="classement.pdf"'
    )
  );
  $message = \Swift_Message::newInstance()
    ->setSubject('Some Subject')
    ->setFrom('test@gmail.com')
    ->setTo('test@gmail.com')
    ->setBody("test email")
    ->attach(Swift_Attachment::fromPath('C:\Users\acer\Downloads\classement.pdf'));

  # Send the message
  $this->get('mailer')
    ->send($message);
  $post = $this->getDoctrine()->getRepository('AppBundle:Post')->findAll();
  return $this->render("pages/index.html.twig", ['post' => $post]);
}

您的代码返回了它创建的PDF,这就是为什么其余代码没有执行的原因。 你应该改变

return new Response(
    $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
    200,
    array(
        'Content-Type'          => 'application/pdf',
        'Content-Disposition'   => 'attachment; filename="classement.pdf"'
    )
);

生成本地PDF文件的代码中:

$this->get('knp_snappy.pdf')->generateFromHtml($html, 'C:\Users\acer\Downloads\classement.pdf');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM