2017 © Pedro Peláez
 

silverstripe-module silverstripe-wkhtml

Provides Wkhtml functionality in SilverStripe

image

heyday/silverstripe-wkhtml

Provides Wkhtml functionality in SilverStripe

  • Monday, November 6, 2017
  • by heyday
  • Repository
  • 24 Watchers
  • 13 Stars
  • 16,747 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 1 Open issues
  • 12 Versions
  • 4 % Grown

The README.md

Silverstripe Wkhtml - by Heyday

This module provides a SilverStripe-centric wrapper for Snappy and wkhtml., (*1)

Requirements

Installation

$ composer require "heyday/silverstripe-wkhtml"

How to use

Four things are required to generate a pdf or an image:, (*2)

  • Knp\Snappy\GeneratorInterface The wrapper for wkhtmltopdf or wkhtmltoimage
  • Heyday\SilverStripe\WkHtml\Input\InputInterface to provide the html
  • Heyday\SilverStripe\WkHtml\Output\OutputInterface output the pdf or image in different ways
  • Heyday\SilverStripe\WkHtml\Generator to glue everything together

Available Inputs

  • Request (generates content from a request)
  • TextString (content is specified by a string)
  • Template (generates content from a SilverStripe template)
  • Url (generates content from a GET request to a Url)
  • Viewer (generates content from an SSViewer instance)

Available Outputs

  • Browser (outputs to the browser)
  • File (outputs to a file)
  • RandomFile (outputs to a random filename)
  • TextString (outputs to a string)

Available Generators

  • Pdf
  • Image

Examples

Full example (from a controller action)

SilverStripe\Core\Injector\Injector:
  # Create PDF generator as an injector service
  # This allows you to specify the binary path once and have it set up
  # automatically by getting the service from the injector.
  Knp\Snappy\Pdf:
    constructor:
      - '/bin/wkhtmltopdf' # Path to your WKTHMLTOPDF binary. Use '`SOME_ENV_VAR`' to define the binary path in .env
use Heyday\SilverStripe\WkHtml;
use SilverStripe\Core\Injector\Injector;

$generator = WkHtml\Generator::create(
    // Use Injector->get(Pdf::class) if you don't need to modify options
    // Use Injector->create() to create a transient service for modifications (e.g. setOption)
    // Using Injector->get() and making changes will cause changes to be made for all uses of get(Pdf::class) for the entire request
    Injector::inst()->create(\Knp\Snappy\Pdf::class),
    WkHtml\Input\Url::create('/'),
    WkHtml\Output\Browser::create('test.pdf', 'application/pdf')
);
return $generator->process();

Inputs

Request

\Heyday\SilverStripe\WkHtml\Input\Request::create(
    // Controller::curr()->getRequest() is also an option
    Injector::inst()->get(\SilverStripe\Control\HTTPRequest::class)
);
\Heyday\SilverStripe\WkHtml\Input\Request::create(
    Injector::inst()->get(\SilverStripe\Control\HTTPRequest::class),
    new Session([
        'arg' => 'value',
    ])
);

String

$html = <<<HTML
<h1>Title</h1>
HTML;
\Heyday\SilverStripe\WkHtml\Input\TextString::create($html);

Template

\Heyday\SilverStripe\WkHtml\Input\Template::create('MyTemplate');

\Heyday\SilverStripe\WkHtml\Input\Template::create(
    'MyTemplate',
    [
        'Var' => 'Hello',
    ]
);

\Heyday\SilverStripe\WkHtml\Input\Template::create(
    'MyTemplate',
    ArrayData::create([
        'Var' => 'Hello',
    ])
);

\Heyday\SilverStripe\WkHtml\Input\Template::create(
    '$Var World',
    ArrayData::create([
        'Var' => 'Hello',
    ]),
    true
);

Viewer

\Heyday\SilverStripe\WkHtml\Input\Viewer::create(
    SSViewer::create([
        'Template',
    ]),
    ArrayData::create([
        'Var' => 'Hello',
    ])
);

Url

\Heyday\SilverStripe\WkHtml\Input\Url::create('/');

\Heyday\SilverStripe\WkHtml\Input\Url::create('http://google.co.nz/');

Outputs

Browser

\Heyday\SilverStripe\WkHtml\Output\Browser::create('test.pdf', 'application/pdf'); // Force download

\Heyday\SilverStripe\WkHtml\Output\Browser::create('test.pdf', 'application/pdf', true); // Embeds

File

\Heyday\SilverStripe\WkHtml\Output\File::create(BASE_PATH . '/test.pdf');

\Heyday\SilverStripe\WkHtml\Output\File::create(BASE_PATH . '/test.pdf', true); // Overwrite

Random File

\Heyday\SilverStripe\WkHtml\Output\RandomFile::create(BASE_PATH);

String

\Heyday\SilverStripe\WkHtml\Output\TextString::create();

Unit Testing

$ composer install
$ phpunit

License

This project is licensed under an MIT license, (*3)

The Versions

06/11 2017

dev-ss4-upgrade

dev-ss4-upgrade

Provides Wkhtml functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

The Development Requires

13/07 2017

dev-master

9999999-dev

Provides Wkhtml functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

The Development Requires

13/07 2017

1.1.1

1.1.1.0

Provides Wkhtml functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

The Development Requires

12/06 2017

dev-php7

dev-php7

Provides Wkhtml functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

The Development Requires

12/06 2017

1.1.0

1.1.0.0

Provides Wkhtml functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

The Development Requires

04/09 2014

1.0.6

1.0.6.0

Provides Wkhtml functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

The Development Requires

07/04 2014

1.0.5

1.0.5.0

Provides Wkhtml functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

The Development Requires

17/06 2013

1.0.x-dev

1.0.9999999.9999999-dev

Provides Wkhtml functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

The Development Requires

26/04 2013

1.0.4

1.0.4.0

Provides Wkhtmltopdf functionality in SilverStripe

  Sources   Download

MIT

The Requires

 

11/04 2013

1.0.3

1.0.3.0

  Sources   Download

The Requires

 

01/11 2012

1.0.2

1.0.2.0

  Sources   Download

The Requires

 

01/11 2012

1.0.1

1.0.1.0

  Sources   Download

The Requires