forked from barryvdh/laravel-snappy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a3ea5b2
Showing
13 changed files
with
442 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto | ||
|
||
# Custom for Visual Studio | ||
*.cs diff=csharp | ||
*.sln merge=union | ||
*.csproj merge=union | ||
*.vbproj merge=union | ||
*.fsproj merge=union | ||
*.dbproj merge=union | ||
|
||
# Standard to msysgit | ||
*.doc diff=astextplain | ||
*.DOC diff=astextplain | ||
*.docx diff=astextplain | ||
*.DOCX diff=astextplain | ||
*.dot diff=astextplain | ||
*.DOT diff=astextplain | ||
*.pdf diff=astextplain | ||
*.PDF diff=astextplain | ||
*.rtf diff=astextplain | ||
*.RTF diff=astextplain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
composer.phar | ||
composer.lock | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
|
||
before_script: | ||
- curl -s http://getcomposer.org/installer | php | ||
- php composer.phar install --dev | ||
|
||
script: phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "barryvdh/laravel-snappy", | ||
"description": "Snappy PDF/Image for Laravel 4", | ||
"keywords": ["laravel","snappy", "pdf", "image", "wkhtmltopdf", "wkhtmltoimage"], | ||
"authors": [ | ||
{ | ||
"name": "Barry vd. Heuvel", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.0", | ||
"illuminate/support": "4.x", | ||
"knplabs/knp-snappy": "*" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"Barryvdh\\Snappy": "src/" | ||
} | ||
}, | ||
"minimum-stability": "dev" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
## Snappy PDF/Image Wrapper for Laravel 4 | ||
|
||
You need to have wkhtmltopdf/wkhtmltoimage installed. See https://github.com/KnpLabs/snappy#wkhtmltopdf-binary-as-composer-dependencies | ||
This package is a ServiceProvider for Snappy: https://github.com/KnpLabs/snappy | ||
It provides $app['snappy.pdf'] and $app['snappy.image']. You have to set the binary location in the config file. | ||
For example, when loaded with composer: | ||
|
||
'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'), | ||
|
||
In addition to the Snappy classes, it provides a wrapper, similar to https://github.com/barryvdh/laravel-dompdf | ||
|
||
Require this package in your composer.json and update composer. | ||
|
||
"barryvdh/laravel-snappy": "dev-master" | ||
|
||
After updating composer, add the ServiceProvider to the providers array in app/config/app.php | ||
|
||
'Barryvdh\Snappy\ServiceProvider', | ||
|
||
You can optionally use the facade for shorter code. Add this to your facades: | ||
|
||
'PDF' => 'Barryvdh\Snappy\Facades\SnappyPdf', | ||
'Image' => 'Barryvdh\Snappy\Facades\SnappyImage', | ||
|
||
You can create a new Snappy PDF/Image instance and load a HTML string, file or view name. You can save it to a file, or stream (show in browser) or download. | ||
|
||
Using the App container: | ||
|
||
$snappy = App::make('snappy.pdf'); | ||
//To file | ||
$snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/tmp/bill-123.pdf'); | ||
$snappy->generate('http://www.github.com', '/tmp/github.pdf')); | ||
//Or output: | ||
return new Response( | ||
$snappy->getOutputFromHtml($html), | ||
200, | ||
array( | ||
'Content-Type' => 'application/pdf', | ||
'Content-Disposition' => 'attachment; filename="file.pdf"' | ||
) | ||
); | ||
|
||
Using the wrapper: | ||
|
||
$pdf = App::make('snappy.pdf.wrapper'); | ||
$pdf->loadHTML('<h1>Test</h1>'); | ||
return $pdf->stream(); | ||
|
||
Or use the facade: | ||
|
||
$pdf = PDF::loadView('pdf.invoice', $data); | ||
return $pdf->download('invoice.pdf'); | ||
|
||
You can chain the methods: | ||
|
||
return PDF::loadFile('http://www.github.com')->stream('github.pdf'); | ||
|
||
You can change the orientation and paper size | ||
|
||
PDF::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0)->save('myfile.pdf') | ||
|
||
If you need the output as a string, you can get the rendered PDF with the output() function, so you can save/output it yourself. | ||
|
||
You can publish the config-file to change some settings (default paper etc). | ||
|
||
php artisan config:publish barryvdh/laravel-snappy | ||
|
||
|
||
### License | ||
|
||
This Snappy Wrapper for Laravel4 is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
namespace Barryvdh\Snappy\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade as BaseFacade; | ||
|
||
class SnappyImage extends BaseFacade { | ||
|
||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() { return 'snappy.image'; } | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
namespace Barryvdh\Snappy\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade as BaseFacade; | ||
|
||
class SnappyPdf extends BaseFacade { | ||
|
||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() { return 'snappy.pdf.wrapper'; } | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
namespace Barryvdh\Snappy; | ||
|
||
/** | ||
* A Laravel wrapper for SnappyPDF | ||
* | ||
* @package laravel-snappy | ||
* @author Barry vd. Heuvel | ||
*/ | ||
class PdfWrapper{ | ||
|
||
/** @var \Knp\Snappy\Pdf */ | ||
protected $snappy; | ||
protected $rendered = false; | ||
|
||
protected $options; | ||
|
||
|
||
public function __construct($snappy){ | ||
$this->snappy = $snappy; | ||
$this->options = array(); | ||
} | ||
|
||
|
||
/** | ||
* Set the paper size (default A4) | ||
* | ||
* @param string $paper | ||
* @param string $orientation | ||
* @return $this | ||
*/ | ||
public function setPaper($paper, $orientation=null){ | ||
$this->snappy->setOption('page-size', $paper); | ||
if($orientation){ | ||
$this->snappy->setOption('orientation', $orientation); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set the orientation (default portrait) | ||
* | ||
* @param string $orientation | ||
* @return static | ||
*/ | ||
public function setOrientation($orientation){ | ||
$this->snappy->setOption('orientation', $orientation); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Show or hide warnings | ||
* | ||
* @param bool $warnings | ||
* @return $this | ||
* @deprecated | ||
*/ | ||
public function setWarnings($warnings){ | ||
//Doesn't do anything | ||
return $this; | ||
} | ||
|
||
public function setOption($name, $value){ | ||
$this->snappy->setOption($name, $value); | ||
return $this; | ||
} | ||
|
||
public function setOptions($options){ | ||
$this->snappy->setOptions($options); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Load a HTML string | ||
* | ||
* @param string $string | ||
* @return static | ||
*/ | ||
public function loadHTML($string){ | ||
$this->html = (string) $string; | ||
$this->file = null; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Load a HTML file | ||
* | ||
* @param string $file | ||
* @return static | ||
*/ | ||
public function loadFile($file){ | ||
$this->html = null; | ||
$this->file = $file; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Load a View and convert to HTML | ||
* | ||
* @param string $view | ||
* @param array $data | ||
* @param array $mergeData | ||
* @return static | ||
*/ | ||
public function loadView($view, $data = array(), $mergeData = array()){ | ||
$html = \View::make($view, $data, $mergeData); | ||
$this->html = (string) $html; | ||
$this->file = null; | ||
return $this; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Output the PDF as a string. | ||
* | ||
* @return string The rendered PDF as string | ||
*/ | ||
public function output(){ | ||
$output = ''; | ||
if($this->html){ | ||
$output = $this->snappy->getOutputFromHtml($this->html, $this->options); | ||
}elseif($this->file){ | ||
$output = $this->snappy->getOutput($this->file, $this->options); | ||
} | ||
return $output; | ||
} | ||
|
||
/** | ||
* Save the PDF to a file | ||
* | ||
* @param $filename | ||
* @return static | ||
*/ | ||
public function save($filename){ | ||
|
||
if($this->html){ | ||
$this->snappy->generateFromHtml($this->html, $filename, $this->options); | ||
}elseif($this->file){ | ||
$this->snappy->generate($this->file, $filename, $this->options); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Make the PDF downloadable by the user | ||
* | ||
* @param string $filename | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function download($filename = 'document.pdf' ){ | ||
$output = $this->output(); | ||
return \Response::make($output, 200, array( | ||
'Content-Type' => 'application/pdf', | ||
'Content-Disposition' => 'attachment; filename="'.$filename.'"' | ||
)); | ||
} | ||
|
||
/** | ||
* Return a response with the PDF to show in the browser | ||
* | ||
* @param string $filename | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function stream($filename = 'document.pdf' ){ | ||
$that = $this; | ||
return \Response::stream(function() use($that){ | ||
echo $that->output(); | ||
}, 200, array( | ||
'Content-Type' => 'application/pdf', | ||
'Content-Disposition' => 'inline; filename="'.$filename.'"', | ||
)); | ||
} | ||
|
||
public function __call($name, $arguments){ | ||
return call_user_func_array (array( $this->snappy, $name), $arguments); | ||
} | ||
|
||
} |
Oops, something went wrong.