【问题标题】:Chrome error when generating PDF with PHP使用 PHP 生成 PDF 时出现 Chrome 错误
【发布时间】:2015-07-29 11:11:56
【问题描述】:

我在 chrome 中使用 PHP 显示 PDF 时遇到问题。

上下文:

我正在使用 TCPDF (http://www.tcpdf.org/) 生成 PDF。 我这样设置标题:

header("Content-type: application/pdf");
header('Content-Disposition: inline; filename="Sujet.pdf"');
header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');

出于测试目的,我将标头放在 index.php 文件的最开头,就在第一个 <?php 之后

其他信息,我正在使用 FatFree 框架,我认为框架不处理标题。

我正在使用 TCPDF 类的扩展来显示 PDF。我做了一个扩展类来实现函数render,它使用以下命令:

$this->Output('Sujet.pdf', 'I');

问题:

  1. 使用 chrome:PDF 不会显示,我有一个 ERR_INVALID_RESPONSE
  2. 使用 firefox:显示 PDF 但有一个 firebug 错误:NetworkError: 500 Internal Server Error。答案是

来自这个 url 的资源不是文本

我认为这意味着 firefox 需要文本并且有其他内容。

此外,PDF 可以正确显示在 TCPDF 网站示例中。在我看来,问题来自错误或放错的标题。

感谢您考虑我的问题。

马蒂亚斯

【问题讨论】:

  • 你能发布你的完整代码吗
  • index.php 还是 tcpdf 类?两个都 ?你认为什么是相关的?
  • 作为响应,它说 firefox 需要文本,但得到了其他内容。

标签: php pdf http-headers tcpdf


【解决方案1】:

使用头功能

public function Header()

【讨论】:

  • 你说的是TCPDF头函数吗?
【解决方案2】:

解决方案

标题由 TCPDF 负责,这里不需要它们。但这不是问题。

它来自毫无意义的一行:

echo \Template::instance()->render($pdf->rendu($f3, $this));

这句话来自我尝试使用 FatFree 模板用 TCPDF 编写 html 的时候。忘记抹掉了。

【讨论】:

    【解决方案3】:

    index.php 文件:

    <?php
    header("Content-type: application/pdf");
    header('Content-Disposition: inline; filename="Sujet.pdf"');
    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');
    
    // defining constants
    include('liste_permissions.php');
    define('PAGE_ERREUR', 'templates/forbidden.htm');
    
    // adding librairies
    $f3 = require('lib/base.php');
    // adding config files
    $f3->config('config/config.ini');
    $f3->config('config/routes.ini');
    $f3->config('config/bdd.ini');
    
    // defining other constants in the Hive of fatfree framework
    $bdd = new Bdd($f3);
    $f3->set('Bdd', $bdd->getDb());
    $f3->set('f3', $f3);
    $f3->set('FILE', __FILE__);
    
    // Global functions used a lot
    include('fct_globales.php');
    
    // Refreshing the session of the connexion
    \Membre\Manager::instance()->refreshConnection();
    
    // running fatfree framework
    $f3->run();
    

    路由服务调用如下函数:

    function renduPdf($f3) {
    
        if(CIA(SEE_SUJETS_ET_COR)) {
            $pdf = new \Pdf;
            $pdf->rendu(Manager::instance()->getFromId($f3->get('PARAMS.id')));
            echo \Template::instance()->render($pdf->rendu($f3, $this));
        }
        else {
            echo ERREUR;
        }
    }
    

    pdf-&gt;rendu() 在这里:(pdf 扩展了 TCPDF 类)

    public function rendu(\Sujet\Data $sujet) {
        //fatfree base
        $f3 = \Base::instance();
        // Setting the class attributes
        $this->sujet = $sujet;
        $questions = $sujet->getQuestions();
        $this->nomAbrege = 'CB' . $sujet->getNumero_cb();
        $this->numMatiere = $sujet->getMatiere();
        $this->nomMatiere = $f3->get('matieres')[$sujet->getMatiere()];
    
        // PDF initialisation : author, imagescale
        $this->initialisation();
    
        // Displaying questions
        $this->premierePage();
        foreach ($questions as $question) {
            $this->question($question);
        }
    
        $this->Output('Sujet.pdf', 'I');
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多