Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send pdf generated by TCPDF as Swiftmailer attachment

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.

like image 883
matino Avatar asked Nov 09 '11 11:11

matino


1 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.

like image 81
Angelica Rodriguez Avatar answered Sep 30 '22 18:09

Angelica Rodriguez