使用FPDF和SwiftMailer发送时空白的pdf附件 [英] Blank pdf attachment while sent using FPDF and SwiftMailer

查看:75
本文介绍了使用FPDF和SwiftMailer发送时空白的pdf附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FPDF库创建PDF流,并使用Swift Mailer通过电子邮件发送pdf.下面是我的代码.邮件已成功发送,甚至还附带了pdf,但是pdf的大小为零字节,因此无法打开.当我使用$ pdf-> Output('receipt.pdf','D')下载pdf时,它是成功的,并且PDF的内容也存在.有人可以帮助我确定我做错了什么吗?还是我需要在消息中设置一些其他字段,以便它可以正确发送PDF.我搜索了所有与FPDF和swiftmailer相关的帖子,但无法确定代码中的问题.

I am trying to create a PDF stream using FPDF library and to send the pdf over e-mail using Swift Mailer. Below is my code. The mail is sent successfully and even pdf is also attached but the pdf is of zero bytes size and so could not be opened. When I download the pdf using $pdf->Output('receipt.pdf', 'D') it is successful and the content of PDF is also present. Can somebody help me to identify whether I am doing something wrong? Or do I need to set some additional fields in the message so that it will send the PDF correctly. I searched all the posts related to FPDF and swiftmailer but could not identify issue in my code.

$pdf = new FPDF();
$pdf->AddPage();
$pdf->Text(80, 30, "Dummy text");
$pdfstream = $pdf->Output('receipt.pdf', 'S');
$attachment = Swift_Attachment::newInstance($pdfstream, 'name.pdf', 'application/pdf');

$message = Swift_Message::newInstance('Dummy subject')
    ->setFrom(array("abc@xyz.com" => 'XYZ'))
    ->setTo('pqr@abc.com')
    ->setBody('Dummy body text');

$message->attach($attachment);

$transport = Swift_MailTransport::newInstance();

$mailer = Swift_Mailer::newInstance($transport);

$result = $mailer->send($message);

以下对我来说工作正常.请注意,我已经使用了两个外部库SwiftMailer和FPDF,所以您需要做一些必要的事情,例如导入等,才能使它们起作用.

EDIT 1: Below is working perfectly fine for me. Please note that I have used two external libraries SwiftMailer and FPDF so you need to do required things like imports etc. to make them work.

$pdf = new FPDF();
$pdf->AddPage();
$pdf->Text(80, 30, "Dummy text");
$data=$pdf->Output('receipt.pdf', 'S');

$message = Swift_Message::newInstance('Subject')
    ->setFrom(array("Geoff" => 'abc@xyz.com'))
    ->setTo('xyz@abc.com')
    ->setBody('This is body text', 'text/html');    
if(!empty($data))
{
  $attachment = Swift_Attachment::newInstance($data, 'pdf_name.pdf', 'application/pdf');
  $message->attach($attachment);
}
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);

推荐答案

尝试一下:

$pdfstream = $pdf->Output('receipt.pdf', 'S');

$emailStream = 'Content-Type: application/pdf;'."\r\n";
$emailStream.= ' name="name.pdf"'."\r\n";
$emailStream.= 'Content-Transfer-Encoding: base64'."\r\n";
$emailStream.= 'Content-Disposition: attachment;'."\r\n";
$emailStream.= ' filename="name.pdf"'."\r\n\r\n";
$emailStream.= chunk_split(base64_encode($pdfstream), 76, "\r\n");

$attachment = Swift_Attachment::newInstance($emailStream, 'name.pdf', 'application/pdf');

这篇关于使用FPDF和SwiftMailer发送时空白的pdf附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