【问题标题】:You have requested a non-existent service "knp_snappy.pdf"您请求了一个不存在的服务“knp_snappy.pdf”
【发布时间】:2018-03-02 15:58:48
【问题描述】:

我按照https://github.com/KnpLabs/KnpSnappyBundlehttps://ourcodeworld.com/articles/read/250/how-to-create-a-pdf-from-html-using-knpsnappybundle-wkhtmltopdf-in-symfony-3 上的教程进行操作。

app/config/config.yml

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

应用程序/应用程序内核

new Knp\Bundle\SnappyBundle\KnpSnappyBundle(),

我的控制器

use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;

public function pdfAction(string $offerId)
{
    $html = $this->renderView('@App/Offer/offer_pdf.html.twig', array(
        'offerId'  => $offerId
    ));

    return new PdfResponse(
        $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
        'file.pdf'
    );
}

我试试这个:

 public function offerToPDFAction(string $offerId)
{

    $snappy = $this->container->get('knp_snappy.pdf');

    $html = '<h1>Hello</h1>';

    $filename = 'myFirstSnappyPDF';

    return new Response(
        $snappy->getOutputFromHtml($html),
        200,
        array(
            'Content-Type'          => 'application/pdf',
            'Content-Disposition'   => 'attachment; filename="'.$filename.'.pdf"'
        )
    );

同样的问题: 您请求了一个不存在的服务“knp_snappy.pdf”。

【问题讨论】:

  • config.yml 中的缩进只是复制/粘贴问题? php bin/console debug:container 是否显示 knp_snappy.pdf 服务?
  • 你清除缓存了吗?
  • 我认为这会使您的 .yml 无效 => enter code here,请尝试删除它
  • 我检查了 php bin/console debug:container 它出现了,我清除了缓存,但没有任何结果。 “在此处输入代码” - 它出现在 stackoverflow 中,我在代码中没有
  • 这很奇怪。可以肯定的是,您的控制器扩展了 symfony FrameworkBundle 控制器,例如“类 DefaultController 扩展控制器”?您可以加载任何其他服务,例如记录器或邮件程序吗?

标签: symfony pdf pdf-generation sonata


【解决方案1】:
use Knp\Snappy\Pdf;

use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;

/**
 * @Route("/show/pdf/{token}", name="admin_invoice_show_pdf", methods={"GET"})
 */

public function showPdf(Command $command, Pdf $pdf)
{

  $html = $this->renderView('admin/invoice/showPdf.html.twig',array('command' $command));
  return new PdfResponse($pdf->getOutputFromHtml($html), 'invoice.pdf');

}

【讨论】:

    猜你喜欢
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2016-12-29
    • 1970-01-01
    相关资源
    最近更新 更多