Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
websolutionstuff committed Dec 25, 2022
1 parent 0516e39 commit 3dee77b
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
composer.lock
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "websolutionstuff/pdf-laravel",
"description": "Websolutionstuff PDF support for Laravel 6, Laravel 7, Laravel 8, Laravel 9",
"minimum-stability": "stable",
"license": "MIT",
"keywords": [
"laravel",
"websolutionstuff",
"pdf"
],
"authors": [
{
"name": "websolutionstuff",
"email": "[email protected]"
}
],
"require": {
"illuminate/support": "^6.0|^7.0|^8.0|^9.0",
"tecnickcom/tcpdf": "6.2.*|6.3.*|6.4.*|6.5.*|6.6.*|dev-main"
},
"autoload": {
"psr-4": {
"Websolutionstuff\\PDF\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Websolutionstuff\\PDF\\ServiceProvider"
],
"aliases": {
"PDF": "Websolutionstuff\\PDF\\Facades\\PDF"
}
}
}
}
18 changes: 18 additions & 0 deletions config/pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
return [
'page_format' => 'A4',
'page_orientation' => 'P',
'page_units' => 'mm',
'unicode' => true,
'encoding' => 'UTF-8',
'font_directory' => '',
'image_directory' => '',
'tcpdf_throw_exception' => false,
'use_fpdi' => false,
'use_original_header' => false,
'use_original_footer' => false,
'pdfa' => false,

// See more info at the tcpdf_config.php file in TCPDF (if you do not set this here, TCPDF will use it default)
// https://raw.githubusercontent.com/tecnickcom/TCPDF/master/config/tcpdf_config.php
];
11 changes: 11 additions & 0 deletions src/Facades/PDF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Websolutionstuff\PDF\Facades;

use Illuminate\Support\Facades\Facade;

class PDF extends Facade
{
protected static function getFacadeAccessor(){
return 'pdf';
}
}
48 changes: 48 additions & 0 deletions src/FpdiPDFHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Websolutionstuff\PDF;

use Illuminate\Support\Facades\Config;
use setasign\Fpdi\TcpdfFpdi;

class FpdiPDFHelper extends TcpdfFpdi
{
protected $headerCallback;

protected $footerCallback;

public function Header()
{
if ($this->headerCallback != null && is_callable($this->headerCallback)) {
$cb = $this->headerCallback;
$cb($this);
} else {
if (Config::get('pdf.use_original_header')) {
parent::Header();
}
}
}

public function Footer()
{
if ($this->footerCallback != null && is_callable($this->footerCallback)) {
$cb = $this->footerCallback;
$cb($this);
} else {
if (Config::get('pdf.use_original_footer')) {
parent::Footer();
}
}
}

public function setHeaderCallback($callback)
{
$this->headerCallback = $callback;
}

public function setFooterCallback($callback)
{
$this->footerCallback = $callback;
}

}
58 changes: 58 additions & 0 deletions src/PDF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Websolutionstuff\PDF;

use Illuminate\Support\Facades\Config;

class PDF
{
protected static $format;

protected $app;
/** @var PDFHelper */
protected $pdf;

public function __construct($app)
{
$this->app = $app;
$this->reset();
}

public function reset()
{
$class = Config::get('pdf.use_fpdi') ? FpdiPDFHelper::class : PDFHelper::class;

$this->pdf = new $class(
Config::get('pdf.page_orientation', 'P'),
Config::get('pdf.page_units', 'mm'),
static::$format ? static::$format : Config::get('pdf.page_format', 'A4'),
Config::get('pdf.unicode', true),
Config::get('pdf.encoding', 'UTF-8'),
false, // Diskcache is deprecated
Config::get('pdf.pdfa', false)
);
}

public static function changeFormat($format)
{
static::$format = $format;
}

public function __call($method, $args)
{
if (method_exists($this->pdf, $method)) {
return call_user_func_array([$this->pdf, $method], $args);
}
throw new \RuntimeException(sprintf('the method %s does not exists in PDF', $method));
}

public function setHeaderCallback($headerCallback)
{
$this->pdf->setHeaderCallback($headerCallback);
}

public function setFooterCallback($footerCallback)
{
$this->pdf->setFooterCallback($footerCallback);
}
}
72 changes: 72 additions & 0 deletions src/PDFHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Websolutionstuff\PDF;

use Illuminate\Support\Facades\Config;

