Skip to content

Commit

Permalink
refactor: use separate PDF instances
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasravnsborg committed Dec 26, 2016
1 parent b5238f8 commit 8b58e05
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 175 deletions.
137 changes: 137 additions & 0 deletions src/LaravelPdf/Pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace niklasravnsborg\LaravelPdf;

use Config;
use mPDF;

/**
* Laravel PDF: mPDF wrapper for Laravel 5
*
* @package laravel-pdf
* @author Niklas Ravnsborg-Gjertsen
*/
class Pdf {

public function __construct($html = '')
{
if (Config::has('pdf.custom_font_path') && Config::has('pdf.custom_font_data')) {
define(_MPDF_SYSTEM_TTFONTS_CONFIG, __DIR__ . '/../mpdf_ttfonts_config.php');
}

$this->mpdf = new mPDF(
Config::get('pdf.mode'), // mode - default ''
Config::get('pdf.format'), // format - A4, for example, default ''
Config::get('pdf.default_font_size'), // font size - default 0
Config::get('pdf.default_font'), // default font family
Config::get('pdf.margin_left'), // margin_left
Config::get('pdf.margin_right'), // margin right
Config::get('pdf.margin_top'), // margin top
Config::get('pdf.margin_bottom'), // margin bottom
Config::get('pdf.margin_header'), // margin header
Config::get('pdf.margin_footer'), // margin footer
Config::get('pdf.orientation') // L - landscape, P - portrait
);

$this->mpdf->SetTitle(Config::get('pdf.title'));
$this->mpdf->SetAuthor(Config::get('pdf.author'));
$this->mpdf->SetWatermarkText(Config::get('pdf.watermark'));
$this->mpdf->SetDisplayMode(Config::get('pdf.display_mode'));
$this->mpdf->showWatermarkText = Config::get('pdf.show_watermark');
$this->mpdf->watermark_font = Config::get('pdf.watermark_font');
$this->mpdf->watermarkTextAlpha = Config::get('pdf.watermark_text_alpha');

$this->mpdf->WriteHTML($html);
}

/**
* Encrypts and sets the PDF document permissions
*
* @param array $permisson Permissons e.g.: ['copy', 'print']
* @param string $userPassword User password
* @param string $ownerPassword Owner password
* @return static
*
*/
public function setProtection($permisson, $userPassword = '', $ownerPassword = '')
{
if (func_get_args()[2] === NULL) {
$ownerPassword = bin2hex(openssl_random_pseudo_bytes(8));
};
return $this->mpdf->SetProtection($permisson, $userPassword, $ownerPassword);
}

/**
* Sets the watermark image for the PDF
*
* @param string $src Image file
* @param string $alpha Transparency of the image
* @param integer or array $size Defines the size of the watermark.
* @param array $position Array of $x and $y defines the position of the watermark.
* @return static
*
*/
public function setWatermarkImage($src, $alpha = 0.2, $size = 'D', $position = 'P')
{
$this->mpdf->showWatermarkImage = true;
return $this->mpdf->SetWatermarkImage($src);
}

/**
* Sets a watermark text for the PDF
*
* @param string $text Text for watermark
* @param string $alpha Transparency of the text
* @return static
*
*/
public function setWatermarkText($text, $alpha = 0.2)
{
$this->mpdf->showWatermarkText = true;
return $this->mpdf->SetWatermarkText($text);
}


/**
* Output the PDF as a string.
*
* @return string The rendered PDF as string
*/
public function output()
{
return $this->mpdf->Output('', 'S');
}

/**
* Save the PDF to a file
*
* @param $filename
* @return static
*/
public function save($filename)
{
return $this->mpdf->Output($filename, 'F');
}

/**
* Make the PDF downloadable by the user
*
* @param string $filename
* @return \Symfony\Component\HttpFoundation\Response
*/
public function download($filename = 'document.pdf')
{
return $this->mpdf->Output($filename, 'D');
}

/**
* 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')
{
return $this->mpdf->Output($filename, 'I');
}
}
37 changes: 5 additions & 32 deletions src/LaravelPdf/PdfServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

include base_path('vendor/mpdf/mpdf/mpdf.php');

class PdfServiceProvider extends BaseServiceProvider {

/**
Expand All @@ -21,40 +19,14 @@ class PdfServiceProvider extends BaseServiceProvider {
*
* @return void
*/
public function register() {
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/../config/pdf.php', 'pdf'
);

$this->app->bind('mpdf.wrapper', function($app, $cfg) {

if (Config::has('pdf.custom_font_path') && Config::has('pdf.custom_font_data')) {
define(_MPDF_SYSTEM_TTFONTS_CONFIG, __DIR__ . '/../mpdf_ttfonts_config.php');
}

$mpdf = new \mPDF(
Config::get('pdf.mode'), // mode - default ''
Config::get('pdf.format'), // format - A4, for example, default ''
Config::get('pdf.default_font_size'), // font size - default 0
Config::get('pdf.default_font'), // default font family
Config::get('pdf.margin_left'), // margin_left
Config::get('pdf.margin_right'), // margin right
Config::get('pdf.margin_top'), // margin top
Config::get('pdf.margin_bottom'), // margin bottom
Config::get('pdf.margin_header'), // margin header
Config::get('pdf.margin_footer'), // margin footer
Config::get('pdf.orientation') // L - landscape, P - portrait
);

$mpdf->SetTitle(Config::get('pdf.title'));
$mpdf->SetAuthor(Config::get('pdf.author'));
$mpdf->SetWatermarkText(Config::get('pdf.watermark'));
$mpdf->SetDisplayMode(Config::get('pdf.display_mode'));
$mpdf->showWatermarkText = Config::get('pdf.show_watermark');
$mpdf->watermark_font = Config::get('pdf.watermark_font');
$mpdf->watermarkTextAlpha = Config::get('pdf.watermark_text_alpha');

return new PdfWrapper($mpdf);
return new PdfWrapper();
});
}

