【问题标题】:The exit status code '1' says something went wrong [knp snappy]退出状态代码“1”表示出了点问题 [knp snappy]
【发布时间】:2016-04-03 09:47:17
【问题描述】:

我正在使用 knp snappy bundle 从我的应用程序生成报告。但是不起作用,当我调用我的控制器生成 pdf 时会引发下一个错误:

The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
[=======> ] 12%
Warning: Failed to load file:///gestionresiduospel/web/bundles/usuario/css/bootstrap.css (ignore)
Warning: Failed to load file:///gestionresiduospel/web/bundles/usuario/js/bootstrap.js (ignore)
[============================================================] 100%
Counting pages (2/6)
[============================================================] Object 1 of 1
Resolving links (4/6)
[============================================================] Object 1 of 1
Loading headers and footers (5/6)
Printing pages (6/6)
[> ] Preparing
[============================================================] Page 1 of 1
Done
Exit with code 1 due to network error: ContentNotFoundError
"
stdout: ""
command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" --lowquality "C:\Users\rodrigo\AppData\Local\Temp\knp_snappy5682878973ade7.09664058.html" "C:\Users\rodrigo\AppData\Local\Temp\knp_snappy56828789746968.46649162.pdf". 

我的控制器是这样的:

public function historicosDetallePdfAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        //gets residuos peligrosos.
        $solicitudRetiro= $em->getRepository('SolicitudRetiroBundle:SolicitudRetiro')->find($id);
        //gets residuos no peligrosos
        $residuosPRetirados = $em->getRepository('SolicitudRetiroBundle:RetiroResiduo')->findResiduos($id);
        //retorna los residuos no peligrosos en la solicitud
        $residuosNPRetirados = $em->getRepository('SolicitudRetiroBundle:RetiroResiduoNoPeligroso')->findResiduosNoPeligrosos($id);
        // view generator
        $html = $this->renderView('SolicitudRetiroBundle:Pdf:resumenSolicitudResiduoRetirado.html.twig', array(
            'solicitudRetiro' => $solicitudRetiro,  
            'residuosRetirados' => $residuosPRetirados,
            'residuosNoPel' => $residuosNPRetirados ));
        // pdf generator
        return new Response(
            $this->get('knp_snappy.pdf')->getOutputFromHtml($html), 
            200,
            array(
                'Content-Type' => 'application/pdf',
                'Content-Disposition' => 'attachment; filename="historico.pdf"'
                )
            );
    }

我复制了我的网址并使用 wkhtmltopdf.exe 运行到控制台[CMD]。在控制台中创建了我的 pdf,但是当我看到该文件时只显示我的登录屏幕。所以我认为可能是关于我的控制器权限的问题,但我不知道我是如何通过这个的。

另一方面,如果你知道另一个 pdf 生成器(来自 html)可以用于 symfony,这对我也有帮助。

我在symfony2.7下工作

【问题讨论】:

    标签: symfony pdf-generation wkhtmltopdf


    【解决方案1】:

    这意味着你的 HTML 文件中的一个资源不能被 wkhtmltopdf 加载,如果你的所有资源都有这样的绝对 URL,请检查你的 HTML 文件:http://www.yourserver.com/images/myimage.jpg

    如果不是这样,请尝试设置它:

    {{ app.request.schemeAndHttpHost ~ 'resource relative url' }}
    

    如果您在 {% stylesheets %}{% javascript %}{% image %} 中加载资产,您也可以这样做

    {{ app.request.schemeAndHttpHost ~ asset_url }}
    

    【讨论】:

    • 我在 laravel 5 中的 wkhtmltopdf 遇到了同样的问题和错误消息,这就是问题所在。我有引发错误的 CDN 资产。一旦我改用本地版本,PDF 生成就会按预期运行。
    • 嗨,我应该把 {{ app.request.schemeAndHttpHost ~ 'resource relative url' }} 放在哪里?在 html html 内容中? o_o
    【解决方案2】:

    我曾经在这里遇到过两种问题:

    第一个是因为 kpn snappy 在尝试转换 *.js 文件时失败。所以我不得不在我的情况下使用 *.js 文件。

    第二个是因为路径。我不得不使用绝对文件系统路径。我的意思是,类似于:/home/ubuntu/workspace/bootstrap/css/bootstrap.css 这用于 *html.twig 文件中使用的 href 标记,然后由 snappy 转换为 pdf。

    通过这样做,我使用了一些 twig 全局变量。

    twig:
        form:
            resources:
                - 'MyBundle:Form:fields.html.twig'
                - 'MyUploaderBundle:Form:fields.html.twig'
        debug:            "%kernel.debug%"
        strict_variables: "%kernel.debug%"
        globals:
            pathToWeb: "%kernel.root_dir%/../web"
            bootstrapCss: "/home/ubuntu/workspace/MyApp/web/bootstrap/css/bootstrap.css"
            mainCss: "/home/ubuntu/workspace/MyApp/web/css/main.css"
            layoutCss: "/home/ubuntu/workspace/MyApp/web/css/layout.css"
    

    在树枝上

    <link rel="stylesheet" type="text/css" href="{{ bootstrapCss }}">
    <link rel="stylesheet" type="text/css" href="{{ layoutCss }}">
    <link rel="stylesheet" type="text/css" href="{{ mainCss }}">
    

    【讨论】:

      【解决方案3】:

      你可以使用absolute_url()树枝函数{{ absolute_url(asset('assets/image.jpg')) }}

      【讨论】:

        猜你喜欢
        • 2016-11-29
        • 1970-01-01
        • 2018-07-27
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 2019-08-28
        • 2021-10-23
        • 1970-01-01
        相关资源
        最近更新 更多