The exit status code '1' says something went wrong [knp snappy]
Asked Answered
R

5

5

I'm working with knp snappy bundle for generate reports from my app. But doesn't work, when I call my controller for generate the pdf throws the next error:

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". 

My controller is this:

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"'
                )
            );
    }

I copied my url and run into console[CMD] with wkhtmltopdf.exe. In console my pdf is created but when I see the file only shows my login screen. So I think could be a issue about permission into my controller but I don't know how I pass this.

In other hand, if you know about another pdf generator(from html) that working into symfony this could be help me too.

I'm working under symfony2.7

Reduplication answered 29/12, 2015 at 13:36 Comment(0)
H
12

This mean's that one of the resources in your HTML file can't be loaded by wkhtmltopdf, check in your HTML file if all your resources have an absolute URL like this : http://www.yourserver.com/images/myimage.jpg

If it's not the case try to set it :

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

If you are loading assets with assetic in {% stylesheets %} or {% javascript %} or {% image %} you can also do

{{ app.request.schemeAndHttpHost ~ asset_url }}
Hargett answered 29/12, 2015 at 14:18 Comment(3)
I had this same issue and error message with wkhtmltopdfin laravel 5, and this was the problem. I had CDN assets that threw the errors. Once I used local versions instead, the PDF generation ran as expected.Oscillogram
Hi, where should i put {{ app.request.schemeAndHttpHost ~ 'resource relative url' }} ? in html html content ? o_oFrei
how about <script src="https://cdn.tailwindcss.com"></script>? How can I load tailwind without causing the error above?Phinney
A
0

I had two kind of problems here once:

The first one was because kpn snappy fails when trying to convert a *.js file. So I had to take *.js files in my case.

The second one was because of the path. I had to use the absolute filesystem path. By this, I mean, something like: /home/ubuntu/workspace/bootstrap/css/bootstrap.css this is used in href tag used in *html.twig file that is then transformed to pdf by snappy.

By doing this, I used some twig global variables.

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"

In twig

<link rel="stylesheet" type="text/css" href="{{ bootstrapCss }}">
<link rel="stylesheet" type="text/css" href="{{ layoutCss }}">
<link rel="stylesheet" type="text/css" href="{{ mainCss }}">
Aught answered 8/4, 2016 at 15:17 Comment(0)
M
0

You can use absolute_url() twig function{{ absolute_url(asset('assets/image.jpg')) }}

Mayda answered 7/8, 2016 at 16:40 Comment(0)
K
0

For me this worked, the error was from html

<!doctype html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    {% for path in encore_entry_css_files('app') %}
        <link rel="stylesheet" href="{{ absolute_url(path) }}">
    {% endfor %}
</head>
Kikuyu answered 24/8, 2022 at 12:24 Comment(0)
B
0

This problem never gets old, I encountered this just now and I manage to solve it, here's how.

This problem mostly happens in Windows so this solution is for that.

1. Install this composer dependency wemersonjanuario/wkhtmltopdf-windows
composer require wemersonjanuario/wkhtmltopdf-windows "0.12.2.3"
2. Open your config/snappy.php and use this config.
<?php

return [
    'pdf' => [
        'enabled' => true,
        'binary'  => base_path('vendor\wemersonjanuario\wkhtmltopdf-windows\bin\64bit\wkhtmltopdf'),
        'timeout' => false,
        'options' => [],
        'env'     => [],
    ],

    'image' => [
        'enabled' => true,
        'binary'  => base_path('vendor\wemersonjanuario\wkhtmltoimage-windows\bin\64bit\wkhtmltoimage'),
        'timeout' => false,
        'options' => [
            'enable-local-file-access' => true,
            'keep-relative-links' => true,
        ],
        'env'     => [],
    ],
];
3. Make sure the blade file you loading for pdf does not contain external links, like the one below.
<head>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,300;0,400;0,600;0,700;1,300;1,400;1,600;1,700&display=swap" rel="stylesheet">
</head>
4. Make sure you load assets using public_path()
<link href="{{ public_path('css/pdf.css') }}" rel="stylesheet">


<img src="{{ public_path('fancy-image.png') }}">
Bluebeard answered 27/10, 2022 at 5:27 Comment(1)
how about <script src="cdn.tailwindcss.com"></script>? How can I load tailwind without causing the error above?Phinney

© 2022 - 2024 — McMap. All rights reserved.