Send TCPDF generated pdf with phpmailer

心不动则不痛 提交于 2020-06-23 08:28:26

问题


I am generating a pdf file on the fly without saving it to the disk with:

$attachment = $this->pdf->Output('e-tickets.pdf', 'S');

According to TCPDF this should return a string containing the pdf file.

But sending it with PHPMailer results in a corrupt file:

$mail->AddStringAttachment($attachment, 'e-tickets.pdf', 'base64', 'application/pdf');

I tried the following alternatives (and all possible combinations):

$attachment = $this->pdf->Output('e-tickets.pdf', 'E');

$mail->AddStringAttachment($attachment, 'e-tickets.pdf');

Nothing resulted in a working pdf file. The file is not empty (it has a filesize) and when I use the D option in TCPDF the file is downloaded fine.

All other topics on Stackoverflow did not help me. they are all quite old and I am guessing using an older version.

Any suggestions?


回答1:


I solved it myself. It was a stupid mistake really

I use $this->pdf->Output('e-tickets.pdf', 'E'); in a class.

I changed it to return $this->pdf->Output('e-tickets.pdf', 'E'); and it solved the issue.

Fresh day, fresh look at things can help.

Thank you for your help




回答2:


Have you tried:

$attachment = $this->pdf->Output('e-tickets.pdf', 'E');
$attachment = chunk_split($attachment);
$mail->AddStringAttachment($attachment, 'e-tickets.pdf');


来源:https://stackoverflow.com/questions/46267433/send-tcpdf-generated-pdf-with-phpmailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!