【问题标题】:how can i automatically download generated pdf using php [duplicate]我如何使用php自动下载生成的pdf [重复]
【发布时间】: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();
 }
}

?>

【问题讨论】:

标签: php pdf


【解决方案1】:

我刚刚添加了return $pdf-&gt;stream(); 并删除了

    header('Content-Type: application/download');
    header("Content-Disposition: attachment; filename=\"" . $file_name . "\"");
    header("Content-Length: " . filesize($file_name));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 2020-06-10
    • 2017-12-07
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多