简体   繁体   English

将 Symfony 包添加到 Silex 框架

[英]Add Symfony bundle to Silex Framework

I have a Silex project and I'm trying to generate a pdf file from a twig view with the KnpSnappyBundle .我有一个 Silex 项目,我正在尝试使用KnpSnappyBundle从树枝视图生成一个 pdf 文件。 I have the following code in my controller :我的控制器中有以下代码:

$this->app['knp_snappy.pdf'] = new KnpSnappyBundle($this->app, $this->client);

And then I have something like that :然后我有类似的东西:

if ($request->get('print') == 'print') {
    $html = $body;

    return new Response(
        $app['knp_snappy.pdf']->getOutputFromHtml($html),
        200,
        array(
            'Content-Type'          => 'application/pdf',
            'Content-Disposition'   => 'attachment; filename="file.pdf"'
        )
    );
}

I have the following error : Fatal error: Call to undefined method Knp\\Bundle\\SnappyBundle\\KnpSnappyBundle::getOutputFromHtml()我有以下错误: Fatal error: Call to undefined method Knp\\Bundle\\SnappyBundle\\KnpSnappyBundle::getOutputFromHtml()

Can you guys help me to fix that problem or to find another way to get pdf file from html with Silex (I tried domtopdf, it works, but it's ugly and it seems to not support svg images)...你们能帮我解决这个问题吗,或者找到另一种方法来使用 Silex 从 html 获取 pdf 文件(我试过 domtopdf,它有效,但它很丑,似乎不支持 svg 图像)...

Maybe I found another way to do that but I guess i'll have the same problem... PdfBundle也许我找到了另一种方法来做到这一点,但我想我会遇到同样的问题...... PdfBundle

KnpSnappyBundle is just a wrapper of SNappy library, therefore it doesn't make sense to try to inject it into Silex application. KnpSnappyBundle 只是 SNappy 库的封装,因此尝试将其注入 Silex 应用程序是没有意义的。 It's not worth it.这不值得。

Simply use Snappy .只需使用Snappy Register Pdf class as a service an than it will work as you expect it to do.Pdf类注册为服务,它会像您期望的那样工作。

Also you can check this SilexServiceProvider for Snappy if you're not sure how to put it into your DI container.如果您不确定如何将它放入您的 DI 容器,您也可以检查这个SilexServiceProvider for Snappy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM