首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用TCPDF与Symfony混合输出,除非我将$pdf放在控制器中。

使用TCPDF与Symfony混合输出,除非我将$pdf放在控制器中。
EN

Stack Overflow用户
提问于 2016-11-21 23:15:06
回答 2查看 934关注 0票数 0

在将HTML放置在模板视图中时,不能使用TCPDF生成PDF吗?

示例:

details.html.pdf:

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
    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'),
            ],
        ]
    );
    }

给我:

%PDF-1.7 % << /Type /Page /Parent 1 0 R /LastModified (D:20161121165447-06'00') /Resources 2-06‘00’/Resources 2 0 R /MediaBox 841.890000 595.276000 841.890000 /CropBox 841.890000 595.276000 841.890000 /BleedBox /BleedBox 595.276000 595.276000 841.890000 /TrimBox D 595.276000 841.890000 /ArtBox /Parent /Rotate 8 0 R/Rotate 0/Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /PZ 1 >> endobj 8 0 obj <>小溪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 //更多的几十行��������j�W�\�6m/^LwY���:fp��A��\�vmpp�v16����Q0�Bl5��T���A&�=`F�XX^!C高中LK(�_��WCCC����,Q,LK W,endstream endstream 2.0 obj << /ProcSet /PDF /Text /ImageB /ImageC /ImageI /Font << /F1 3 0 R/F2 4 0 R /F3 5 0 R >> /XObject /F3) 13 0 R 19# 14 0 R/I2 15 0 R /I3 16 0 R >> >> endobj 6 0 obj <> /H /I>> endobj 17 0 obj << /Title (��报告生成11月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 >>流应用程序/pdf生成11月21日,2016年电子邮件主题:数据库2016-11-21T16:54:47-06:00 2016 -06:54:47-06:00 2016-06:00 2016-06:00 TCPDF 6.2.12 (tcpdf.org) uuid:4945196b-d05c-76e8-8a79-e9009fcad893 UUID :4945196b-d05c-76e8-8a79-e9009fcad8a79-e9009fcad8adob.com/ pdf /1.3/ PDF文件InstanceID URI www.aiim.org/pdfa/ns/ ID / pdfaid PDF/A ID Schema的内部部分PDF/A标准部件的内部修正PDF/A标准和文本内部一致性级别PDF/A标准文本的内部一致性级别00000 n 0000006415 00000 n 0000006526 00000 n 0000035942 00000 n 000000000 n 0000000465 00000 n 0000003474 00000 000 n 0000003925 00000 n 0000000000000 n 00000000446000000 n 00000000493000000 n 0000000000000 n 000000664000000 n 000000000 n 0000014933 00000 n 0000023217 000 n 0000036214 00000 n 000003660000 n 0000040992 00000 n << /Size /Size 20 /Root 19 0 R /ID <4945196bd05c76e88a79e9009fcad893> <4945196bd05c76e88a79e9009fcad893> >> startxref 41201 %

但是,如果我将所有代码都放在控制器中,并完全跳过使用模板文件,那么它将非常完美地加载。

$this->delegateView()将数据从我的控制器发送到模板。(我也尝试过让这个$html =$ this ->delegateView(.)(没有任何结果)

编辑控制器如下:

代码语言:javascript
运行
复制
        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
            ]
        ]
    ));

在尝试生成PDF时(当内容从控制器移动到模板时),PDF不会创建。我认为我正在错误地包装TCPDF服务,但我不知道如何正确地完成它。

EN

回答 2

Stack Overflow用户

发布于 2016-11-22 17:08:07

您应该将输出方法的第二个参数更改为字符串"S“。外包在symfony服务中创建pdf,并使用像"createPdf()“这样的方法来获得tcpdf输出方法的返回值。(这是pdf二进制内容)

要获得有效的PDF输出,您应该将内容类型的标题更改为application/pdf。

如果使用模板呈现页面,默认情况下将得到html内容类型。

票数 0
EN

Stack Overflow用户

发布于 2016-11-23 17:20:35

虽然不完美,但我让它加载:

代码语言:javascript
运行
复制
$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'));

}

}

但是,它还没有呈现JS,而且

HTTP/1.0 200 OK Cache-Control:无缓存

头部在顶部。一项正在进行的工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40731157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档