2013-08-20 78 views
1

ZF2中,我試圖使用DOMPDFModule生成PDF並使用EmailZF2通過電子郵件發送。渲染PDF並附加到電子郵件(使用DOMPDFModule和EmailZF2)

這是我在我的控制器正在做:

// fetch data 
$user = $this->getEntityManager()->getRepository('Application\Entity\Users')->find(1); 
$address = $this->getEntityManager()->getRepository('Application\Entity\Addresses')->find(1); 

// generate PDF 
$pdf = new PdfModel(); 
$pdf->setOption('filename', 'Renter_application-report-' . date("Y_m_d")); 
$pdf->setOption('paperSize', 'a4'); 
$pdf->setVariables(array(
    'User' => $user, 
    'Address' => $address, 
)); 

到目前爲止一切都很好,但是DOMPDFModule需要我return $pdf提示生成的PDF,並沒有DOMPDF的似乎工作(例如$pdf->render()$pdf->output())。

我想也呈現自己失敗如下圖(也許有些問題與頭代?)

// Render PDF 
$pdfView = new ViewModel($pdf); 
$pdfView->setTerminal(true) 
    ->setTemplate('Application/index/pdf') 
    ->setVariables(array(
     'User' => $user, 
     'Address' => $address, 
    )); 
$pdfOutput = $this->getServiceLocator() 
    ->get('viewrenderer') 
    ->render($pdfView); 

最後,我們的目標是讓這個渲染PDF和枯萎保存到某個地方,能夠附加或將其附加直線距離 - 甚至簡單

// Save PDF to disk 
$file_to_save = '/path/to/pdf/file.pdf'; 
file_put_contents($file_to_save, $pdfOutput); 

// Send Email 
$view = new ViewModel(array(
    'name' => $User->getName(), 
)); 
$view->setTerminal(true); 
$view->setTemplate('Application/view/emails/user'); 
$this->mailerZF2()->send(array(
    'to' => $User->getEmail(), 
    'subject' => 'Test email' 
), $view, $file_to_save); 

對此我設法通過編輯文件\src\EmailZF2\Controller\Plugin\Mailer.php與這些線工作的PDF附件:

... 
public function send($data = array(), $viewModel, $pdf) 
... 
if($pdf && file_exists($pdf)) { 
    $pdf = fopen($pdf, 'r'); 
    $MessageAttachment = new MimePart($pdf); 
    $MessageAttachment->type = 'application/pdf'; 
    $MessageAttachment->filename = 'output.pdf'; 
    $MessageAttachment->encoding = \Zend\Mime\Mime::ENCODING_BASE64; 
    $MessageAttachment->disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT; 
} 
... 

$body_html = new MimeMessage(); 
$body_html->setParts(array($text, $html, $MessageAttachment)); 

任何幫助表示讚賞,謝謝! :)

回答

1

我不知道這個ID是否是正確的方式,但我設法使它工作,所以我會發布我們如何做,以防其他人會遇到同樣的問題。

我使用DOMPDFModule作爲鹼引擎,然後在控制器,在生成PDF的動作,我提供的PDF通過Viewmodel要使用的視圖源程序作爲模板

use Zend\View\Model\ViewModel, 
    DOMPDFModule\View\Model\PdfModel; 

... 

public function indexAction() 
{ 

    $User = $this->getEntityManager()->getRepository('Application\Entity\Users')->find(1); 

    // generate PDF 
    $pdf = new PdfModel(); 
    $pdf->setOption('filename', 'user_details-' . date("Y_m_d")); 
    $pdf->setOption('paperSize', 'a4'); 
    $pdf->setVariables(array(
     'User' => $User, 
    )); 

    // Render PDF 
    $pdfView = new ViewModel($pdf); 
    $pdfView->setTerminal(true) 
     ->setTemplate('Application/index/pdf.phtml') 
     ->setVariables(array(
      'User' => $User, 
     )); 
    $html = $this->getServiceLocator()->get('viewpdfrenderer')->getHtmlRenderer()->render($pdfView); 
    $eng = $this->getServiceLocator()->get('viewpdfrenderer')->getEngine(); 

    $eng->load_html($html); 
    $eng->render(); 
    $pdfCode = $eng->output(); 

    // Send the email 
    $mailer->sendEmail($User->getId(), $pdfCode); 

} 

