dougu3988 2014-05-21 19:54 采纳率: 100%
浏览 58
已采纳

使用FPDF和SwiftMailer发送的空白pdf附件

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("admin@mysite.com" => 'Company Admin'))
    ->setTo('my@email.com')
    ->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

  • 写回答

1条回答 默认 最新

  • dr6673999 2014-05-22 09:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?