Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Easily create PDF and images in Symfony by converting html using webkit

License

NotificationsYou must be signed in to change notification settings

KnpLabs/KnpSnappyBundle

Repository files navigation

Build StatusScrutinizer Code QualityStyleCI

Snappy is a PHP wrapper for thewkhtmltopdf conversion utility.It allows you to generate either pdf or image files from your html documents, using the webkit engine.

The KnpSnappyBundle provides a simple integration for your Symfony project.

Limitations

If you use JavaScript to render your pages, you may encounter some issues because ofwkhtmltopdf not being fully compatible with ES6 apis.The only way to solve this issue is to provide polyfills that fix the gaps between modern ES6 apis and thewkhtmltopdf rendering engine.

Installation

Withcomposer, require:

composer require knplabs/knp-snappy-bundle

If you are not using Flex, enable it in your kernel :

// config/bundles.php<?phpreturn [//...Knp\Bundle\SnappyBundle\KnpSnappyBundle::class => ['all' =>true],//...];

Configuration

If you need to change the binaries, change the instance options or even disable one or both services, you can do it through the configuration.

# config/packages/knp_snappy.yamlknp_snappy:pdf:enabled:truebinary:/usr/local/bin/wkhtmltopdf#"\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\"" for Windows usersoptions:[]image:enabled:truebinary:/usr/local/bin/wkhtmltoimage#"\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\"" for Windows usersoptions:[]

If you want to change temporary folder which issys_get_temp_dir() by default, you can use

# config/packages/knp_snappy.yamlknp_snappy:temporary_folder:"%kernel.cache_dir%/snappy"

You can also configure the timeout used by the generators withprocess_timeout:

# config/packages/knp_snappy.yamlknp_snappy:process_timeout:20# In seconds

Usage

The bundle registers two services:

  • theknp_snappy.image service allows you to generate images;
  • theknp_snappy.pdf service allows you to generate pdf files.

Generate an image from a URL

// @var Knp\Snappy\Image$knpSnappyImage->generate('http://www.google.fr','/path/to/the/image.jpg');

Generate a pdf document from a URL

// @var \Knp\Snappy\Pdf$knpSnappyPdf->generate('http://www.google.fr','/path/to/the/file.pdf');

Generate a pdf document from multiple URLs

// @var \Knp\Snappy\Pdf$knpSnappyPdf->generate(array('http://www.google.fr','http://www.knplabs.com','http://www.google.com'),'/path/to/the/file.pdf');

Generate a pdf document from a twig view

// @var \Knp\Snappy\Pdf$knpSnappyPdf->generateFromHtml($this->renderView('MyBundle:Foo:bar.html.twig',array('some'  =>$vars        )    ),'/path/to/the/file.pdf');

Render an image as response from a controller

useKnp\Bundle\SnappyBundle\Snappy\Response\JpegResponse;useSymfony\Bundle\FrameworkBundle\Controller\AbstractController;class SomeControllerextends AbstractController{publicfunctionimageAction(\Knp\Snappy\Image$knpSnappyImage)    {$html =$this->renderView('MyBundle:Foo:bar.html.twig',array('some'  =>$vars        ));returnnewJpegResponse($knpSnappyImage->getOutputFromHtml($html),'image.jpg'        );    }}

Render a pdf document as response from a controller

useKnp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;useSymfony\Bundle\FrameworkBundle\Controller\AbstractController;class SomeControllerextends AbstractController{publicfunctionpdfAction(\Knp\Snappy\Pdf$knpSnappyPdf)    {$html =$this->renderView('MyBundle:Foo:bar.html.twig',array('some'  =>$vars        ));returnnewPdfResponse($knpSnappyPdf->getOutputFromHtml($html),'file.pdf'        );    }}

Render a pdf document with a relative url inside like css files

useKnp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;useSymfony\Bundle\FrameworkBundle\Controller\AbstractController;class SomeControllerextends AbstractController{publicfunctionpdfAction(\Knp\Snappy\Pdf$knpSnappyPdf)    {$pageUrl =$this->generateUrl('homepage',array(),true);// use absolute path!returnnewPdfResponse($knpSnappyPdf->getOutput($pageUrl),'file.pdf'        );    }}

Maintainers

KNPLabs is looking for maintainers (see why).

If you are interested, feel free to open a PR to ask to be added as a maintainer.

We’ll be glad to hear from you :)

Credits

SnappyBundle andSnappy are based on the awesomewkhtmltopdf.SnappyBundle has been developed byKnpLabs.


[8]ページ先頭

©2009-2025 Movatter.jp