使用html2pdf_v4.03_php5在Symfony2.3上的应用程序
控制器:public function printAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('MyBundle:Activite')->findAll();
$html=$this->get('templating')->render('MyBundle:Activite:print.html.twig',array('entities' => $entities,));
//var_dump($html);
$html2pdf = new \Html2Pdf_Html2Pdf('P','A4','fr');
$html2pdf->pdf->SetDisplayMode('real');
$html2pdf->pdf->SetTitle('un pdf test');
$html2pdf->writeHTML($html);
$html2pdf->Output('my-document-name.pdf');
}
树枝:
{% extends "::html2pdf.html.twig" %}
{% block content %}
<div>
<h2>activite list</h2>
<table class="records_list">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Class</th>
<th>Position</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td>{{ entity.id }}</td>
<td>{{ entity.name }}</td>
<td>{{ entity.class }}</td>
<td>{{ entity.position }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
第二枝(延伸):
<page>
<page_header>
<h1> Header </h1>
</page_header>
{% block content %}
{% endblock %}
<page_footer>
<h3> Footer</h3>
</page_footer>
</page>
我得到的东西似乎是invamlid pdf:
%PDF-1.7 3 0 obj <> /Resources 2 0 R /Contents 4 0 R>> endobj 4 0 obj <> stream x���QO�0�����/���^5��㒾-{ ̔���{1����!���^~��][Be`f@��%~nA �ڻ�p5 ���>��ztޤ�dwM��4�F�?���Bm��]������0�&v?"Ld�|� ,.��8I��%���f~:x�M��E8d�Rs�jQ����-�R�de5H��$�K���_��Rs���:n�Gխ�>��/ZFO�R�P�C����VF��&.˱��>R��>�eVe�k�.k���רMh�:Nq��8<*�{D*o�4Z\�P��XQ9�C��$���:�p�t8l����p���@gm��6���n5��� endstream endobj 1 0 obj <> endobj 5 0 obj << /Type /OCG /Name (��print) /Usage << /Print <> /View <> >> >> endobj 6 0 obj << /Type /OCG /Name (��view) /Usage << /Print <> /View <> >> >> endobj 7 0 obj <> endobj 8 0 obj <> endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 7 0 R /F2 8 0 R >> /XObject << >> /Properties <> /ExtGState << >> >> endobj 9 0 obj << /Title (��un pdf test) /Creator (��HTML2PDF - TCPDF) /Producer (��TCPDF 5.0.002 \(http://www.tcpdf.org\) \(TCPDF\)) /CreationDate (D:20131103155855+00'00') /ModDate (D:20131103155855+00'00') >> endobj 10 0 obj << /Type /Catalog /Pages 1 0 R /OpenAction [3 0 R /XYZ null null 1] /PageLayout /SinglePage /PageMode /UseNone /Names << >> /ViewerPreferences << /Direction /L2R >> /OCProperties <> <>]>>>> >> endobj xref 0 11 0000000000 65535 f 0000000581 00000 n 0000001094 00000 n 0000000009 00000 n 0000000175 00000 n 0000000641 00000 n 0000000760 00000 n 0000000877 00000 n 0000000983 00000 n 0000001263 00000 n 0000001546 00000 n trailer << /Size 11 /Root 10 0 R /Info 9 0 R >> startxref 1918 %%EOF
有人可以帮我看看如何获得正确的pdf文件吗?
答案 0 :(得分:4)
这可能是 NOT 无效的PDF。
您正在使用错误的内容类型转移PDF文件,可能是text/html
而不是application/pdf
或application/force-download
。试试这段代码:
$content = $html2pdf->Output('', true);
$response = new Response();
$response->setContent($content);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-disposition', 'filename=my-document-name.pdf');
return $response;