symfonypdfsymfony4knp-snappy

KnpSnappyBundle file was not created


I'm on Symfony4 and I'm using knpSnappyBundle. And I meet a strange error than I can't resolve:

[snappy] An error happened while generating "/tmp/knp_snappy5c86716975a458.88561675.pdf". ["command" => "/var/www/html/gestImmo/bin/wkhtml2pdf --lowquality --user-style-sheet './build/app.css' '/tmp/knp_snappy5c867169759ed9.86228916.html' '/tmp/knp_snappy5c86716975a458.88561675.pdf'","status" => 127,"stdout" => "/var/www/html/gestImmo/bin/wkhtmltopdf: error while loading shared libraries: libQt5Svg.so.5: cannot open shared object file: No such file or directory\n","stderr" => ""]

In AbstractGenerator.php line 350:

The file '/tmp/knp_snappy5c86716975a458.88561675.pdf' was not created 
(command: /var/ww  
w/html/gestImmo/bin/wkhtml2pdf --lowquality --user-style-sheet 
'./build/app.css' '/tmp/  
knp_snappy5c867169759ed9.86228916.html' '/tmp/knp_snappy5c86716975a458.88561675.pdf').

Maybe you want my code so here is my service `

namespace App\Service;

use App\Entity\RentRelease;
use App\Repository\PropertyRepository;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Knp\Snappy\Pdf;

class PdfGenerator
{
/**
 * @var PropertyRepository
 */
private $propertyRepository;

/**
 * @var Pdf
 */
private $knpSnappyPdf;

/**
 * @var EntityManagerInterface
 */
private $em;

/**
 * @var \Twig_Environment
 */
private $twig;

public function __construct(
    PropertyRepository $propertyRepository,
    Pdf $knpSnappyPdf,
    EntityManagerInterface $em,
    \Twig_Environment $twig
) {
    $this->propertyRepository = $propertyRepository;
    $this->knpSnappyPdf = $knpSnappyPdf;
    $this->em = $em;
    $this->twig = $twig;
}

public function generateRentReleasePdf()
{
    $rentRelease = $this->em->getRepository(RentRelease::class)->findAll();

    foreach ($rentRelease as $release) {
        $date = $release->getDate()->format('m-Y');
        $currentDate = new \DateTime();
        $currentDate = $currentDate->format('m-Y');
        if ($date === $currentDate) {
            $propertyName = $release->getPropertyName();
            $lesseeName = $release->getLesseeName();

            $html = $this->twig->render('rent_release/pdf.html.twig', [
                'rent_release' => $release,
            ]);

            return new PdfResponse(
                $this->knpSnappyPdf->getOutputFromHtml($html, [
                    'user-style-sheet' => ['./build/app.css',],
                ]),
                '/public/generated/' . $propertyName . '_' . $lesseeName . '_' . date("m-Y") . '.pdf'
            );
        }
    }
}
}

And here is wkhtml2pdf from my bin/ directory

#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

PROD_ENV=true

if [ $PROD_ENV == true ]
then
   xvfb-run $DIR/wkhtmltopdf $@
else
    wkhtmltopdf $@
fi

I'm not the one who create the wkhtml2pdf algorithm and to be honnest I don't really understand how it works, all that I know is than I already use it in another Project and it works fine..


Solution

  • It is requesting a library that you dont have...

    libQt5Svg.so.5: cannot open shared object file: No such file or directory

    If you are using Ubuntu, install it.

    sudo apt-get install libqt5svg5