如何使用 Symfony 4 从网络 URL 生成 pdf?
How can I generate a pdf from web URL with Symfony 4?
我尝试使用 snappy 包从 webURL 生成 pdf:
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.whosebug.com";
return new Response(
$snappy->getOutput($webSiteURL),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filname.'.pdf"'
)
);
}
但是当我尝试打开 pdf 时,我收到错误消息:
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
这是我的 config/packages/knp_snappy.yaml 文件:
knp_snappy:
pdf:
enabled: true
binary: /usr/local/bin/wkhtmltopdf
options: []
image:
enabled: true
binary: /usr/local/bin/wkhtmltoimage
options: []
解决此问题的一种方法是,我尝试将 use Knp\Component\Pager\PaginatorInterface;
添加到我的控制器,但随后我收到错误消息:
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".
解决此问题的另一种方法是将其添加到我的控制器中:
public static function getSubscribedServices(): array
{
$services = parent::getSubscribedServices();
$services['fos_elastica.manager'] = RepositoryManagerInterface::class;
$services['knp_paginator'] = PaginatorInterface::class;
return $services;
}
但随后我收到错误消息:
The service "App\Controller\PagesController" has a dependency on a
non-existent service "App\Controller\RepositoryManagerInterface".
当 Controller
扩展 AbstractController
时,它无法访问负责包含服务的 container
。尝试扩展 Controller
(请注意,此 class 将在 Symfony 5 中删除)或在 services.yaml 文件
的控制器中注入 snappy 服务
您可以直接将依赖项注入到控制器的操作中,例如
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.whosebug.com";
return new Response(
$snappy->getOutput($webSiteURL),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filname.'.pdf"'
)
);
}
确保在 service.yml 文件中启用服务自动装配
https://symfony.com/doc/current/service_container/autowiring.html#
我尝试使用 snappy 包从 webURL 生成 pdf:
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.whosebug.com";
return new Response(
$snappy->getOutput($webSiteURL),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filname.'.pdf"'
)
);
}
但是当我尝试打开 pdf 时,我收到错误消息:
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
这是我的 config/packages/knp_snappy.yaml 文件:
knp_snappy:
pdf:
enabled: true
binary: /usr/local/bin/wkhtmltopdf
options: []
image:
enabled: true
binary: /usr/local/bin/wkhtmltoimage
options: []
解决此问题的一种方法是,我尝试将 use Knp\Component\Pager\PaginatorInterface;
添加到我的控制器,但随后我收到错误消息:
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".
解决此问题的另一种方法是将其添加到我的控制器中:
public static function getSubscribedServices(): array
{
$services = parent::getSubscribedServices();
$services['fos_elastica.manager'] = RepositoryManagerInterface::class;
$services['knp_paginator'] = PaginatorInterface::class;
return $services;
}
但随后我收到错误消息:
The service "App\Controller\PagesController" has a dependency on a non-existent service "App\Controller\RepositoryManagerInterface".
当 Controller
扩展 AbstractController
时,它无法访问负责包含服务的 container
。尝试扩展 Controller
(请注意,此 class 将在 Symfony 5 中删除)或在 services.yaml 文件
您可以直接将依赖项注入到控制器的操作中,例如
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.whosebug.com";
return new Response(
$snappy->getOutput($webSiteURL),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filname.'.pdf"'
)
);
}
确保在 service.yml 文件中启用服务自动装配 https://symfony.com/doc/current/service_container/autowiring.html#