【问题标题】:wkhtmltopdf php wrapper's send() not workingwkhtmltopdf php wrapper 的 send() 不工作
【发布时间】:2018-09-15 00:19:45
【问题描述】:

我正在尝试使用带有 library 的 wkhtmltopdf php 包装器将我的 html 页面转换为 pdf。我尝试使用 saveAs() 函数,效果很好。但我想在浏览器上下载生成的 pdf 文件,所以我使用了 send(filename) 函数,不幸的是这对我不起作用。

这是我的代码:

public static function generatePDF(){
       $this->setJsonResponse();
       $data = $this->request->getPost();
       $srno = $data["number"];
       $type = $data["type"];
       $html = $data["html"];
       $pdf = new Pdf(array(
              'print-media-type',
              'commandOptions' => array(
                  'useExec' => true, 
                  'procEnv' => array(
                      'LANG' => 'en_US.utf-8',
                  )
       )));
       $pdf->binary = $config->application->wkhtmltopdf_binary;
       $pageOptions = array(
              'disable-smart-shrinking',
       );
       $pdf->addPage($html, $pageOptions);
       $filename = $type . "-" . $srno . '-' . date("Ymd") . '' . date("his") . ".pdf";
       if (!$pdf->send($filename)) {
           $error = $pdf->getError();
           return array('error' => $error);
       }
       return true;
  }

我调试了代码,发现 MIME 类型有问题。 send() 需要 MIME 类型“application/pdf”,但在我的回复中是“application/json”。那是因为函数$this->setJsonResponse();的第一行

这是我的 ControllerBase.php 中的这个函数

protected function setJsonResponse() {
    $this->json_response = true;

    $this->view->disable();

    $this->response->setContentType('application/json', 'UTF-8');

    // If request AJAX is json type then parse it and update
    // php's POST member.
    $contentType = $this->request->getHeader('CONTENT_TYPE');
    if (strpos(strtolower($contentType), "application/json") !== FALSE) {
        $jsonRawBody = $this->request->getJsonRawBody(true);
        if ($this->request->getRawBody() && !$jsonRawBody) {
            // throw new Exception("Invalid JSON syntax");
        } else {
            $_POST = $jsonRawBody;
        }
    }
}

如果我从generatePDF 中删除该函数调用,则$data 为空。 我应该怎么做才能使 send() 工作并从 ajax 中获取 $data 不为空?

P.S.:我在客户端使用 AngularJs 并使用 http 请求。

编辑:http 请求(getJson 是我为项目制作的全局函数。即只是一个简单的 http 请求或 ajax)

$rootScope.getJson(controller + '/generatePdf', data, function (r) {
if (r.error) {
    console.log(r.error);
} else if (r.success) {
    var filename = r.success.filename;
    var url = r.success.base + 'temp/' + filename;
    var link = document.createElement('a');
    link.href = url;
    link.setAttribute('target', '_self');
    link.download = filename;
    link.click();
} else {
    console.log('something went wrong');
}

});

【问题讨论】:

    标签: php angularjs mime-types wkhtmltopdf phalcon


    【解决方案1】:

    不确定您是否可以设置标题并从 Ajax 调用中输出 pdf。这与在 Ajax 调用后尝试进行服务器端重定向不一样吗 - 不会工作:)

    如果我是你,我会保存生成的 PDF 文件并返回一个链接到你的前端。

    generatePDF() 而不是send() 中使用saveAs() 方法并收集链接。比如www.website.com/generated-files/pdf-1233.pdf等等……

    之后,您只需返回 pdf 的链接并在您的 Angular 代码中处理它。不是 Angular 专家,但它应该看起来像这样:

    .then(function(response) {
        window.location.href = response.pdfLocation;
        ...
    });
    

    【讨论】:

    • 我试过了,但是下载的pdf文件损坏了。
    • 您可以使用您返回到 Angular 回调的链接访问 PDF 吗?请使用角度 http 请求代码更新您的问题。
    • 在本地主机上测试时工作正常。如果文件来自不同的域,则出于安全原因,Chrome 会重定向而不是下载。也许只是做window.location.href = url;,你应该完成:) 也试试console.log(url),复制链接并把它放在你的浏览器中,100% 确定 pdf 文件在那里!
    【解决方案2】:

    我已经在 ControllerBase 中添加了这个函数,并在 generatePDF() 中调用了这个函数:

    protected function setPdfResponse() {
        $this->json_response = false;
        $this->pdf_response = true;
    
        $this->view->disable();
    
        // Important response-headers copied from wkhtmltopdf
        $this->response->setContentType('application/pdf');
        $this->response->setHeader('Pragma', 'public');
        $this->response->setHeader('Expires', '0');
        $this->response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
        $this->response->setHeader('Content-Transfer-Encoding', 'binary');
    
        // If request AJAX is json type then parse it and update
        // php's POST member.
        $contentType = $this->request->getHeader('CONTENT_TYPE');
        if (strpos(strtolower($contentType), "application/json") !== FALSE) {
            $jsonRawBody = $this->request->getJsonRawBody(true);
            if ($this->request->getRawBody() && !$jsonRawBody) {
                //throw new Exception("Invalid JSON syntax");
            } else {
                $_POST = $jsonRawBody;
            }
        }
    }
    

    这对我有用,这里不需要 java-script 来构建 pdf 文件。

    【讨论】:

      猜你喜欢
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 2013-08-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多