Skip to content

Latest commit

 

History

38 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MvlabsSnappy

Build Status Latest Stable Version Total Downloads License

MvlabsSnappy is a Laminas 3 module that allow easy to thumbnail, snapshot or PDF generation from a url or a html page using Snappy PHP (5.6+) wrapper for the wkhtmltopdf conversion utility.

Installation

With composer

php composer.phar require mvlabs/mvlabs-snappy

Post installation

  1. Enabling it in your application.config.phpfile.

    <?php
    return [
        'modules' => [
            // ...
            'MvlabsSnappy',
        ],
        // ...
    ];

Configuration

After installing MvlabsSnappy, copy ./vendor/mvlabs/MvlabsSnappy/config/mvlabs-snappy.local.php.dist to ./config/autoload/mvlabs-snappy.local.php and change the binaries path and add options as desired.

# /config/autoload/mvlabs-snappy.local.php
<?php
return [
    'mvlabs-snappy' => [
        'pdf' => [
           'binary'  => '/usr/local/bin/wkhtmltopdf',
           'options' => [], // Type wkhtmltopdf -H to see the list of options
        ],
        'image' => [
            'binary'  => '/usr/local/bin/wkhtmltoimage',
            'options' => [], // Type wkhtmltoimage -H to see the list of options
         ]
     ]
];

wkhtmltopdf binary as composer dependencies

If you want to download wkhtmltopdf with composer you add to composer.json:

{
    "require": {
        "h4cc/wkhtmltopdf-i386": "0.12.4"
    }
}

Or require the package for i386 with:

php composer.phar require h4cc/wkhtmltopdf-i386 "0.12.4"

If you are in 64 bit based system:

{
    "require": {
        "h4cc/wkhtmltopdf-amd64": "0.12.4"
    }
}

Or require the package for amd64 with:

php composer.phar require h4cc/wkhtmltopdf-amd64 "0.12.4"

The binary will then be located at:

vendor/h4cc/wkhtmltopdf-i386/bin/wkhtmltopdf-i386

Also a symlink will be created in your configured bin/ folder, for example:

vendor/bin/wkhtmltopdf-i386

Usage

The module registers two services:

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

Generate an image from an URL

 $mvlabsSnappyImage = $container->get('mvlabssnappy.image.service'),
 $mvlabsSnappyImage->generate('http://www.mvlabs.it', '/path/to/myapp/data/image.jpg');

Generate a pdf document from an URL

 $mvlabsSnappyPdf = $container->get('mvlabssnappy.pdf.service'),
 $mvlabsSnappyPdf->generate('http://www.mvlabs.it', '/path/to/myapp/data/document.pdf');

Render a pdf document as response from a controller

class IndexController extends AbstractActionController
{
    /**
     * @var Knp\Snappy\Pdf;
     */
    protected $mvlabsSnappyPdf;

    /**
     * @var Laminas\View\Renderer\RendererInterface
     */
    protected $renderer;

    public function __construct(Pdf $mvlabsSnappyPdf, RendererInterface $renderer)
    {
        $this->mvlabsSnappyPdf = $mvlabsSnappyPdf;
        $this->renderer = $renderer;
    }

    public function testPdfAction()
    {
        $now = new \DateTime();

        $layoutViewModel = $this->layout();
        $layoutViewModel->setTemplate('layout/pdf-layout');

        $viewModel = new ViewModel([
            'vars' => $vars,
        ]);

        $viewModel->setTemplate('myModule/myController/pdf-template');

        $layoutViewModel->setVariables([
            'content' => $this->renderer->render($viewModel),
        ]);

        $htmlOutput = $this->renderer->render($layoutViewModel);

        $output = $this->mvlabsSnappyPdf->getOutputFromHtml($htmlOutput);

        $response = $this->getResponse();
        $headers  = $response->getHeaders();
        $headers->addHeaderLine('Content-Type', 'application/pdf');
        $headers->addHeaderLine('Content-Disposition', "attachment; filename=\"export-" . $now->format('d-m-Y H:i:s') . ".pdf\"");

        $response->setContent($output);

        return $response;
    }
}

Credits

MvlabsSnappy and Snappy are based on the awesome wkhtmltopdf. MvlabsSnappy has been developed by mvlabs.

About

MvlabsSnappy is a ZF2/ZF3 module that allow easy to thumbnail, snapshot or PDF generation from a url or a html page using Snappy PHP (5.6+) wrapper for the wkhtmltopdf/wkhtmltoimage conversion utility.

Resources

Stars

24 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages