Reputation: 13
I have an function downloadPdf($id, \Knp\Snappy\Pdf $snappy, Request $request)
.
This function downloads a pdf with information from objects everything works fine.
This is the function:
public function downloadPdf($id, \Knp\Snappy\Pdf $snappy, Request $request): Response
{
//search id
$workOrder = $this->getDoctrine()->getRepository(WorkOrders::class)->find($id);
//data to pdf template
$html = $this->renderView('pdf/pdf.html.twig', array(
'workOrder' => $workOrder,
));
//name file
$filename = $workOrder->getId();
//download pdf
return new PdfResponse(
$snappy->getOutputFromHtml($html),
$filename.'.pdf'
);
}
And then I have an swift mailer function:
//check if signed and if check is true
if ($data_uri) {
//send workOrder to company
if ($check == true) {
$transport = (new \Swift_SmtpTransport('smtp.sendgrid.net', 587))
->setUsername('sendgridUSERNAME')
->setPassword('sendgridPASSWORD')
;
$mailer = new \Swift_Mailer($transport);
$message = (new \Swift_Message('Werkbon '.$workOrder->getTitel()))
->setFrom(['xxx' => 'xxx'])
->setTo('xxx')
->setBody('xxx')
->attach(\Swift_Attachment::fromPath($this->downloadPdf()))
;
$result = $mailer->send($message);
}
The email works fine but I want to attach the pdf from the other function to this email in the code above you can see what I tried but I think I got it totally wrong. I have no idea where to start.
Can someone give me a little push in the right direction? Thanks!
Upvotes: 0
Views: 659
Reputation: 17916
here is how it works for me,
$invoicepdf = $this->get('knp_snappy.pdf')->getOutputFromHtml($html);
/* And instead of returning pdf, send it with mailer: */
$message = \Swift_Message::newInstance()
->setSubject('YOUR_TITLE')
->setFrom('[email protected]')
->setTo('[email protected]')
->attach(\Swift_Attachment::newInstance($invoicepdf, 'your_file_name','application/pdf'))
->setBody("yyy");
$mailer->send($message);
Upvotes: 1