Geoff
Geoff

Reputation: 197

blank pdf attachment sent using FPDF and 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 blank. It has a size of 1Kb and can be opened as a pdf.

My code is :

<?php

include('./fpdf/fpdf.php');
require_once './lib/swift_required.php';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Text(40, 10, "Dummy text");
$data=$pdf->Output('./emails/order.pdf', 'F');
$message = Swift_Message::newInstance('Subject')
    ->setFrom(array("[email protected]" => 'Company Admin'))
    ->setTo('[email protected]')
    ->setBody('This is body text', 'text/html');    
$attachment = Swift_Attachment::fromPath('./emails/order.pdf');
//$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);

I referred to this question previous question

Upvotes: 0

Views: 494

Answers (1)

Geoff
Geoff

Reputation: 197

I finally got it to work. I changed the code as follows:

$pdf->AddPage(); $pdf->SetFont('Arial','B',16);

as edited above and it worked. I think the problem was in in the AddPage.

Upvotes: 2

Related Questions