-1

I try to generate a pdf from webURL with the snappy bundle:

class PagesController extends AbstractController
{
 /**
  * @Route("/pdf", name="pdf")
  */

  public function pdf(Request $request)
  {
    $snappy = $this->get("knp_snappy.pdf");
    $snappy->setOption("encoding","UTF-8");

    $filename = "mypdf";
    $webSiteURL = "http://www.stackoverflow.com";
    return new Response(
      $snappy->getOutput($webSiteURL),
      200,
      array(
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="'.$filname.'.pdf"'
      )
    );
  }

But when I try to open the pdf I get the error message:

Service "knp_snappy.pdf" not found: even though it exists in the app's container, the container inside "App\Controller\PagesController" 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

This is my config/packages/knp_snappy.yaml file:

knp_snappy:
    pdf:
        enabled:    true
        binary:     /usr/local/bin/wkhtmltopdf
        options:    []
    image:
        enabled:    true
        binary:     /usr/local/bin/wkhtmltoimage
        options:    []

One approach to solve this was, I tried to add use Knp\Component\Pager\PaginatorInterface; to my Controller, but then I get the error message:

Cannot determine controller argument for "App\Controller\PagesController::pdf()": the $paginator argument is type-hinted with the non-existent class or interface: "Knp\Component\Pager\PaginatorInterface".

Another approach to solve this was adding to my controller:

  public static function getSubscribedServices(): array
{
    $services = parent::getSubscribedServices();
    $services['fos_elastica.manager'] = RepositoryManagerInterface::class;
    $services['knp_paginator'] = PaginatorInterface::class;

    return $services;
}

But then I get the error message:

The service "App\Controller\PagesController" has a dependency on a non-existent service "App\Controller\RepositoryManagerInterface".

2 Answers2

0

When a Controller extends the AbstractController it doesn’t have access to container which is responsible to contains services. Try to extends the Controller (note that this class will be removed in Symfony 5) or inject the snappy services in the Controller in the services.yaml file

Smaïne
  • 1,374
  • 12
  • 19
0

You can directly inject your dependencies into controller’s action like

use Knp\Snappy\Pdf;
class PagesController extends AbstractController
{
  /**
  * @Route("/pdf", name="pdf")
  */

  public function pdf(Request $request, Pdf $snappy)
  {
      $snappy->setOption("encoding","UTF-8");

      $filename = "mypdf";
      $webSiteURL = "http://www.stackoverflow.com";
      return new Response(
         $snappy->getOutput($webSiteURL),
      200,
      array(
         'Content-Type' => 'application/pdf',
         'Content-Disposition' => 'inline; filename="'.$filname.'.pdf"'
      )
    );
   } 

Make sure service autowiring enabled in service.yml file https://symfony.com/doc/current/service_container/autowiring.html#

Seybsen
  • 14,989
  • 4
  • 40
  • 73
slmder_h
  • 201
  • 1
  • 4
  • "Pfd" do you mean "Pdf"? –  May 27 '19 at 13:48
  • `Cannot determine controller argument for "App\Controller\PagesController::pdf()": the $snappy argument is type-hinted with the non-existent class or interface: "App\Controller\Pdf". Did you forget to add a use statement?` –  May 27 '19 at 13:50
  • 1
    Add use Knp\Snappy\Pdf; statement – slmder_h May 27 '19 at 14:17