【问题标题】:How to generate more than one PDF and send through email attachment using PHP?如何使用 PHP 生成多个 PDF 并通过电子邮件附件发送?
【发布时间】:2019-08-27 13:46:05
【问题描述】:

如何生成多个pdf并通过电子邮件附件发送?

我正在使用 fpdf php 库生成 pdf 并希望通过电子邮件发送多个 pdf 附件。所以我试过了,

<?php
require('fpdf.php');

// Generate first One.
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output('.assets/temp/download1.pdf','f');
$file1='.assets/temp/download1.pdf';

 // Generate Second PDF.
 $pdf = new FPDF();
 $pdf->AddPage();
 $pdf->SetFont('Arial','B',16);
 $pdf->Cell(40,10,'Hello World!');
 $pdf->Output('.assets/temp/download2.pdf','f');
 $file2='.assets/temp/download2.pdf';

 // use phpmailer to send email.
 // load attachments and messages.
 // send
 // unset files.
?>

这将产生错误消息:FPDF 错误:文档已关闭。因为我不能在没有关闭文档的情况下加载两次。所以请询问我通过附件发送多个 pdf 的解决方案。

【问题讨论】:

    标签: php fpdf


    【解决方案1】:

    尝试使用“S”参数而不是“F”

    $pdf1 = new FPDF();
    $pdf1->AddPage();
    $pdf1->SetFont('Arial', 'B', 16);
    $pdf1->Cell(40, 10, 'Hello World1!');
    $file1 = ".assets/temp/download1.pdf";
    $pdf1content = $pdf1->Output('S');
    file_put_contents($file1, $pdf1content);
    
    $pdf2 = new FPDF();
    $pdf2->AddPage();
    $pdf2->SetFont('Arial', 'B', 16);
    $pdf2->Cell(40, 10, 'Hello World2!');
    /**/
    $file2 = ".assets/temp/download2.pdf";
    $pdf2content = $pdf2->Output('S');
    file_put_contents($file2, $pdf2content);
    
    // use phpmailer to send email.
    // load attachments and messages.
    // send unset files.
    

    http://www.fpdf.org/en/doc/output.htm

    【讨论】:

    • 非常感谢@Andrea Manzi
    • 不客气@vijay!您使用哪个版本的 FPDF?
    • 从 FPDF 1.81 开始,$dest 参数是Output() 的第一个参数。所以第一个参数中不需要空字符串。但我想这就是你要求版本号的原因?
    • 完全是@JanSlabon,无论如何function Output( $dest = '', $name = '', $isUTF8 = false ) { of 1.81 检查参数是否向后兼容if( strlen( $name ) == 1 &amp;&amp; strlen( $dest ) != 1 ) { // Fix parameter order $tmp = $dest; $dest = $name; $name = $tmp; }
    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    相关资源
    最近更新 更多