Home » Php » php – How to send pdf generated by TCPDF as Swiftmailer attachment

php – How to send pdf generated by TCPDF as Swiftmailer attachment

Posted by: admin July 12, 2020 Leave a comment

Questions:

I already tried several solutions, the closest (for me) should look like this:

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

$pdf is an instance of TCPDF and $message is an instance of Swift_Message.
Using above the email is being sent ok, file is attached but when I try to open it I get the error message that file is corrupted or badly encoded.

My question is: how to send pdf generated by TCPDF as Swiftmailer attachment without saving the file to server and deleting it after sending the email. Here is the link to the TCPDF output method documentation, maybe somebody can see something I have missed.

How to&Answers:

I am using something like this and it is working. For the PDF content I am using one of the simplest examples on the PDF library.

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

Maybe this answer comes a little late since I am using swiftmailer v4_3_0 and TCPDF v6_0_002. But just in case is worth to someone.

Answer:

I have had no problems attaching TCPDFs on the fly.

I call a function which eventually returns the PDF using the Output type ‘S’:

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

I attach the file using:

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

Where $val['file'] is the returned value from above.

I am using TCPDF Version: 5.9.134 and Swift Mailer Version: 4.1.3

Answer:

You can use outputmode ‘E’ to get base64String.

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

Beware: Maybe you have to cut the first 5-6 lines, because of

Content-Type: application/pdf; name=””
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=””

Base64StringStartsHere….

cut

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

Now you have the Email as base64String.
Before sending you have to decode it.

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

Answer:

Did you try this?

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

I’m doing this with another mail backend in PHP, and this does work. I guess the mail backend takes care of encoding the attachment, so there is no need to encode it as base64 manually.