Skip to content

Commit

Permalink
Merge pull request #6 from hchokshi/feature/upgrade-to-ss4
Browse files Browse the repository at this point in the history
Upgrade to SS4
  • Loading branch information
stevie-mayhew authored Jan 9, 2019
2 parents 2bbf99f + 3e7a1c8 commit 4d09762
Show file tree
Hide file tree
Showing 40 changed files with 635 additions and 1,318 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
language: php

php:
- 5.3
- 5.4
- 5.6
- 7.0
- 7.1
- 7.2

before_script:
- composer self-update
- composer install --dev --prefer-dist
- composer install --prefer-dist
138 changes: 65 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This module provides a SilverStripe-centric wrapper for [Snappy](https://github.com/KnpLabs/snappy) and [wkhtml](http://code.google.com/p/wkhtmltopdf/).

A SilverStripe `2.4` version is available in the `1.0` branch.
A `SilverStripe 3` version is available as `^2.x`, and a SilverStripe `2.4` version is available as `^1.x`.

## Requirements

Expand All @@ -13,7 +13,7 @@ A SilverStripe `2.4` version is available in the `1.0` branch.

## Installation

$ composer require "heyday/silverstripe-wkhtml:~1.0.6"
$ composer require "heyday/silverstripe-wkhtml:^3"

## How to use

Expand All @@ -30,7 +30,7 @@ Four things are required to generate a pdf or an image:
- 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 contetn from an SSViewer instance)
- Viewer (generates content from an SSViewer instance)

### Available Outputs

Expand All @@ -48,12 +48,27 @@ Four things are required to generate a pdf or an image:

### Full example (from a controller action)

```yaml
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
```
```php
use Heyday\SilverStripe\WkHtml;
$generator = new WkHtml\Generator(
new \Knp\Snappy\Pdf('/pathto/wkhtmltopdf'),
new WkHtml\Input\Url('/'),
new WkHtml\Output\Browser('test.pdf', 'application/pdf')
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();
```
Expand All @@ -63,137 +78,114 @@ return $generator->process();
#### Request

```php
new \Heyday\SilverStripe\WkHtml\Input\Request(
new SS_HTTPRequest('GET', '/')
\Heyday\SilverStripe\WkHtml\Input\Request::create(
// Controller::curr()->getRequest() is also an option
Injector::inst()->get(\SilverStripe\Control\HTTPRequest::class)
);
```

```php
new \Heyday\SilverStripe\WkHtml\Input\Request(
new SS_HTTPRequest('GET', '/'),
new Session(
array(
'arg' => 'value'
)
)
\Heyday\SilverStripe\WkHtml\Input\Request::create(
Injector::inst()->get(\SilverStripe\Control\HTTPRequest::class),
new Session([
'arg' => 'value',
])
);
```

#### String

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

#### Template

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

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

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

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

#### Viewer

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

#### Url

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

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

### Outputs

#### Browser

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

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

#### File

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

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

#### Random File

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

#### String

```php
new \Heyday\SilverStripe\WkHtml\Output\TextString();
\Heyday\SilverStripe\WkHtml\Output\TextString::create();
```

##Unit Testing

$ composer install --dev
$ phpunit
```bash
$ composer install
$ phpunit
```

## License

Expand Down
9 changes: 9 additions & 0 deletions _config/cache.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
Name: wkhtmlcache
---

SilverStripe\Core\Injector\Injector:
Psr\SimpleCache\CacheInterface.wkhtml:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: wkhtml
29 changes: 18 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
{
"name": "heyday/silverstripe-wkhtml",
"description": "Provides Wkhtml functionality in SilverStripe",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"license": "MIT",
"autoload": {
"psr-0": {
"Heyday\\SilverStripe\\WkHtml": "src/"
"psr-4": {
"Heyday\\SilverStripe\\WkHtml\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Heyday\\SilverStripe\\WkHtml\\": "tests/"
}
},
"require": {
"composer/installers": "~1.0",
"knplabs/knp-snappy": "~0.2.0",
"symfony/process": "~2.2",
"maximebf/cachecache": "~1.0"
"php": ">=5.6",
"knplabs/knp-snappy": "^1",
"silverstripe/framework": "^4"
},
"require-dev": {
"phpunit/phpunit": "~3.7",
"symfony/class-loader": "~2.2",
"silverstripe/framework": "3.1.*@dev",
"mikey179/vfsStream": "dev-master"
"phpunit/phpunit": "^5.7",
"mikey179/vfsStream": "^1"
},
"extra": {
"branch-alias": {
"dev-master": "1.1.x-dev"
}
},
"config": {
"preferred-install": {
"silverstripe/framework": "source"
}
}
}
Loading

0 comments on commit 4d09762

Please sign in to comment.