Difficultés avec knpSnappy
Bonjour,
Je tente de générer un fichier pdf contenant une liste de candidats et pour cela j’utilise le bundle knpSnappy.
Je l'ai installé via composer :
composer require knplabs/knp-snappy-bundle
Voici le contenu de app/config/package/knp_snappy.yaml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| knp_snappy:
pdf:
enabled: true
binary: "\"C:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\""
options:
## set default settings
no-outline: true
page-size: LETTER
# recommend to set utf-8 as default encoding
encoding: UTF-8
image:
enabled: true
binary: "\"C:\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\""
options: [] |
celui du controller
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?php
namespace App\Controller;
use App\Entity\Candidat;
use App\Entity\Tuteur;
use App\Form\CandidatType;
use App\Form\TuteurType;
use App\Repository\CandidatRepository;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Knp\Bundle\SnappyBundle\Snappy;
use Knp\Snappy\Pdf;
use Knp\Bundle\SnappyBundle\DependencyInjection;
class VanController extends AbstractController
{
/**
* @Route("/listCandidats", name="van_list", methods={"GET"})
*
*/
public function listCandidats(Request $request)
{
$repo = $this->getDoctrine()->getRepository(Candidat::class);
$candidat = $repo->findAll();
$snappy = $this->get("knp_snappy.pdf");
//$snappy->setOption("encoding", "UTF-8");
$html = $this->renderView('van/list.html.twig', [
'candidats'=> $candidat,
'title' => "Welcome to our PDF Test"
]);
$filename = "myfirst_pdf_with_snappy";
return new Response(
$snappy->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filename.'.pdf"'
)
);
}
} |
A l'exécution j'ai le message d'erreur suivant que je n'arrive pas à comprendre
Citation:
ServiceNotFoundException
HTTP 500 Internal Server Error
Service "knp_snappy.pdf" not found: even though it exists in the app's container, the container inside "App\Controller\VanController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.
Je serais ravi d'avoir des idées pour sortir de ce pétrin. Merci