class PDFHelper extends \TCPDF
{
protected $headerCallback;

protected $footerCallback;

public function Header()
{
if ($this->headerCallback != null && is_callable($this->headerCallback)) {
$cb = $this->headerCallback;
$cb($this);
} else {
if (Config::get('pdf.use_original_header')) {
parent::Header();
}
}
}

public function Footer()
{
if ($this->footerCallback != null && is_callable($this->footerCallback)) {
$cb = $this->footerCallback;
$cb($this);
} else {
if (Config::get('pdf.use_original_footer')) {
parent::Footer();
}
}
}

public function setHeaderCallback($callback)
{
$this->headerCallback = $callback;
}

public function setFooterCallback($callback)
{
$this->footerCallback = $callback;
}

public function addTOC($page = '', $numbersfont = '', $filler = '.', $toc_name = 'TOC', $style = '', $color = array(0, 0, 0))
{
// sort bookmarks before generating the TOC
parent::sortBookmarks();

parent::addTOC($page, $numbersfont, $filler, $toc_name, $style, $color);
}

public function addHTMLTOC($page = '', $toc_name = 'TOC', $templates = array(), $correct_align = true, $style = '', $color = array(0, 0, 0))
{
// sort bookmarks before generating the TOC
parent::sortBookmarks();

parent::addHTMLTOC($page, $toc_name, $templates, $correct_align, $style, $color);
}

public function checkPageBreak($h = 0, $y = null, $addpage = true)
{
parent::checkPageBreak($h, $y, $addpage);
}

public function getPageBreakTrigger()
{
return $this->PageBreakTrigger;
}
}
94 changes: 94 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Websolutionstuff\PDF;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;

/**
* Class ServiceProvider
* @version 1.0
* @package Websolutionstuff\PDF
*/
class ServiceProvider extends LaravelServiceProvider
{
protected $constantsMap = [
'K_PATH_MAIN' => 'path_main',
'K_PATH_URL' => 'path_url',
'K_PATH_FONTS' => 'font_directory',
'K_PATH_IMAGES' => 'image_directory',
'PDF_HEADER_LOGO' => 'header_logo',
'PDF_HEADER_LOGO_WIDTH' => 'header_logo_width',
'K_PATH_CACHE' => 'path_cache',
'K_BLANK_IMAGE' => 'blank_image',
'PDF_PAGE_FORMAT' => 'page_format',
'PDF_PAGE_ORIENTATION' => 'page_orientation',
'PDF_CREATOR' => 'creator',
'PDF_AUTHOR' => 'author',
'PDF_HEADER_TITLE' => 'header_title',
'PDF_HEADER_STRING' => 'header_string',
'PDF_UNIT' => 'page_units',
'PDF_MARGIN_HEADER' => 'margin_header',
'PDF_MARGIN_FOOTER' => 'margin_footer',
'PDF_MARGIN_TOP' => 'margin_top',
'PDF_MARGIN_BOTTOM' => 'margin_bottom',
'PDF_MARGIN_LEFT' => 'margin_left',
'PDF_MARGIN_RIGHT' => 'margin_right',
'PDF_FONT_NAME_MAIN' => 'font_name_main',
'PDF_FONT_SIZE_MAIN' => 'font_size_main',
'PDF_FONT_NAME_DATA' => 'font_name_data',
'PDF_FONT_SIZE_DATA' => 'font_size_data',
'PDF_FONT_MONOSPACED' => 'foto_monospaced',
'PDF_IMAGE_SCALE_RATIO' => 'image_scale_ratio',
'HEAD_MAGNIFICATION' => 'head_magnification',
'K_CELL_HEIGHT_RATIO' => 'cell_height_ratio',
'K_TITLE_MAGNIFICATION' => 'title_magnification',
'K_SMALL_RATIO' => 'small_ratio',
'K_THAI_TOPCHARS' => 'thai_topchars',
'K_TCPDF_CALLS_IN_HTML' => 'tcpdf_calls_in_html',
'K_TCPDF_THROW_EXCEPTION_ERROR' => 'tcpdf_throw_exception',
'K_TIMEZONE' => 'timezone',
];

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$configPath = dirname(__FILE__) . '/../config/pdf.php';
$this->mergeConfigFrom($configPath, 'pdf');
$this->app->singleton('pdf', function ($app) {
return new PDF($app);
});
}

public function boot()
{
if (!defined('K_TCPDF_EXTERNAL_CONFIG')) {
define('K_TCPDF_EXTERNAL_CONFIG', true);
}
foreach ($this->constantsMap as $key => $value) {
$value = Config::get('pdf.' . $value, null);
if (!is_null($value) && !defined($key)) {
if (is_string($value) && strlen($value) == 0) {
continue;
}
define($key, $value);
}
}
$configPath = dirname(__FILE__) . '/../config/pdf.php';
if (function_exists('config_path')) {
$targetPath = config_path('pdf.php');
} else {
$targetPath = app()->basePath() . '/config/pdf.php';
}
$this->publishes(array($configPath => $targetPath), 'config');
}

public function provides()
{
return ['pdf'];
}
}

0 comments on commit 3dee77b

Please sign in to comment.