Php html2pdf生成不正确的pdf

Php html2pdf生成不正确的pdf,php,symfony,pdf,Php,Symfony,Pdf,html2pdf_v4.03_php5在Symfony2.3上的应用 控制员: public function printAction() { $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository('MyBundle:Activite')->findAll(); $html=$this->get('templating')->render('

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文件吗?

这可能不是一个无效的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;
这可能不是无效的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;

@古斯塔夫特伦纳特:你应该。在所有这些之前lines@GustavTrennert字体你应该在所有这些行之前