How to add PDF as attachment in magento 2.2.6 ?
I have created code using an event "checkout_onepage_controller_success_action
" to sends an email after place an order button click.
1) When someone clicks on the place order button then email sent successfully and redirect to the checkout/success page,
2) but when I use AddAttachment() function to send PDF as an attachment with email then after clicking on place order it's not redirecting to checkout/success/page and loader showing ... but I have received email with attachment in my mail
below is my code to add attachment :
$this->inlineTranslation->suspend();
$this->_transportBuilder
->setTemplateIdentifier($templateId)
->setTemplateOptions([
'area' => $area,
'store' => $store->getId()
])
->setTemplateVars($vars)
->setFrom($from)
->addTo($to['email'], $to['name'])
->addAttachment($pdfData, 'Credit Application');
$transport = $this->_transportBuilder->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
//code to create PDF:
public function createPdf( $name,$email)
{
$pdf = new \Zend_Pdf(); //Create new PDF file
$page = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
$top = 810;
$left = 50;
$page->setFont(\Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA), 20); //Set Font
$page->drawText("Heading", $left+200, $top-110,'UTF-8');
$page->setFont(\Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA), 14); //Set Font
$page->drawText("Name : ".$name, $left, $top-140,'UTF-8');
$page->drawText("Email : ".$email, $left, $top-165,'UTF-8');
$pdfData = $pdf->render(); // Get PDF document as a string
header("Content-Disposition: inline; filename=result.pdf");
header("Content-type: application/x-pdf");
return $pdfData;
}
Anyone has an idea then let me know.