0

In Magento, while submit form from frontend in controller using post method i got every data. I want create one pdf file and keep this data in it after that attachment of this file and send with mail.

Thanks in advance.

2 Answers 2

0

Use TCPDF file for generate PDF.

How to use TCPDF in magento Go Here

Create one custom phtml file in your theme direactory and generate output to print it in tcpdf.

call it in you controller file like this

$htmlContent = $this->getLayout()->createBlock('core/template')->setPostData($data)->setTemplate('<your template file>');

and in your template file you can get your post data like

$this->getPostData()

attached generated pdf with mail.

Edit

generate pdf in the media directory with post data and send it using below code.

change code according to your variable and need.

        $mailer = Mage::getModel('core/email_template_mailer');

    $emailInfo = Mage::getModel('core/email_info');
    $emailInfo->addTo($to, $customerName);
    $mailer->addEmailInfo($emailInfo);

    $mailer->setSender('[email protected]');
    $mailer->setStoreId(1);
    $mailer->setTemplateId(2);

    $filePath = $mediaDir.DS.$uploadedfilePath;
    $_pdfSavePath = Mage::getBaseDir('media').DS.'pdf_order_template';
    if(file_exists($filePath)){
         $pdfContent  = file_get_contents($filePath);
        $mailer->addPDFAttachment($pdfContent,  'Order_PDF-' . $order->getIncrementId() .  '.pdf');
     }

       $mailer->send();
3
  • Have you any example of that ? i want to add post data into pdf file and send it with mail using controller. Commented Oct 15, 2018 at 10:03
  • code added for mail with attachement
    – Zahirabbas
    Commented Oct 15, 2018 at 10:17
  • Please check new answer. Commented Oct 15, 2018 at 11:57
0

Please check this code:

public function sendemailAction()
{    
      $request     = $this->getRequest()->getPost();
      $mail   = Mage::getModel('core/email');           
            $content = '<h2>Personal Information</h2><br>';
            $content .= '<label>Name:</label>'.$request['name'].'<br>';
            $content .= '<label>Email:</label>'.$request['email'].'<br>';  
    require_once(Mage::getBaseDir('lib') . DS . 'TCPDF-master/examples/tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
            // set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('Asmani');
            $pdf->SetTitle('TCPDF Example GJ01');
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

            $pdf->writeHTMLCell(0, 0, '', '', $content, 0, 1, 0, true, '', true);
            $pdf->Output('example_001.pdf', 'I');
$to = '[email protected]';

            $mail->setBody($body);
            $mail->setFromEmail($fromemail);
            $mail->setFromName($request['name']);
            $mail->setToEmail($to);
            $mail->setSubject('Review Form');
            $mail->setType('html');
            try {
                $mail->send();               

            }
            catch (Exception $e) {
                Mage::getSingleton('core/session')->addError('Unable to send.');
            }

but still not working for me. Got 500 error.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.