├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── config └── tcpdf.php └── src ├── Facades └── TCPDF.php ├── FpdiTCPDFHelper.php ├── ServiceProvider.php ├── TCPDF.php └── TCPDFHelper.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel 6-7-8-9-10-11-12 TCPDF 2 | [![Latest Stable Version](https://poser.pugx.org/elibyy/tcpdf-laravel/v/stable)](https://packagist.org/packages/elibyy/tcpdf-laravel) [![Total Downloads](https://poser.pugx.org/elibyy/tcpdf-laravel/downloads)](https://packagist.org/packages/elibyy/tcpdf-laravel) [![Latest Unstable Version](https://poser.pugx.org/elibyy/tcpdf-laravel/v/unstable)](https://packagist.org/packages/elibyy/tcpdf-laravel) [![License](https://poser.pugx.org/elibyy/tcpdf-laravel/license)](https://packagist.org/packages/elibyy/tcpdf-laravel) 3 | 4 | A simple [Laravel](http://www.laravel.com) service provider with some basic configuration for including the [TCPDF library](http://www.tcpdf.org/) 5 | 6 | #### TCPDF is not really supported in PHP 7 but there's a plan for supporting it, check [this](https://github.com/tecnickcom/tc-lib-pdf) out. 7 | 8 | ## Installation 9 | 10 | The Laravel TCPDF service provider can be installed via [composer](http://getcomposer.org) by requiring the `elibyy/tcpdf-laravel` package in your project's `composer.json`. (The installation may take a while, because the package requires TCPDF. Sadly its .git folder is very heavy) 11 | 12 | ``` 13 | composer require elibyy/tcpdf-laravel 14 | ``` 15 | 16 | or 17 | 18 | Laravel 5.5+ will use the auto-discovery function. 19 | 20 | ```json 21 | { 22 | "require": { 23 | "elibyy/tcpdf-laravel": "^9.0" 24 | } 25 | } 26 | ``` 27 | 28 | If you don't use auto-discovery you will need to include the service provider / facade in `config/app.php`. 29 | 30 | 31 | ```php 32 | 'providers' => [ 33 | //... 34 | Elibyy\TCPDF\ServiceProvider::class, 35 | ] 36 | 37 | //... 38 | 39 | 'aliases' => [ 40 | //... 41 | 'PDF' => Elibyy\TCPDF\Facades\TCPDF::class 42 | ] 43 | ``` 44 | 45 | (Please note: TCPDF cannot be used as an alias) 46 | 47 | for lumen you should add the following lines: 48 | 49 | ```php 50 | $app->register(Elibyy\TCPDF\ServiceProvider::class); 51 | class_alias(Elibyy\TCPDF\Facades\TCPDF::class, 'PDF'); 52 | ``` 53 | 54 | That's it! You're good to go. 55 | 56 | Here is a little example: 57 | 58 | ```php 59 | use PDF; // at the top of the file 60 | 61 | PDF::SetTitle('Hello World'); 62 | PDF::AddPage(); 63 | PDF::Write(0, 'Hello World'); 64 | PDF::Output('hello_world.pdf'); 65 | ``` 66 | 67 | another example for generating multiple PDF's 68 | 69 | ```php 70 | use PDF; // at the top of the file 71 | 72 | for ($i = 0; $i < 5; $i++) { 73 | PDF::SetTitle('Hello World'.$i); 74 | PDF::AddPage(); 75 | PDF::Write(0, 'Hello World'.$i); 76 | PDF::Output(public_path('hello_world' . $i . '.pdf'), 'F'); 77 | PDF::reset(); 78 | } 79 | ``` 80 | 81 | For a list of all available function take a look at the [TCPDF Documentation](http://www.tcpdf.org/doc/code/classTCPDF.html) 82 | 83 | ## Configuration 84 | 85 | Laravel-TCPDF comes with some basic configuration. 86 | If you want to override the defaults, you can publish the config, like so: 87 | 88 | php artisan vendor:publish --provider="Elibyy\TCPDF\ServiceProvider" 89 | 90 | Now access `config/tcpdf.php` to customize. 91 | 92 | * use_original_header is to used the original `Header()` from TCPDF. 93 | * Please note that `PDF::setHeaderCallback(function($pdf){})` overrides this settings. 94 | * use_original_footer is to used the original `Footer()` from TCPDF. 95 | * Please note that `PDF::setFooterCallback(function($pdf){})` overrides this settings. 96 | * use_fpdi is so that our internal helper will extend `TcpdfFpdi` instead of `TCPDF`. 97 | * Please note fpdi is not a dependency in my project so you will have to follow their install instructions [here](https://github.com/Setasign/FPDI) 98 | 99 | ## Header/Footer helpers 100 | 101 | I've got a pull-request asking for this so I've added the feature 102 | 103 | now you can use `PDF::setHeaderCallback(function($pdf){})` or `PDF::setFooterCallback(function($pdf){})` 104 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "elibyy/tcpdf-laravel", 3 | "description": "tcpdf support for Laravel 6, 7, 8, 9, 10, 11", 4 | "minimum-stability": "stable", 5 | "license": "MIT", 6 | "keywords": [ 7 | "laravel", 8 | "tcpdf", 9 | "pdf" 10 | ], 11 | "authors": [ 12 | { 13 | "name": "eli y", 14 | "email": "elibyy@gmail.com" 15 | } 16 | ], 17 | "require": { 18 | "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 19 | "tecnickcom/tcpdf": "6.2.*|6.3.*|6.4.*|6.5.*|6.6.*|6.7.*|6.8.*|6.9.*|6.10.*|dev-main" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Elibyy\\TCPDF\\": "src/" 24 | } 25 | }, 26 | "extra": { 27 | "laravel": { 28 | "providers": [ 29 | "Elibyy\\TCPDF\\ServiceProvider" 30 | ], 31 | "aliases": { 32 | "PDF": "Elibyy\\TCPDF\\Facades\\TCPDF" 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /config/tcpdf.php: -------------------------------------------------------------------------------- 1 | 'A4', 4 | 'page_orientation' => 'P', 5 | 'page_units' => 'mm', 6 | 'unicode' => true, 7 | 'encoding' => 'UTF-8', 8 | 'font_directory' => '', 9 | 'image_directory' => '', 10 | 'tcpdf_throw_exception' => false, 11 | 'use_fpdi' => false, 12 | 'use_original_header' => false, 13 | 'use_original_footer' => false, 14 | 'pdfa' => false, // Options: false, 1, 3 15 | 16 | // See more info at the tcpdf_config.php file in TCPDF (if you do not set this here, TCPDF will use it default) 17 | // https://raw.githubusercontent.com/tecnickcom/TCPDF/master/config/tcpdf_config.php 18 | 19 | // 'path_main' => '', // K_PATH_MAIN 20 | // 'path_url' => '', // K_PATH_URL 21 | // 'header_logo' => '', // PDF_HEADER_LOGO 22 | // 'header_logo_width' => '', // PDF_HEADER_LOGO_WIDTH 23 | // 'path_cache' => '', // K_PATH_CACHE 24 | // 'blank_image' => '', // K_BLANK_IMAGE 25 | // 'creator' => '', // PDF_CREATOR 26 | // 'author' => '', // PDF_AUTHOR 27 | // 'header_title' => '', // PDF_HEADER_TITLE 28 | // 'header_string' => '', // PDF_HEADER_STRING 29 | // 'page_units' => '', // PDF_UNIT 30 | // 'margin_header' => '', // PDF_MARGIN_HEADER 31 | // 'margin_footer' => '', // PDF_MARGIN_FOOTER 32 | // 'margin_top' => '', // PDF_MARGIN_TOP 33 | // 'margin_bottom' => '', // PDF_MARGIN_BOTTOM 34 | // 'margin_left' => '', // PDF_MARGIN_LEFT 35 | // 'margin_right' => '', // PDF_MARGIN_RIGHT 36 | // 'font_name_main' => '', // PDF_FONT_NAME_MAIN 37 | // 'font_size_main' => '', // PDF_FONT_SIZE_MAIN 38 | // 'font_name_data' => '', // PDF_FONT_NAME_DATA 39 | // 'font_size_data' => '', // PDF_FONT_SIZE_DATA 40 | // 'foto_monospaced' => '', // PDF_FONT_MONOSPACED 41 | // 'image_scale_ratio' => '', // PDF_IMAGE_SCALE_RATIO 42 | // 'head_magnification' => '', // HEAD_MAGNIFICATION 43 | // 'cell_height_ratio' => '', // K_CELL_HEIGHT_RATIO 44 | // 'title_magnification' => '', // K_TITLE_MAGNIFICATION 45 | // 'small_ratio' => '', // K_SMALL_RATIO 46 | // 'thai_topchars' => '', // K_THAI_TOPCHARS 47 | // 'tcpdf_calls_in_html' => '', // K_TCPDF_CALLS_IN_HTML 48 | // 'timezone' => '', // K_TIMEZONE 49 | // 'allowed_tags' => '', // K_ALLOWED_TCPDF_TAGS 50 | ]; 51 | -------------------------------------------------------------------------------- /src/Facades/TCPDF.php: -------------------------------------------------------------------------------- 1 | headerCallback != null && is_callable($this->headerCallback)) { 17 | $cb = $this->headerCallback; 18 | $cb($this); 19 | } else { 20 | if (Config::get('tcpdf.use_original_header')) { 21 | parent::Header(); 22 | } 23 | } 24 | } 25 | 26 | public function Footer() 27 | { 28 | if ($this->footerCallback != null && is_callable($this->footerCallback)) { 29 | $cb = $this->footerCallback; 30 | $cb($this); 31 | } else { 32 | if (Config::get('tcpdf.use_original_footer')) { 33 | parent::Footer(); 34 | } 35 | } 36 | } 37 | 38 | public function setHeaderCallback($callback) 39 | { 40 | $this->headerCallback = $callback; 41 | } 42 | 43 | public function setFooterCallback($callback) 44 | { 45 | $this->footerCallback = $callback; 46 | } 47 | 48 | public function disableTcpdfLink() 49 | { 50 | // disable tcpdf metalink *sigh* 51 | $this->tcpdflink = false; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 'path_main', 17 | 'K_PATH_URL' => 'path_url', 18 | 'K_PATH_FONTS' => 'font_directory', 19 | 'K_PATH_IMAGES' => 'image_directory', 20 | 'PDF_HEADER_LOGO' => 'header_logo', 21 | 'PDF_HEADER_LOGO_WIDTH' => 'header_logo_width', 22 | 'K_PATH_CACHE' => 'path_cache', 23 | 'K_BLANK_IMAGE' => 'blank_image', 24 | 'PDF_PAGE_FORMAT' => 'page_format', 25 | 'PDF_PAGE_ORIENTATION' => 'page_orientation', 26 | 'PDF_CREATOR' => 'creator', 27 | 'PDF_AUTHOR' => 'author', 28 | 'PDF_HEADER_TITLE' => 'header_title', 29 | 'PDF_HEADER_STRING' => 'header_string', 30 | 'PDF_UNIT' => 'page_units', 31 | 'PDF_MARGIN_HEADER' => 'margin_header', 32 | 'PDF_MARGIN_FOOTER' => 'margin_footer', 33 | 'PDF_MARGIN_TOP' => 'margin_top', 34 | 'PDF_MARGIN_BOTTOM' => 'margin_bottom', 35 | 'PDF_MARGIN_LEFT' => 'margin_left', 36 | 'PDF_MARGIN_RIGHT' => 'margin_right', 37 | 'PDF_FONT_NAME_MAIN' => 'font_name_main', 38 | 'PDF_FONT_SIZE_MAIN' => 'font_size_main', 39 | 'PDF_FONT_NAME_DATA' => 'font_name_data', 40 | 'PDF_FONT_SIZE_DATA' => 'font_size_data', 41 | 'PDF_FONT_MONOSPACED' => 'foto_monospaced', 42 | 'PDF_IMAGE_SCALE_RATIO' => 'image_scale_ratio', 43 | 'HEAD_MAGNIFICATION' => 'head_magnification', 44 | 'K_CELL_HEIGHT_RATIO' => 'cell_height_ratio', 45 | 'K_TITLE_MAGNIFICATION' => 'title_magnification', 46 | 'K_SMALL_RATIO' => 'small_ratio', 47 | 'K_THAI_TOPCHARS' => 'thai_topchars', 48 | 'K_TCPDF_CALLS_IN_HTML' => 'tcpdf_calls_in_html', 49 | 'K_TCPDF_THROW_EXCEPTION_ERROR' => 'tcpdf_throw_exception', 50 | 'K_TIMEZONE' => 'timezone', 51 | 'K_ALLOWED_TCPDF_TAGS' => 'allowed_tags', 52 | ]; 53 | 54 | /** 55 | * Register the service provider. 56 | * 57 | * @return void 58 | */ 59 | public function register() 60 | { 61 | $configPath = dirname(__FILE__) . '/../config/tcpdf.php'; 62 | $this->mergeConfigFrom($configPath, 'tcpdf'); 63 | $this->app->singleton('tcpdf', function ($app) { 64 | return new TCPDF($app); 65 | }); 66 | } 67 | 68 | public function boot() 69 | { 70 | if (!defined('K_TCPDF_EXTERNAL_CONFIG')) { 71 | define('K_TCPDF_EXTERNAL_CONFIG', true); 72 | } 73 | foreach ($this->constantsMap as $key => $value) { 74 | $value = Config::get('tcpdf.' . $value, null); 75 | if (!is_null($value) && !defined($key)) { 76 | if (is_string($value) && strlen($value) == 0) { 77 | continue; 78 | } 79 | define($key, $value); 80 | } 81 | } 82 | $configPath = dirname(__FILE__) . '/../config/tcpdf.php'; 83 | if (function_exists('config_path')) { 84 | $targetPath = config_path('tcpdf.php'); 85 | } else { 86 | $targetPath = app()->basePath() . '/config/tcpdf.php'; 87 | } 88 | $this->publishes(array($configPath => $targetPath), 'config'); 89 | } 90 | 91 | public function provides() 92 | { 93 | return ['pdf']; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/TCPDF.php: -------------------------------------------------------------------------------- 1 | app = $app; 18 | $this->reset(); 19 | } 20 | 21 | public function reset() 22 | { 23 | $class = Config::get('tcpdf.use_fpdi') ? FpdiTCPDFHelper::class : TCPDFHelper::class; 24 | 25 | $this->tcpdf = new $class( 26 | Config::get('tcpdf.page_orientation', 'P'), 27 | Config::get('tcpdf.page_units', 'mm'), 28 | static::$format ? static::$format : Config::get('tcpdf.page_format', 'A4'), 29 | Config::get('tcpdf.unicode', true), 30 | Config::get('tcpdf.encoding', 'UTF-8'), 31 | false, // Diskcache is deprecated 32 | Config::get('tcpdf.pdfa', false) 33 | ); 34 | 35 | $this->tcpdf->disableTcpdfLink(); 36 | } 37 | 38 | public static function changeFormat($format) 39 | { 40 | static::$format = $format; 41 | } 42 | 43 | public function __call($method, $args) 44 | { 45 | if (method_exists($this->tcpdf, $method)) { 46 | return call_user_func_array([$this->tcpdf, $method], $args); 47 | } 48 | throw new \RuntimeException(sprintf('the method %s does not exists in TCPDF', $method)); 49 | } 50 | 51 | public function setHeaderCallback($headerCallback) 52 | { 53 | $this->tcpdf->setHeaderCallback($headerCallback); 54 | } 55 | 56 | public function setFooterCallback($footerCallback) 57 | { 58 | $this->tcpdf->setFooterCallback($footerCallback); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/TCPDFHelper.php: -------------------------------------------------------------------------------- 1 | headerCallback != null && is_callable($this->headerCallback)) { 16 | $cb = $this->headerCallback; 17 | $cb($this); 18 | } else { 19 | if (Config::get('tcpdf.use_original_header')) { 20 | parent::Header(); 21 | } 22 | } 23 | } 24 | 25 | public function Footer() 26 | { 27 | if ($this->footerCallback != null && is_callable($this->footerCallback)) { 28 | $cb = $this->footerCallback; 29 | $cb($this); 30 | } else { 31 | if (Config::get('tcpdf.use_original_footer')) { 32 | parent::Footer(); 33 | } 34 | } 35 | } 36 | 37 | public function setHeaderCallback($callback) 38 | { 39 | $this->headerCallback = $callback; 40 | } 41 | 42 | public function setFooterCallback($callback) 43 | { 44 | $this->footerCallback = $callback; 45 | } 46 | 47 | public function addTOC($page = '', $numbersfont = '', $filler = '.', $toc_name = 'TOC', $style = '', $color = array(0, 0, 0)) 48 | { 49 | // sort bookmarks before generating the TOC 50 | parent::sortBookmarks(); 51 | 52 | parent::addTOC($page, $numbersfont, $filler, $toc_name, $style, $color); 53 | } 54 | 55 | public function addHTMLTOC($page = '', $toc_name = 'TOC', $templates = array(), $correct_align = true, $style = '', $color = array(0, 0, 0)) 56 | { 57 | // sort bookmarks before generating the TOC 58 | parent::sortBookmarks(); 59 | 60 | parent::addHTMLTOC($page, $toc_name, $templates, $correct_align, $style, $color); 61 | } 62 | 63 | public function checkPageBreak($h = 0, $y = null, $addpage = true) 64 | { 65 | parent::checkPageBreak($h, $y, $addpage); 66 | } 67 | 68 | public function getPageBreakTrigger() 69 | { 70 | return $this->PageBreakTrigger; 71 | } 72 | 73 | public function disableTcpdfLink() 74 | { 75 | // disable tcpdf metalink *sigh* 76 | $this->tcpdflink = false; 77 | } 78 | } 79 | --------------------------------------------------------------------------------