【问题标题】:Emailing pdf generated by dompdf with codeigniter使用 codeigniter 发送由 dompdf 生成的 pdf
【发布时间】:2016-10-17 15:31:43
【问题描述】:

我有一个从 Codeigniter 中的 html 视图生成的 PDF,现在我想将它发送到我的电子邮件,但我遇到的问题是它显示 $pdf 中有一个字符串,但它是空的在我的电子邮件中发送时。这是整个 Codeigniter 函数。

public function generatePDF()
{
    require_once(APPPATH.'third_party/dompdf/dompdf_config.inc.php');

    $dompdf = new Dompdf();
    $msg = $this->load->view('credit_agreement_view', '', true);
    $html = mb_convert_encoding($msg, 'HTML-ENTITIES', 'UTF-8');
    $dompdf->load_html($html);
    $paper_orientation = 'Potrait';
    $dompdf->set_paper($paper_orientation);

    // Render the HTML as PDF
    $dompdf->render();
    $pdf = $dompdf->output();

    // sending the pdf to email
    $this->load->library('email');
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com';
    $config['smtp_port']    = '465';
    $config['smtp_timeout'] = '7';
    $config['smtp_user']    = 'support@aurorax.co';
    $config['smtp_pass']    = '#######';
    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
    $config['mailtype'] = 'text'; // or html
    $config['validation'] = TRUE; // bool whether to validate email or not    
    $this->email->initialize($config);

    $this->email->from('support@aurorax.co', 'aurora exchange');
    $this->email->to('masnad@aurorax.co');
    $this->email->subject('pdf');
    $this->email->attach($pdf);
    $this->email->message('Hello!');  

    $this->email->send();
}

【问题讨论】:

  • 先保存再附加绝对路径
  • @ManinderpreetSingh 这基本上是保存到我不想要的计算机上,我想直接将其发送到我的电子邮件或稍后将其保存到 mysql 数据库
  • 改变你的这一行 $this->email->attach($pdf);到 $this->email->string_attach($pdf, 'myfile.pdf', 'application/pdf');
  • @ManinderpreetSingh 我确实做到了,但出现错误致命错误:调用 C:\Apache24\htdocs\中未定义的方法 CI_Email::string_attach()
  • @ManinderpreetSingh 你看过我之前的评论了吗?

标签: php codeigniter pdf dompdf


【解决方案1】:

您的代码帮助我实现了发送 pdf 附件而无需将它们保存在服务器上。我没有调用 render 方法。

    //load dompdf library
    $this->load->library('PHPPdf');
    $objPHPPdf = new PHPPdf();
    $objPHPPdf->loadHtml($attachment, 'UTF-8');
    // (Optional) Setup the paper size and orientation
    $objPHPPdf->setPaper('A4', 'portrait');
    // Render the HTML as PDF
    $objPHPPdf->render();
    $pdf = $objPHPPdf->output();
    $this->load->library('email');
    $this->email->initialize($this->config->item("email_config"));
    $from = $this->config->item("email_from");
    $this->email->from($from["from"], $from["from_name"]);
    $this->email->to($to);
    $this->email->subject('Sample');
    $this->email->message('Sample message');
    $this->email->attach($pdf, 'application/pdf', "Pdf File " . date("m-d H-i-s") . ".pdf", false);

    $r = $this->email->send();
    if (!$r) {
        // "Failed to send email:" . $this->email->print_debugger(array("header")));
    } else {
        // "Email sent"
    }

【讨论】:

  • 这很不错!
【解决方案2】:

要实际发送附件,您必须生成 pdf 文件,而不仅仅是在浏览器中将其呈现为 pdf。在$dompdf->render(); 行后添加 $dompdf->stream("attachment.pdf"); 并在附加时使用此附件文件的绝对路径。发送电子邮件后,您可以随时取消链接(删除)附件。

【讨论】:

  • 是的,但我不希望它将电子邮件存储在某个路径中,要么将其存储在数据库中,要么将其作为电子邮件发送,因此我必须不要将其存储在驱动器中
  • @MasnadNihit 你用的是哪个版本的codeigniter??
  • 2.1.4是版本
  • 我认为不存在任何接受原始数据的方法...您必须扩展电子邮件类并覆盖现有方法。希望此链接对您有所帮助.. stackoverflow.com/questions/2146591/…
  • 但是有没有办法把它保存到数据库然后导出呢?
猜你喜欢
  • 1970-01-01
  • 2017-08-14
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 2017-07-14
  • 2020-07-12
  • 2022-01-03
相关资源
最近更新 更多