diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..116f35f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +vendor +composer.lock +.idea \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d216c3c --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e7edc58 --- /dev/null +++ b/composer.json @@ -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": "websolutionstuff@gmail.com" + } + ], + "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" + } + } + } +} \ No newline at end of file diff --git a/config/pdf.php b/config/pdf.php new file mode 100644 index 0000000..67da1c0 --- /dev/null +++ b/config/pdf.php @@ -0,0 +1,18 @@ + '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 +]; diff --git a/src/Facades/PDF.php b/src/Facades/PDF.php new file mode 100644 index 0000000..f7cc0eb --- /dev/null +++ b/src/Facades/PDF.php @@ -0,0 +1,11 @@ +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; + } + +} \ No newline at end of file diff --git a/src/PDF.php b/src/PDF.php new file mode 100644 index 0000000..189202d --- /dev/null +++ b/src/PDF.php @@ -0,0 +1,58 @@ +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); + } +} diff --git a/src/PDFHelper.php b/src/PDFHelper.php new file mode 100644 index 0000000..99cb6c1 --- /dev/null +++ b/src/PDFHelper.php @@ -0,0 +1,72 @@ +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; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..f036e10 --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,94 @@ + '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']; + } +}