我正在尝试强制下载pdf文件。我已经在堆栈上看到了几个有关此问题的问题。但是当应用时,解决方案对我来说似乎不起作用。
输出后的PDF文件(使用FPDF)可以保存并直接在服务器上查看,但是当我下载或通过电子邮件发送文件时,它显示为已损坏。
有人可以提供建议吗?
下载或发送电子邮件的代码(取决于所需的操作)
$pdf->output(ROOTDIR.'/modules/addons/statement_generator/reports/statement.pdf');
if($action=='print'){
$file_name = 'statement.pdf';
$file_url = "http://".$_SERVER['SERVER_NAME']."/modules/addons/statement_generator/reports/" . $file_name;
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
return('Statement Generated. Starting download.');
exit;
}elseif($action=='mail'){
require_once(ROOTDIR."/modules/addons/statement_generator/PHPMailer_5.2.0/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = false; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = $from_mail;
$mail->FromName = $from_name;
$mail->AddAddress($client_email, $name); // name is optional
$mail->AddReplyTo($from_mail, $from_name);
$mail->WordWrap = 50; // set word wrap to 50 characters
//$mail->AddAttachment(ROOTDIR.'/modules/addons/statement_generator/reports/statement.pdf'); // add attachments
$mail->AddAttachment(ROOTDIR.'/modules/addons/statement_generator/reports/statement.pdf', "Statement.pdf"); // optional name
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = $non_html_body;
if(!$mail->Send())
{
return( "Message could not be sent. <p>"."Mailer Error: " . $mail->ErrorInfo);
exit;
}
return( "Message has been sent");
}