mvlabs/mvlabs-snappy

MvlabsSnappy is a Zend Framework and Laminas module that allow easy to thumbnail, snapshot or PDF generation from a url or a html page using Snappy PHP wrapper for the wkhtmltopdf/wkhtmltoimage conversion utility.

Installs: 52 037

Dependents: 0

Suggesters: 0

Security: 0

Stars: 24

Watchers: 13

Forks: 10

Open Issues: 1

Type:laminas-module

2.0.0 2020-08-31 09:53 UTC

This package is not auto-updated.

Last update: 2024-12-21 14:37:49 UTC


README

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.