Creating PDF Helper for CakePHP 2.0 using TCPDF

2019-08-03 09:23发布

I'm currently working on a PDF view for my cake application and found this tutorial.

http://www.startutorial.com/articles/view/how-to-create-pdf-helper-with-tcpdf

But currently I'm struggling over this error message:

Notice (8): Undefined variable: pdf [APP\View\Pdfs\index.ctp, line 2]
Notice (8): Trying to get property of non-object [APP\View\Pdfs\index.ctp, line 2]
Fatal error: Call to a member function addPage() on a non-object in \app\View\Pdfs\index.ctp on line 2

Can anyone help me out, what I have to adapt to CakePHP 2.0

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-03 09:51

When i use "$this->Pdf->core->Output('example_001.pdf', 'D');" pdf file is download fine. But when i use "$this->Pdf->core->Output('example_001.pdf', 'I');" to show in screen, i see:

%PDF-1.7 %���� 7 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20150318050138+01'00') /Resources 2 0 R /MediaBox [0.000000 0.000000 612.000000 792.000000] /CropBox [0.000000 0.000000 612.000000 792.000000] /BleedBox [0.000000 0.000000 612.000000 792.000000] /TrimBox [0.000000 0.000000 612.000000 792.000000] /ArtBox [0.000000 0.000000 612.000000 792.000000] /Contents 8 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Annots [ 6 0 R ] /PZ 1 >> endobj 8 0 obj <> stream x��SMo�0��W�#�棁� &Nl��I�e�T��2�I���ܢvCtZ�Ȋ��9/֋�f,� �$��zK�#�glb�%���2�=,]��t�uB\�G���#S����l��gO�:3��"*.~��Zr������^�UW���1�s�W�n��]�ϴ�J@�D���2��+�"ux����fsl!���t{��l3lH��'�-�����u�kO> endobj 3 0 obj <> endobj 5 0 obj <> endobj 4 0 obj << /Type /XObject /Subtype /Form /FormType 1 /Filter /FlateDecode /BBox [0.000000 0.000000 612.000000 28.350000] /Matrix [1 0 0 1 0 0] /Resources << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F2 5 0 R /F1 3 0 R >> >> /Length 161 >> stream x���?� �w?ō�bՇ���&���[�& �vh�|�>ҿ� �z�����@�T�B8`���Z�3:���[d��Q�:��|K^�2�z�Fp0�M7o���j���D��7*�{�ѯP]��r��o�sV�T�kqϛ���� �$�Ǒ3�#Y>�'��m�2�[� endstream endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 3 0 R /F2 5 0 R >> /XObject << /XT4 4 0 R >> >> endobj 6 0 obj <> /H /I>> endobj 9 0 obj << /Producer (��TCPDF 6.2.6 (tcpdf.org)) /CreationDate (D:20150318050138+01'00') /ModDate (D:20150318050138+01'00') /Trapped /False >> endobj 10 0 obj << /Type /Metadata /Subtype /XML /Length 4223 >> stream application/pdf 2015-03-18T05:01:38+01:00 2015-03-18T05:01:38+01:00 2015-03-18T05:01:38+01:00 TCPDF 6.2.6 (tcpdf.org) uuid:45f4b8fd-7424-3ef2-034a-a956c4a4d4ec uuid:45f4b8fd-7424-3ef2-034a-a956c4a4d4ec http://ns.adobe.com/pdf/1.3/ pdf Adobe PDF Schema http://ns.adobe.com/xap/1.0/mm/ xmpMM XMP Media Management Schema internal UUID based identifier for specific incarnation of a document InstanceID URI 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 11 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 10 0 R >> endobj xref 0 12 0000000000 65535 f 0000000832 00000 n 0000001547 00000 n 0000000891 00000 n 0000001108 00000 n 0000000997 00000 n 0000001672 00000 n 0000000015 00000 n 0000000483 00000 n 0000001943 00000 n 0000002140 00000 n 0000006446 00000 n trailer << /Size 12 /Root 11 0 R /Info 9 0 R /ID [ <45f4b8fd74243ef2034aa956c4a4d4ec> <45f4b8fd74243ef2034aa956c4a4d4ec> ] >> startxref 6655 %%EOF

查看更多
女痞
3楼-- · 2019-08-03 09:52

For Cake 2.1, use this code:

<?php
    $this->Pdf->core->addPage('', 'USLETTER');
    $this->Pdf->core->setFont('helvetica', '', 12);
    $this->Pdf->core->cell(30, 0, 'Hello World');
    $this->Pdf->core->Output('example_001.pdf', 'D');
?>
查看更多
登录 后发表回答