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 :

                'area' => $area,
                'store' => $store->getId()
            ->addTo($to['email'], $to['name'])
            ->addAttachment($pdfData, 'Credit Application');

    $transport = $this->_transportBuilder->getTransport();


//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.

  • attach full code of observer.. Commented Jan 2, 2020 at 11:09
  • how can i use invoice pdf as attachment in my custom module ??@RkRathod Commented Jan 3, 2020 at 5:53


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.