2013-11-03 52 views
2

应用上Symfony2.3与html2pdf_v4.03_php5HTML2PDF生成不正确的PDF

所述控制器:

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> 

我ge t东西似乎是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`[email protected]��%~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文件?

回答

4

这可能是不是无效的PDF。

您正在传输PDF文件的内容类型错误,可能是text/html而不是application/pdfapplication/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; 
+0

谢谢 - 现在一切正常! –

+1

@GustavTrennert:你应该[接受这个答案](http://meta.stackoverflow.com/help/someone-answers)。 –

+0

这部分应该在哪里'$ html2pdf-> writeHTML($ html);'go ???? – Pathros