【发布时间】:2020-02-24 11:43:35
【问题描述】:
我需要让我的网站自动下载我生成的 pdf, 所以我使用这段代码使用 dompdf 生成 pdf :
include('pdf.php');
$file_name = "ORDER-".$name . '.pdf';
$html_code = '<link rel="stylesheet" href="bootstrap.min.css">';
$html_code .= fetch_customer_data();
$pdf = new Pdf();
$pdf->load_html($html_code);
$pdf->render();
$file = $pdf->output();
file_put_contents($file_name, $file);
当我在 localhost 中运行它时,pdf 下载到源代码文件所在的同一文件中,一切都很好,但是当我尝试在真实服务器上运行它时,我找不到 pdf,所以我使用了这段代码它会自动下载到下载文件中:
header('Content-Type: application/download');
header("Content-Disposition: attachment; filename=\"" . $file_name . "\"");
header("Content-Length: " . filesize($file_name));
这样就完成了工作,但是当我打开文件时,我发现它是空的,或者是代码源页面的结果 我不知道这里出了什么问题,谁能告诉我
pdf.php 文件:
<?php
//pdf.php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct(){
parent::__construct();
}
}
?>
【问题讨论】:
-
好吧,如果我想下载多个 pdf,这似乎对我不起作用