Bonjour,

Mon objectif est de générer un fichier pdf contenant une liste de candidats.
Je bloque depuis quelques jours et je voudrais donc un coup de main.

j'ai installé le package via composer require knplabs/knp-snappy-bundle

Décrivez ici votre code ou ce que vous cherchez à faire

Voici comment se presente mon fichier app/config/package/knp_snappy.yaml
``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:    []

`
le contenu du Controller
``
<?php
namespace App\Controller;
use App\Entity\Candidat;
use App\Form\CandidatType;
use App\Form\TuteurType;
use App\Repository\CandidatRepository;
use Doctrine\Common\Persistence\ObjectManager;
//use Doctrine\Common\Persistence\ManagerInterface;
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";
    //$websiteurl = "ourcodeworld.com";
    //$snappy->get('knp_snappy.pdf')->generate('http://www.google.fr', '/path/to/the/file.pdf');
    return new Response(
        $snappy->getOutputFromHtml($html),
        200,
        array(
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="'.$filename.'.pdf"'
        )
    );

    }

}
`

Là l'erreur d'exécution que je reçois.

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.

Aucune réponse