使用FPDF和SwiftMailer发送的空白pdf附件 [英] blank pdf attachment sent using FPDF and SwiftMailer

查看:83
本文介绍了使用FPDF和SwiftMailer发送的空白pdf附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FPDF库创建PDF流,并使用Swift Mailer通过电子邮件发送pdf.下面是我的代码.邮件已成功发送,甚至还附带了pdf,但pdf为空白.它的大小为1Kb,可以打开为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.

我的代码是:

<?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 finally got it to work. I changed the code as follows:

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

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

如上所编辑,它可以正常工作.我认为问题出在AddPage中.

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

这篇关于使用FPDF和SwiftMailer发送的空白pdf附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