Code Monkey home page Code Monkey logo

yii-snappy's Introduction

yii-snappy

This is a basic wrapper around Snappy implemented as Yii application component. Snappy is a PHP library that uses wkhtmltopdf and wkhtmltoimage to render HTML into PDF and various image formats.

Installation

Installation using Composer should be straightforward

% composer require dmitrivereshchagin/yii-snappy

To register components in your application add something like the following in configuration components section:

'pdf' => array(
    'class' => 'dmitrivereshchagin\\yii\\snappy\\PdfComponent',
    'binary' => '/usr/local/bin/wkhtmltopdf',
    'options' => array('orientation' => 'landscape'),
    'tempdir' => basename(__DIR__).'/runtime/pdf',
),
'image' => array(
    'class' => 'dmitrivereshchagin\\yii\\snappy\\ImageComponent',
    'binary' => '/usr/local/bin/wkhtmltoimage',
    'tempdir' => basename(__DIR__).'/runtime/image',
),

Specifying paths to binaries is mandatory. Optionally you can configure default command line arguments that can be overridden or adjusted later. It is also possible to set temporary directory for both components using snappyTempdir application parameter.

Usage

Use Yii::app()->pdf and Yii::app()->image to generate PDF documents and images respectively. These components provide access to methods declared by \Knp\Snappy\GeneratorInterface.

Here are some examples.

Generate image from an URL

Yii::app()->image->generate('http://example.com', '/path/to/image.jpg');

Generate PDF document from an URL

Yii::app()->pdf->generate('http://example.com', '/path/to/document.pdf');

Generate PDF document from multiple URLs

Yii::app()->pdf->generate(
    array('http://example.com', 'http://example.org'),
    '/path/to/document.pdf'
);

Generate PDF document from a view

Yii::app()->pdf->generateFromHtml(
    $this->render('view', array('name' => $value), $return = true),
    '/path/to/document.pdf'
);

Generate PDF document as response from controller

$html = $this->render('view', array('name' => $value), $return = true);

Yii::app()->request->sendFile(
    'document.pdf',
    Yii::app()->pdf->getOutputFromHtml($html)
);

Generate PDF document with relative URLs inside

$url = Yii::app()->createAbsoluteUrl('controller/action');

Yii::app()->request->sendFile(
    'document.pdf',
    Yii::app()->pdf->getOutput($url)
);

Credits

This code uses Snappy library. Snappy has been originally developed by KnpLabs team.

yii-snappy's People

Contributors

dmitrivereshchagin avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

yii-snappy's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.