【发布时间】:2016-02-18 11:17:35
【问题描述】:
在我的 symfony 项目中,我使用 Html2pdf 库,并且我的控制器中有这段代码:
$html = $this->render('MyBundle:MyFolder:myPDF.html.twig', array('slug' => $slug));
$html->getContent();
try {
$html2pdf = new \Html2Pdf_Html2Pdf('P', 'A4', 'fr', true, 'UTF-8');
$html2pdf->pdf->SetAuthor('....');
$html2pdf->pdf->SetDisplayMode('real');
$html2pdf->writeHTML($html);
$html2pdf->Output('Bill.pdf');
$response = new Response();
$response->headers->set('Content-type', 'application/pdf');
return $response;
} catch(HTML2PDF_exception $e) {
die($e);
}
这里没有发生错误,但它呈现给我:
因此,控制器没有返回正确的 pdf 文件。
为什么 html PDF 会这样渲染输出?
这是我想以 pdf 格式呈现的视图:
<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="description" content="Bill">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<table>
<thead>
<tr>
<th>test</th>
</tr>
</thead>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</body>
这是我的浏览器中返回的标头响应:
HTTP/1.1 500 Internal Server Error Date: Thu, 18 Feb 2016 13:05:16 GMT Server: Apache/2.4.9 (Win64) PHP/5.5.12 x-powered-by: PHP/5.5.12 Cache-Control: public, must-revalidate, max-age=0, no-cache Pragma: public Expires: Sat, 26 Jul 1997 05:00:00 GMT Last-Modified: Thu, 18 Feb 2016 13:05:17 GMT Content-Length: 2198 Content-Disposition: inline; filename="Bill.pdf"; X-Debug-Token: 5e207b X-Debug-Token-Link: /symfony/web/app_dev.php/_profiler/5e207b Connection: close Content-Type: text/html; charset=UTF-8
如您所见,内容类型也不是 application/pdf。
【问题讨论】:
标签: php html symfony pdf html2pdf