Skip to content

Commit

Permalink
feat: add functionality for custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasravnsborg committed Dec 26, 2016
1 parent 8b58e05 commit 2dc7281
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
51 changes: 32 additions & 19 deletions src/LaravelPdf/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,50 @@
*/
class Pdf {

public function __construct($html = '')
protected $config = [];

public function __construct($html = '', $config = [])
{
$this->config = $config;

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->getConfig('mode'), // mode - default ''
$this->getConfig('format'), // format - A4, for example, default ''
$this->getConfig('default_font_size'), // font size - default 0
$this->getConfig('default_font'), // default font family
$this->getConfig('margin_left'), // margin_left
$this->getConfig('margin_right'), // margin right
$this->getConfig('margin_top'), // margin top
$this->getConfig('margin_bottom'), // margin bottom
$this->getConfig('margin_header'), // margin header
$this->getConfig('margin_footer'), // margin footer
$this->getConfig('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->SetTitle ( $this->getConfig('title') );
$this->mpdf->SetAuthor ( $this->getConfig('author') );
$this->mpdf->SetWatermarkText ( $this->getConfig('watermark') );
$this->mpdf->SetDisplayMode ( $this->getConfig('display_mode') );

$this->mpdf->showWatermarkText = $this->getConfig('show_watermark');
$this->mpdf->watermark_font = $this->getConfig('watermark_font');
$this->mpdf->watermarkTextAlpha = $this->getConfig('watermark_text_alpha');

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

protected function getConfig($key) {
if (isset($this->config[$key])) {
return $this->config[$key];
} else {
return Config::get('pdf.' . $key);
}
}

/**
* Encrypts and sets the PDF document permissions
*
Expand Down
12 changes: 6 additions & 6 deletions src/LaravelPdf/PdfWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class PdfWrapper {
* @param string $html
* @return Pdf
*/
public function loadHTML($html)
public function loadHTML($html, $config = [])
{
return new Pdf($html);
return new Pdf($html, $config);
}

/**
Expand All @@ -24,9 +24,9 @@ public function loadHTML($html)
* @param string $file
* @return Pdf
*/
public function loadFile($file)
public function loadFile($file, $config = [])
{
return new Pdf(File::get($file));
return new Pdf(File::get($file), $config);
}

/**
Expand All @@ -37,9 +37,9 @@ public function loadFile($file)
* @param array $mergeData
* @return Pdf
*/
public function loadView($view, $data = [], $mergeData = [])
public function loadView($view, $data = [], $mergeData = [], $config = [])
{
return new Pdf(View::make($view, $data, $mergeData)->render());
return new Pdf(View::make($view, $data, $mergeData)->render(), $config);
}

}

0 comments on commit 2dc7281

Please sign in to comment.