Expand All @@ -63,7 +35,8 @@ public function register() {
*
* @return array
*/
public function provides() {
public function provides()
{
return array('mpdf.pdf');
}

Expand Down
159 changes: 16 additions & 143 deletions src/LaravelPdf/PdfWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,31 @@

namespace niklasravnsborg\LaravelPdf;

/**
* Laravel PDF: mPDF wrapper for Laravel 5
*
* @package laravel-pdf
* @author Niklas Ravnsborg-Gjertsen
*/
class PdfWrapper {

protected $mpdf;
protected $rendered = false;
protected $options;
use File;
use View;

public function __construct($mpdf) {
$this->mpdf = $mpdf;
$this->options = array();
}
class PdfWrapper {

/**
* Load a HTML string
*
* @param string $string
* @return static
* @param string $html
* @return Pdf
*/
public function loadHTML($string, $mode = 0) {
$this->mpdf->WriteHTML((string) $string, $mode);
$this->html = null;
$this->file = null;
return $this;
public function loadHTML($html)
{
return new Pdf($html);
}

/**
* Load a HTML file
*
* @param string $file
* @return static
* @return Pdf
*/
public function loadFile($file) {
$this->html = null;
$this->file = $file;
return $this;
public function loadFile($file)
{
return new Pdf(File::get($file));
}

/**
Expand All @@ -50,123 +35,11 @@ public function loadFile($file) {
* @param string $view
* @param array $data
* @param array $mergeData
* @return static
*/
public function loadView($view, $data = array(), $mergeData = array()) {
$this->html = \View::make($view, $data, $mergeData)->render();
$this->file = null;
return $this;
}

/**
* Output the PDF as a string.
*
* @return string The rendered PDF as string
*/
public function output() {

if($this->html) {
$this->mpdf->WriteHTML($this->html);
} elseif($this->file) {
$this->mpdf->WriteHTML($this->file);
}

return $this->mpdf->Output('', 'S');
}

/**
* Save the PDF to a file
*
* @param $filename
* @return static
* @return Pdf
*/
public function save($filename) {

if($this->html) {
$this->mpdf->WriteHTML($this->html);
} elseif($this->file) {
$this->mpdf->WriteHTML($this->file);
}

return $this->mpdf->Output($filename, 'F');
}

/**
* Make the PDF downloadable by the user
*
* @param string $filename
* @return \Symfony\Component\HttpFoundation\Response
*/
public function download($filename = 'document.pdf') {

if ($this->html) {
$this->mpdf->WriteHTML($this->html);
} elseif ($this->file) {
$this->mpdf->WriteHTML($this->file);
}

return $this->mpdf->Output($filename, 'D');
}

/**
* 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' ){
if ($this->html) {
$this->mpdf->WriteHTML($this->html);
} elseif($this->file) {
$this->mpdf->WriteHTML($this->file);
}

return $this->mpdf->Output($filename, 'I');
}

/**
* Encrypts and sets the PDF document permissions
*
* @param array $permisson Permissons e.g.: ['copy', 'print']
* @param string $userPassword User password
* @param string $ownerPassword Owner password
*
*/
public function setProtection($permisson, $userPassword = '', $ownerPassword = '') {
if (func_get_args()[2] === NULL) {
$ownerPassword = bin2hex(openssl_random_pseudo_bytes(8));
};
return $this->mpdf->SetProtection($permisson, $userPassword, $ownerPassword);
}

/**
* Sets the watermark image for the PDF
*
* @param string $src Image file
* @param string $alpha Transparency of the image
* @param integer or array $size Defines the size of the watermark.
* @param array $position Array of $x and $y defines the position of the watermark.
*
*/
public function setWatermarkImage($src, $alpha = 0.2, $size = 'D', $position = 'P') {
$this->mpdf->showWatermarkImage = true;
return $this->mpdf->SetWatermarkImage($src);
}

/**
* Sets a watermark text for the PDF
*
* @param string $text Text for watermark
* @param string $alpha Transparency of the text
*
*/
public function setWatermarkText($text, $alpha = 0.2) {
$this->mpdf->showWatermarkText = true;
return $this->mpdf->SetWatermarkText($text);
}

public function __call($name, $arguments){
return call_user_func_array(array($this->mpdf, $name), $arguments);
public function loadView($view, $data = [], $mergeData = [])
{
return new Pdf(View::make($view, $data, $mergeData)->render());
}

}

0 comments on commit 8b58e05

Please sign in to comment.