git 使用 knp snappy bundle 生成 pdf - symfony2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22035808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:46:18  来源:igfitidea点击:

Using knp snappy bundle to generate pdf - symfony2

phpgitsymfonypdfwkhtmltopdf

提问by AtanuCSE

I'm new to symfony2 and very to use external libraries. I want to use the KNP Snappy Bundle, my first third party bundle.

我是 symfony2 的新手,非常喜欢使用外部库。我想使用KNP Snappy Bundle,我的第一个第三方包。

I did exactly that is told in the git link.

我完全按照 git 链接中的说明做了。

{
    "require": {
        "knplabs/knp-snappy-bundle": "dev-master"
    }
}

// app/AppKernel.php

// 应用程序/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        //...
        new Knp\Bundle\SnappyBundle\KnpSnappyBundle(),

app/config/config.yml

应用程序/配置/config.yml

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

Then I added following line in ACMEwelcome controller to test

然后我在ACME欢迎控制器中添加了以下行进行测试

$this->get('knp_snappy.pdf')->generate('http://www.google.fr', '/Symfony/file.pdf');

It says The exit status code '1' says something went wrong: stderr: "The system cannot find the path specified. " stdout: "" command: /usr/local/bin/wkhtmltoimage --format "jpg" "http://www.google.fr" "/Symfony/file.pdf".

它说 The exit status code '1' says something went wrong: stderr: "The system cannot find the path specified. " stdout: "" command: /usr/local/bin/wkhtmltoimage --format "jpg" "http://www.google.fr" "/Symfony/file.pdf".

I tried

我试过

$this->get('knp_snappy.pdf')->generateFromHtml(
    $this->renderView(
        'AcmeDemoBundle:Welcome:index.html.twig'),
        '/Symfony/file.pdf'
    );

It shows The exit status code '1' says something went wrong: stderr: "The system cannot find the path specified. " stdout: "" command: /usr/local/bin/wkhtmltopdf --lowquality "C:\Windows\TEMP\knp_snappy530da4525584b8.92211088.html" "/Symfony/file.pdf".

表明 The exit status code '1' says something went wrong: stderr: "The system cannot find the path specified. " stdout: "" command: /usr/local/bin/wkhtmltopdf --lowquality "C:\Windows\TEMP\knp_snappy530da4525584b8.92211088.html" "/Symfony/file.pdf".

What I'm missing? Do I need to install anything ? Please describe how can I run it and generate a proper pdf????? I searched, I'm guessing I need to install wkhtmltoimage etc. But from where and how?

我缺少什么?我需要安装什么吗?请描述我如何运行它并生成正确的 pdf????我搜索过,我猜我需要安装 wkhtmltoimage 等。但是从哪里以及如何安装?

回答by thesearentthedroids

you can also manage wkhtmltopdf with composer too, I did it in a recent project:

你也可以用 composer 管理 wkhtmltopdf,我在最近的一个项目中做到了:

in your composer.json you can add:

在您的 composer.json 中,您可以添加:

"h4cc/wkhtmltopdf-amd64": "0.11.0-RC1"

and in your config.yml:

并在您的 config.yml 中:

binary:     %kernel.root_dir%/../vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64

回答by Ivan Bajalovic

Few months later, but here is what helped me.

几个月后,但这对我有帮助。

I put the path to wkthmltopdf folder in escaped double quotes.

我将 wkthmltopdf 文件夹的路径放在转义的双引号中。

knp_snappy:
   pdf:
      binary: "\"C:/Program Files (x86)/wkhtmltopdf/wkhtmltopdf.exe\""

回答by Ahmed Samy

You can do one of these

你可以做其中之一

1- Update your config.yml

1- 更新您的 config.yml

 pdf:
    enabled:    true
    binary:     wkhtmltopdf
    options:    []
  • And update your windows environment PATH with path/to/my/wkhtmltopdf
  • 并使用以下命令更新您的 Windows 环境 PATH path/to/my/wkhtmltopdf

2- Or set the path directly in config.yml

2- 或者直接在 config.yml 中设置路径

pdf:
        enabled:    true
        binary:     /path/to/my/wkhtmltopdf
        options:    []