强制下载html2pdf生成的pdf文件

时间:2015-08-17 12:09:55

标签: cakephp pdf html2pdf

我正在使用html2pdf转换html行数据以创建pdf文件。我正在使用CakePHP 2.6版。当我点击下载时,它会生成一些代码。

我添加了header("Content-Disposition: attachment; filename=sample.pdf");,但这对我不起作用。

我的代码片段:

header("Content-Disposition: attachment; filename=sample.pdf");
include APP.'Vendor'.DS.'html2pdf'.DS.'html2pdf.class.php'; 
$content = $template['Template']['content'];
$html2pdf = new HTML2PDF('P','A4','en');
$html2pdf->WriteHTML($content);
$html2pdf->Output($template['Template']['title'].'_121.pdf', 'D');

错误代码:

%PDF-1.7
3 0 obj
<</Type /Page /Parent 1 0 R /MediaBox [0 0 595.28 841.89] /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Resources 2 0 R
/Contents 4 0 R>> endobj
4 0 obj
<</Filter /FlateDecode /Length 224>> stream
xÅ1oÂ0wÿ7Å=[)ÎZT@btb°kTùûuÒP1PUlËwïýélÒO-,V ì±ÞÄ%c  £1âü9+WÔ¥¸½;}k~?á/ñå\æÑ êÑ`Î]Ä$Ú8¤êÔs¬^ÄW0ÖL6àU§Êþv¿{18jJ¤Ð òxðܦ×Ø47M¶(o}x¯/àþé]4SpñböQïÔ5»àëÅc©Ôé.÷kÕ'ûå
endstream endobj
1 0 obj <</Type /Pages  /Kids [ 3 0 R ] /Count 1 >>  endobj
5 0 obj
<< /Type /OCG /Name (þÿprint) /Usage << /Print <</PrintState /ON>> /View <</ViewState /OFF>> >> >> endobj
6 0 obj
<< /Type /OCG /Name (þÿview) /Usage << /Print <</PrintState /OFF>> /View <</ViewState /ON>> >> >> endobj
7 0 obj
<</Type /Font /Subtype /Type1 /BaseFont /Helvetica /Name /F1 /Encoding /WinAnsiEncoding >> endobj
8 0 obj
<</Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Name /F2 /Encoding /WinAnsiEncoding >> endobj
2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 7 0 R /F2 8 0 R >> /XObject << >> /Properties <</OC1 5 0 R /OC2 6 0 R>> /ExtGState << >> >> endobj
9 0 obj
<< /Creator (þÿHTML2PDF - TCPDF) /Producer (þÿTCPDF 5.0.002 \(http://www.tcpdf.org\) \(TCPDF\)) /CreationDate (D:20150817174514+05'30') /ModDate (D:20150817174514+05'30') >> endobj
10 0 obj
<< /Type /Catalog /Pages 1 0 R /OpenAction [3 0 R /FitH null] /PageLayout /SinglePage /PageMode /UseNone /Names << >> /ViewerPreferences << /Direction /L2R >> /OCProperties <</OCGs [5 0 R 6 0 R] /D <</ON [5 0 R] /OFF [6 0 R] /AS [<</Event /Print /OCGs [5 0 R 6 0 R] /Category [/Print]>> <</Event /View /OCGs [5 0 R 6 0 R] /Category [/View]>>]>>>> >> endobj
xref
0 11
0000000000 65535 f 
0000000469 00000 n 
0000000982 00000 n 
0000000009 00000 n 
0000000175 00000 n 
0000000529 00000 n 
0000000648 00000 n 
0000000765 00000 n 
0000000871 00000 n 
0000001151 00000 n 
0000001400 00000 n 
trailer << /Size 11 /Root 10 0 R /Info 9 0 R >>
startxref
1766
%%EOF

1 个答案:

答案 0 :(得分:2)

然后您需要在href中创建自己的下载链接,并在控制器中die;之后创建$html2pdf->Output();