Search code examples
phppdfphpmailertcpdf

Send TCPDF generated pdf with phpmailer


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?


Solution

  • 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