【问题标题】:How to send pdf generated by TCPDF as Swiftmailer attachment如何将 TCPDF 生成的 pdf 作为 Swiftmailer 附件发送
【发布时间】:2011-11-09 11:32:33
【问题描述】:

我已经尝试了几种解决方案,最接近的(对我来说)应该是这样的:

$file = $pdf->Output('', 'E');
$message->attach(Swift_Attachment::newInstance($file, 'name.pdf', 'application/pdf'));

$pdfTCPDF 的一个实例,$messageSwift_Message 的一个实例。 使用上面的电子邮件发送正常,文件已附加,但是当我尝试打开它时,我收到文件已损坏或编码错误的错误消息。

我的问题是:如何将 TCPDF 生成的 pdf 作为 Swiftmailer 附件发送 而不将文件保存到服务器并在发送电子邮件后将其删除Here 是 TCPDF 输出方法文档的链接,也许有人能看到我遗漏的东西。

【问题讨论】:

    标签: php tcpdf swiftmailer


    【解决方案1】:

    您可以使用 outputmode 'E' 来获取 base64String。

    $base64PdfString = $pdf->Output('', 'E');
    

    小心:也许你必须剪掉前 5-6 行,因为

    内容类型:应用程序/pdf;名称=""
    内容传输编码:base64
    内容处置:附件;
    文件名=""

    Base64StringStartsHere....

    剪切

    $base64PdfArray = explode("\r\n", $base64PdfString);
    $base64 = '';
    for($i = 5; $i < count($base64PdfArray); $i++) {
        $base64 .= $base64PdfArray[$i];
    }
    

    现在您将电子邮件作为 base64String。
    在发送之前,您必须对其进行解码。

    $mail->attach(new \Swift_Attachment(base64_decode($base64), 'Pdf.pdf', 'application/pdf'));
    

    【讨论】:

      【解决方案2】:

      我正在使用类似的东西,它正在工作。对于 PDF 内容,我使用的是 PDF 库中最简单的示例之一。

      [...]
      $pdf_as_string = $pdf->Output('', 'S'); // $pdf is a TCPDF instance
      [...]
      $transport = Swift_MailTransport::newInstance(); // using php mail function
      $message->setTo(array(
        "client@customdomain.com" => "Main Email",
        "client@publicdomain.com" => "Secondary Email"
      ));
      $message->setSubject("This email is sent using Swift Mailer");
      $message->setBody("You're our best client ever.");
      $message->setFrom("developers@mydomain.com", "Developers United");
      $attachment = Swift_Attachment::newInstance($pdf_as_string, 'my-file.pdf', 'application/pdf');
      $message->attach($attachment);
      [...]
      

      也许这个答案来得有点晚,因为我使用的是 swiftmailer v4_3_0 和 TCPDF v6_0_002。但以防万一对某人来说是值得的。

      【讨论】:

      【解决方案3】:

      即时附加 TCPDF 没有任何问题。

      我调用了一个函数,该函数最终使用输出类型“S”返回 PDF:

      return $pdf-&gt;Output('TE_Invoice.pdf', 'S');

      我使用以下方法附加文件:

      $message->attach(Swift_Attachment::newInstance()
        ->setFilename('TE_Invoice.pdf')
        ->setContentType('application/pdf')
        ->setBody($val['file']));
      

      $val['file'] 是上面的返回值。

      我使用的是 TCPDF 版本:5.9.134 和 Swift Mailer 版本:4.1.3

      【讨论】:

      • 这就是我尝试这样做的方式,但是当我在电子邮件中打开 pdf 后,我收到错误消息,指出文件已损坏或编码错误......
      【解决方案4】:

      你试过了吗?

      $file = $pdf->Output('', 'S');
      

      我正在使用PHP 中的另一个邮件后端执行此操作,这确实有效。我猜邮件后端负责对附件进行编码,因此无需手动将其编码为 base64。

      【讨论】:

      • 已经试过了,再试一次还是不行(同样的错误信息)
      • 如果将文件保存到文件系统,可以打开吗?如果是,那么这是一个 swiftmailer 错误。那么你至少知道在哪里可以看得更远。
      • 如果我保存文件并使用 $message->attach(Swift_Attachment::fromPath($path));然后一切正常
      • 对不起,那我帮不了你。我对 Swiftmailer 一无所知,我使用的是pear.php.net/Mail_Mime,它可以成功添加一个由$file = $pdf-&gt;Output('', 'S') 生成的带有$mime-&gt;addAttachment($file, 'application/pdf', 'name.pdf') 的附件……不过,这对你没有太大帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      相关资源
      最近更新 更多