emailzf2模塊也被棄用,並且定製郵件程序模塊現在管理電子郵件的附件和發送。要做到這一點,在Mailer/config/module.config.php新的服務已經註冊:

'view_manager' => array(
    'template_path_stack' => array(
     __DIR__ . '/../view', 
    ), 
), 
'service_manager' => array(  
    'factories' => array(
     __NAMESPACE__ . '\Service\MailerService' => __NAMESPACE__ . '\Service\MailerServiceFactory', 
    ), 
), 

到文件Mailer/src/Mailer/Service/MailerServiceFactory.php它引用:

<?php 
namespace Mailer\Service; 

use Mailer\Service\MailerService; 
use Zend\ServiceManager\FactoryInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 

class MailerServiceFactory implements FactoryInterface 
{ 
    public function createService(ServiceLocatorInterface $serviceLocator) 
    { 
     $entityManager = $serviceLocator->get('Doctrine\ORM\EntityManager'); 
     $viewRenderer = $serviceLocator->get('ViewRenderer'); 
     $config = $serviceLocator->get('config'); 
     return new MailerService($entityManager, $viewRenderer, $config); 
    } 
} 

而且Mailer/src/Mailer/Service/MailerService.php

use Zend\Mime\Message as MimeMessage; 
use Zend\View\Model\ViewModel; 

class MailerService 
{ 
    protected $em; 
    protected $view; 
    protected $config; 
    protected $options; 
    protected $senderName; 
    protected $senderEmail; 

    public function __construct(\Doctrine\ORM\EntityManager $entityManager, $viewRenderer, $config) 
    { 
     $this->em = $entityManager; 
     $this->view = $viewRenderer; 
     $this->config = $config; 

     $this->options = array(
            'name' => $config['mailer']['smtp_host'], 
          ); 
     $this->senderName = $config['mailer']['sender']['from_name']; 
     $this->senderEmail = $config['mailer']['sender']['from_address']; 
    } 


    protected function send($fromAddress, $fromName, $toAddress, $toName, $subject, $bodyParts) 
    { 
     // setup SMTP options 
     $options = new Mail\Transport\SmtpOptions($this->options); 

     $mail = new Mail\Message(); 
     $mail->setBody($bodyParts); 
     $mail->setFrom($fromAddress, $fromName); 
     $mail->setTo($toAddress, $toName); 
     $mail->setSubject($subject); 

     $transport = new Mail\Transport\Smtp($options); 
     $transport->send($mail); 
    } 

    protected function setBodyHtml($content, $pdf = null, $pdfFilename = null) { 

     $html = new MimePart($content); 
     $html->type = "text/html"; 

     $body = new MimeMessage(); 

     if ($pdf != '') { 
      $pdfAttach = new MimePart($pdf); 
      $pdfAttach->type = 'application/pdf'; 
      $pdfAttach->filename = $pdfFilename; 
      $pdfAttach->encoding = \Zend\Mime\Mime::ENCODING_BASE64; 
      $pdfAttach->disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT; 

      $body->setParts(array($html, $pdfAttach)); 

     } else { 

      $body->setParts(array($html)); 
     } 

     return $body; 
    } 

    public function sendEmail($UserId) 
    { 
     $User = $this->em->getRepository('Application\Entity\Users')->find($UserId); 
     $vars = array( 'firstname'  => $User->getFirstname(), 
         'lastname'   => $User->getLastname()); 
     $content = $this->view->render('emails/user-profile', $vars); 

     $body = $this->setBodyHtml($content); 

     $sendToName = $User->getOaFirstname() .' '. $User->getLastname(); 

     $this->send($this->senderEmail, $this->senderName, $User->getEmailAddress(), $sendToName, 'User profile', $body); 
    } 
} 
相關問題