wkhtmltopdf php 包装器的 send() 不工作

wkhtmltopdf php wrapper's send() not working

我正在尝试使用带有此 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 not null

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');
}

});

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

如果我是你,我会将生成的 PDF 文件和 return link 保存到你的前端。

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

之后你只需要 return 将 link 到 pdf 并在你的 Angular 代码中处理它。不是 Angular 专家,但它应该看起来像这样:

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

我在 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-脚本来构建 pdf 文件。