0
votes

Is it not possible to generate a PDF using TCPDF while placing the HTML within the template view?

Example:

details.html.pdf:

<?php
    $html = ' //all the stuff i want to appear on the pdf ';

       $filename = $emailReport['subject']; //Outputs 'Email Report From Database' in output

       $pdf->writeHTMLCell($w = 250, $h = 200, $x = '', $y = '', $html, $border = 0, '', $fill = 0, $reseth = true, $align = 'center', $autopadding = true);
       $pdf->Output($filename,'I');
}

DefaultController.php

    public function printAction()
    {
        return $this->delegateView(
        [
            'viewParameters' => [
                'pdf' => $pdf
            ],
            'contentTemplate' => 'PrintoutBundle:Default:pdf.html.php',
            'passthroughVars' => [
                'activeLink'    => '#printout_email',
                'mauticContent' => 'email',
                'route'         => $this->generateUrl('printout_email'),
            ],
        ]
    );
    }

Gives me:

%PDF-1.7 %���� 7 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20161121165447-06'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 595.276000 841.890000] /CropBox [0.000000 0.000000 595.276000 841.890000] /BleedBox [0.000000 0.000000 595.276000 841.890000] /TrimBox [0.000000 0.000000 595.276000 841.890000] /ArtBox [0.000000 0.000000 595.276000 841.890000] /Contents 8 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /PZ 1 >> endobj 8 0 obj <> stream x���S�6���W��ݴ7 ,ɒ�}�]������\g��t�D$n+�\��ܿ{rb��HlCl�0!�l��<�G�,�v ��7����?����}��o���;����/���N�����o��dk�]�S(���~vN��+Sf�3?�:�G�?�c�b��/�������s�C ����w(�x~p�o��Y�l=��PBU��Q���r0@�֪��w�+|�?A�nfW // more of this for dozens more lines ��������j�W�\�6m�֭[/^LwY���:fp��A��\�vmpp�v16�� ��Q0�Bl5��T���A&�=`F�XX^!c�LK(� �_��WCCC���� � �Q���LK���W� endstream endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 3 0 R /F2 4 0 R /F3 5 0 R >> /XObject << /I0 13 0 R /I1 14 0 R /I2 15 0 R /I3 16 0 R >> >> endobj 6 0 obj <> /H /I>> endobj 17 0 obj << /Title (��Report Generated Nov 21, 2016) /Subject (��Email Subject from Database) /Producer (��TCPDF 6.2.12 (tcpdf.org)) /CreationDate (D:20161121165447-06'00') /ModDate (D:20161121165447-06'00') /Trapped /False >> endobj 18 0 obj << /Type /Metadata /Subtype /XML /Length 4307 >> stream application/pdf Generated Nov 21, 2016 Email Subject from the Database 2016-11-21T16:54:47-06:00 2016-11-21T16:54:47-06:00 2016-11-21T16:54:47-06:00 TCPDF 6.2.12 (tcpdf.org) uuid:4945196b-d05c-76e8-8a79-e9009fcad893 uuid:4945196b-d05c-76e8-8a79-e9009fcad893 ns.adobe.com/pdf/1.3/ pdf Adobe PDF Schema ns.adobe.com/xap/1.0/mm/ xmpMM XMP Media Management Schema internal UUID based identifier for specific incarnation of a document InstanceID URI www.aiim.org/pdfa/ns/id/ pdfaid PDF/A ID Schema internal Part of PDF/A standard part Integer internal Amendment of PDF/A standard amd Text internal Conformance level of PDF/A standard conformance Text endstream endobj 19 0 obj << /Type /Catalog /Version /1.7 /Pages 1 0 R /Names << >> /ViewerPreferences << /Direction /L2R >> /PageLayout /SinglePage /PageMode /UseNone /OpenAction [7 0 R /FitH null] /Metadata 18 0 R >> endobj xref 0 20 0000000000 65535 f 0000006237 00000 n 0000035774 00000 n 0000006309 00000 n 0000006415 00000 n 0000006526 00000 n 0000035942 00000 n 0000000015 00000 n 0000000465 00000 n 0000003474 00000 n 0000003925 00000 n 0000004460 00000 n 0000004930 00000 n 0000006640 00000 n 0000006938 00000 n 0000014933 00000 n 0000023217 00000 n 0000036214 00000 n 0000036602 00000 n 0000040992 00000 n trailer << /Size 20 /Root 19 0 R /Info 17 0 R /ID [ <4945196bd05c76e88a79e9009fcad893> <4945196bd05c76e88a79e9009fcad893> ] >> startxref 41201 %%EOF

However, if I place all of my code within the controller and completely skip using the template file, it loads perfectly.

$this->delegateView() sends data from my controller to the template. (I've also tried making this $html = $this->delegateView(..) to no avail.)

Editing the controller like so:

        return $pdf->writeHTMLCell($this->delegateView(
        [   'Content-Type'        => 'application/pdf',
            'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), //$pdf->Output($filename,'I'),
            'viewParameters' => [
                'pdf' => $pdf,
                'objectId'     => $objectId
                //more stuff sent to the template
            ]
        ]
    ));

in attempts to generate the PDF (while the content has been moved from the controller to the template), the PDF does not create. I think I am wrapping the TCPDF service incorrectly, but I cannot figure out how to do it appropriately.

2
1 -- I think you mean details.html.php. 2 -- The Output command takes as first argument a pdf extension. Is $filename a PDF?ZugZwang
Yes $filename takes the subject and rewrites to a .pdf extensionbillblast
In the last example you wrote, you're passing wrong arguments to the writeHTMLCell method. Focus on the first example. Have you tried to launch ob_clean() before the Output command?ZugZwang
I haven't thought to try ob_clean(), though it wouldn't be required in this circumstance, would it? I was able to get it to render, I've posted the code below.billblast

2 Answers

0
votes

You should change The second Parameter of the output method to string "S". Outsource the creation of the pdf in a symfony service and use a method like "createPdf()" to get The return value of output method of tcpdf. (that's the pdf Binary content)

To get a valid PDF output, you should change the content-type header to application/pdf

If you are using a template to render your page, you get the html content-type defaultly.

0
votes

Although imperfect, I got it to load:

$html = $this->delegateView(
        [
            'Content-Type'        => 'application/pdf',
            'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), //$pdf->Output($filename,'I'),
            'viewParameters' => [
//variables being sent to the template here
                ]
        ]
    );
    $pdf->writeHTMLCell($w = 250, $h = 200, $x = '', $y = '', $html, $border = 0, '', $fill = 0, $reseth = true, $align = 'center', $autopadding = true);
    return new Response($pdf->Output($filename,'I'));

}

}

However, it doesn't render JS just yet and the

HTTP/1.0 200 OK Cache-Control: no-cache

header is at the top. A work in progress.