您已请求不存在的服务“knp_snappy.pdf”

时间:2017-09-21 10:28:57

标签: symfony pdf pdf-generation sonata

我遵循了https://github.com/KnpLabs/KnpSnappyBundlehttps://ourcodeworld.com/articles/read/250/how-to-create-a-pdf-from-html-using-knpsnappybundle-wkhtmltopdf-in-symfony-3上的教程。

应用程序/配置/ 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: []

应用程序/ AppKernel

new Knp\Bundle\SnappyBundle\KnpSnappyBundle(),

myController的

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”。

1 个答案:

答案 0 :(得分: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');

}