├── .gitignore ├── .travis.yml ├── LICENSE ├── composer.json ├── phpunit.xml ├── public ├── .gitkeep └── images │ └── _blank.png ├── readme.md ├── src ├── Maxxscho │ └── LaravelTcpdf │ │ ├── Facades │ │ └── LaravelTcpdfFacade.php │ │ ├── LaravelTcpdf.php │ │ └── LaravelTcpdfServiceProvider.php └── config │ ├── .gitkeep │ └── config.php └── tests └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .idea -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | 8 | before_script: 9 | - curl -s http://getcomposer.org/installer | php 10 | - php composer.phar install --dev 11 | 12 | script: phpunit -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Markus Schober 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | 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, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "maxxscho/laravel-tcpdf", 3 | "description": "A simple Laravel 4 service provider with some basic configuration for including the TCPDF library", 4 | "keywords": ["laravel", "pdf", "tcpdf"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Markus Schober", 9 | "email": "maxx.schober@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.3.0", 14 | "illuminate/support": "~4", 15 | "tecnickcom/tcpdf": "6.*" 16 | }, 17 | "autoload": { 18 | "psr-0": { 19 | "Maxxscho\\LaravelTcpdf\\": "src/" 20 | } 21 | }, 22 | "minimum-stability": "dev" 23 | } 24 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /public/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxxscho/laravel-tcpdf/cb92f1110c5221ad64c50ff004c660ec7129d9a1/public/.gitkeep -------------------------------------------------------------------------------- /public/images/_blank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxxscho/laravel-tcpdf/cb92f1110c5221ad64c50ff004c660ec7129d9a1/public/images/_blank.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | **NOT MAINTAINED ANYMORE!!** 2 | 3 | # Laravel TCPDF 4 | 5 | [![Build Status](https://travis-ci.org/maxxscho/laravel-tcpdf.png?branch=master)](https://travis-ci.org/maxxscho/laravel-tcpdf) 6 | 7 | A simple [Laravel 4](http://www.laravel.com) service provider with some basic configuration for including the [TCPDF library](http://www.tcpdf.org/) 8 | 9 | ## Installation 10 | 11 | The Laravel TCPDF service provider can be installed via [composer](http://getcomposer.org) by requiring the `maxxscho/laravel-tcpdf` 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) 12 | 13 | ```json 14 | { 15 | "require": { 16 | "maxxscho/laravel-tcpdf": "0.*" 17 | } 18 | } 19 | ``` 20 | 21 | Next, add the service provider to `app/config/app.php`. 22 | 23 | ```php 24 | 'providers' => [ 25 | //.. 26 | 'Maxxscho\LaravelTcpdf\LaravelTcpdfServiceProvider', 27 | ] 28 | ``` 29 | 30 | That's it! You're good to go. 31 | 32 | Here is a little example: 33 | 34 | ```php 35 | PDF::SetTitle('Hello World'); 36 | 37 | PDF::AddPage(); 38 | 39 | PDF::Write(0, 'Hello World'); 40 | 41 | PDF::Output('hello_world.pdf'); 42 | ``` 43 | For a list of all available function take a look at the [TCPDF Documentation](http://www.tcpdf.org/doc/code/classTCPDF.html) 44 | 45 | ## Configuration 46 | 47 | Laravel-TCPDF comes with some basic configuration. 48 | If you want to override the defaults, you can publish the config, like so: 49 | 50 | php artisan config:publish maxxscho/laravel-tcpdf 51 | 52 | Now access `app/config/packages/maxxscho/laravel-tcpdf/config.php`to customize. 53 | 54 | ## Assets 55 | 56 | There is a 'blank' image in the assets folder of the package, 57 | which is in certain circumstances needed by TCPDF. 58 | Publish the assets, like so: 59 | 60 | php artisan asset:publish maxxscho/laravel-tcpdf 61 | 62 | ## Extend/Overwrite 63 | 64 | Extending or overwriting Laravel TCPDF is easy. Simply extend `\Maxxscho\LaravelTcpdf\LaravelTcpdf` with your own class. 65 | 66 | ## Custom Fonts 67 | 68 | To add custom fonts set the fonts_directory in the config, relative to the public path. For example `'fonts/'`. 69 | 70 | To use a custom font you have to convert a font for TCPDF. 71 | Copy your custom font(s) to your fonts path, in our case `public/fonts/`. 72 | In your terminal do this: 73 | 74 | ``` 75 | vendor/maxxscho/laravel-tcpdf/vendor/tecnick.com/tcpdf/tools/tcpdf_addfont.php -i public/fonts/yourfont.ttf -o public/fonts 76 | ``` 77 | 78 | This uses a little tool provided by TCPDF to convert fonts for TCPDF. 79 | The `-i` flag is for the input fonts (comma-separated list) 80 | and the `-o` flag is for the output directory. 81 | Read here all about [TCPDF fonts](http://www.tcpdf.org/fonts.php) and how to convert them [the new way](http://queirozf.com/entries/adding-a-custom-font-to-tcpdf). 82 | 83 | 84 | ## Custom Header 85 | "`PDF::setHtmlHeader($custom_header); 86 | PDF::Header();`" 87 | -------------------------------------------------------------------------------- /src/Maxxscho/LaravelTcpdf/Facades/LaravelTcpdfFacade.php: -------------------------------------------------------------------------------- 1 | 'cache_directory', 17 | 'K_PATH_IMAGES' => 'image_directory', 18 | 'K_BLANK_IMAGE' => 'blank_image', 19 | 'K_SMALL_RATIO' => 'small_font_ratio' 20 | ]; 21 | 22 | 23 | 24 | /** 25 | * Constructor 26 | * 27 | * @author Markus Schober 28 | */ 29 | public function __construct() 30 | { 31 | // Initialize TCPDF 32 | parent::__construct( 33 | Config::get('laravel-tcpdf::page_orientation'), 34 | Config::get('laravel-tcpdf::page_unit'), 35 | Config::get('laravel-tcpdf::page_format'), 36 | Config::get('laravel-tcpdf::unicode'), 37 | Config::get('laravel-tcpdf::encoding'), 38 | Config::get('laravel-tcpdf::enable_disk_cache') 39 | ); 40 | 41 | // default margin settings 42 | $this->SetMargins( 43 | Config::get('laravel-tcpdf::margin_left'), 44 | Config::get('laravel-tcpdf::margin_top'), 45 | Config::get('laravel-tcpdf::margin_right') 46 | ); 47 | 48 | // default header setting 49 | $this->headerSettings(); 50 | 51 | // default footer settings 52 | $this->footerSettings(); 53 | 54 | // default page break settings 55 | $this->SetAutoPageBreak( 56 | Config::get('laravel-tcpdf::page_break_auto'), 57 | Config::get('laravel-tcpdf::footer_margin') 58 | ); 59 | 60 | // default cell settings 61 | $this->cellSettings(); 62 | 63 | // default document properties 64 | $this->setDocumentProperties(); 65 | 66 | // default page font 67 | $this->setFont( 68 | Config::get('laravel-tcpdf::page_font'), 69 | '', 70 | Config::get('laravel-tcpdf::page_font_size') 71 | ); 72 | 73 | // default image scale 74 | $this->setImageScale(Config::get('laravel-tcpdf::image_scale')); 75 | } 76 | 77 | 78 | 79 | /** 80 | * Set all the necessary header settings 81 | * 82 | * @author Markus Schober 83 | */ 84 | protected function headerSettings() 85 | { 86 | $this->setPrintHeader( 87 | Config::get('laravel-tcpdf::header_on') 88 | ); 89 | 90 | $this->setHeaderFont(array( 91 | Config::get('laravel-tcpdf::header_font'), 92 | '', 93 | Config::get('laravel-tcpdf::header_font_size') 94 | )); 95 | 96 | $this->setHeaderMargin( 97 | Config::get('laravel-tcpdf::header_margin') 98 | ); 99 | 100 | $this->SetHeaderData( 101 | Config::get('laravel-tcpdf::header_logo'), 102 | Config::get('laravel-tcpdf::header_logo_width'), 103 | Config::get('laravel-tcpdf::header_title'), 104 | Config::get('laravel-tcpdf::header_string') 105 | ); 106 | } 107 | 108 | 109 | 110 | /** 111 | * Set all the necessary footer settings 112 | * 113 | * @author Markus Schober 114 | */ 115 | protected function footerSettings() 116 | { 117 | $this->setPrintFooter( 118 | Config::get('laravel-tcpdf::footer_on') 119 | ); 120 | 121 | $this->setFooterFont(array( 122 | Config::get('laravel-tcpdf::footer_font'), 123 | '', 124 | Config::get('laravel-tcpdf::footer_font_size') 125 | )); 126 | 127 | $this->setFooterMargin( 128 | Config::get('laravel-tcpdf::footer_margin') 129 | ); 130 | } 131 | 132 | 133 | 134 | /** 135 | * Set the default cell settings 136 | * 137 | * @author Markus Schober 138 | */ 139 | protected function cellSettings() 140 | { 141 | $this->SetCellPadding( 142 | Config::get('laravel-tcpdf::cell_padding') 143 | ); 144 | 145 | $this->setCellHeightRatio( 146 | Config::get('laravel-tcpdf::cell_height_ratio') 147 | ); 148 | } 149 | 150 | 151 | 152 | /** 153 | * Set default document properties 154 | * 155 | * @author Markus Schober 156 | */ 157 | protected function setDocumentProperties() 158 | { 159 | $this->SetCreator(Config::get('laravel-tcpdf::creator')); 160 | $this->SetAuthor(Config::get('laravel-tcpdf::author')); 161 | } 162 | 163 | /** 164 | * Custom Header 165 | * 166 | */ 167 | 168 | public function setHtmlHeader($htmlHeader) { 169 | $this->htmlHeader = $htmlHeader; 170 | } 171 | //Customize according to your specification 172 | public function Header() { 173 | $this->WriteHTML($this->htmlHeader, true,false, false, false,'L'); 174 | 175 | } 176 | 177 | } 178 | -------------------------------------------------------------------------------- /src/Maxxscho/LaravelTcpdf/LaravelTcpdfServiceProvider.php: -------------------------------------------------------------------------------- 1 | 'fonts_directory', 24 | 'K_PATH_IMAGES' => 'image_directory', 25 | 'PDF_HEADER_LOGO' => 'header_logo', 26 | 'PDF_HEADER_LOGO_WIDTH' => 'header_logo_width', 27 | 'K_PATH_CACHE' => 'cache_directory', 28 | 'K_BLANK_IMAGE' => 'blank_image', 29 | 'PDF_PAGE_FORMAT' => 'page_format', 30 | 'PDF_PAGE_ORIENTATION' => 'page_orientation', 31 | 'PDF_CREATOR' => 'creator', 32 | 'PDF_AUTHOR' => 'author', 33 | 'PDF_HEADER_TITLE' => 'header_title', 34 | 'PDF_HEADER_STRING' => 'header_string', 35 | 'PDF_UNIT' => 'page_unit', 36 | 'PDF_MARGIN_HEADER' => 'header_margin', 37 | 'PDF_MARGIN_FOOTER' => 'footer_margin', 38 | 'PDF_MARGIN_TOP' => 'margin_top', 39 | 'PDF_MARGIN_BOTTOM' => 'margin_bottom', 40 | 'PDF_MARGIN_LEFT' => 'margin_left', 41 | 'PDF_MARGIN_RIGHT' => 'margin_right', 42 | 'PDF_FONT_NAME_MAIN' => 'page_font', 43 | 'PDF_FONT_SIZE_MAIN' => 'page_font_size', 44 | 'PDF_FONT_NAME_DATA' => 'page_font', 45 | 'PDF_FONT_SIZE_DATA' => 'page_font_size', 46 | 'PDF_FONT_MONOSPACED' => 'font_monospaced', 47 | 'PDF_IMAGE_SCALE_RATIO' => 'image_scale', 48 | 'HEAD_MAGNIFICATION' => 'head_magnification', 49 | 'K_CELL_HEIGHT_RATIO' => 'cell_height_ratio', 50 | 'K_TITLE_MAGNIFICATION' => 'title_magnification', 51 | 'K_SMALL_RATIO' => 'small_font_ratio', 52 | 'K_THAI_TOPCHARS' => 'thai_topchars', 53 | 'K_TCPDF_CALLS_IN_HTML' => 'tcpdf_calls_in_html', 54 | 'K_TCPDF_THROW_EXCEPTION_ERROR' => 'tcpdf_throw_exception_error', 55 | ]; 56 | 57 | 58 | 59 | /** 60 | * Bootstrap the application events. 61 | * 62 | * @return void 63 | */ 64 | public function boot() 65 | { 66 | $this->package('maxxscho/laravel-tcpdf'); 67 | 68 | /* override the default TCPDF config file 69 | ------------------------------------- */ 70 | if (!defined('K_TCPDF_EXTERNAL_CONFIG')) 71 | { 72 | define('K_TCPDF_EXTERNAL_CONFIG', TRUE); 73 | } 74 | 75 | $this->setTcpdfConstants(); 76 | 77 | AliasLoader::getInstance()->alias('PDF', 'Maxxscho\LaravelTcpdf\Facades\LaravelTcpdfFacade'); 78 | } 79 | 80 | 81 | 82 | /** 83 | * Register the service provider. 84 | * 85 | * @return void 86 | */ 87 | public function register() 88 | { 89 | $this->app['pdf'] = $this->app->share(function ($app) 90 | { 91 | return new LaravelTcpdf; 92 | }); 93 | } 94 | 95 | 96 | 97 | /** 98 | * Get the services provided by the provider. 99 | * 100 | * @return array 101 | */ 102 | public function provides() 103 | { 104 | return array('pdf'); 105 | } 106 | 107 | 108 | 109 | /** 110 | * Set TCPDF constants based on configuration file. 111 | * !Notice! Some contants are never used by TCPDF. They are in the config file of TCPDF, but ... 112 | * This is a bug by TCPDF but we set it for completeness. 113 | * 114 | * @author Markus Schober 115 | */ 116 | private function setTcpdfConstants() 117 | { 118 | foreach ($this->config_constant_map as $const => $configkey) 119 | { 120 | if (!defined($const)) 121 | { 122 | if (is_string(\Config::get('laravel-tcpdf::' . $configkey))) 123 | { 124 | if (strlen(\Config::get('laravel-tcpdf::' . $configkey)) > 0) 125 | { 126 | define($const, \Config::get('laravel-tcpdf::' . $configkey)); 127 | } 128 | } 129 | else 130 | { 131 | define($const, \Config::get('laravel-tcpdf::' . $configkey)); 132 | } 133 | } 134 | } 135 | } 136 | 137 | } -------------------------------------------------------------------------------- /src/config/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxxscho/laravel-tcpdf/cb92f1110c5221ad64c50ff004c660ec7129d9a1/src/config/.gitkeep -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | 'A4', 21 | 22 | /* 23 | |-------------------------------------------------------------------------- 24 | | Page Orientation 25 | |-------------------------------------------------------------------------- 26 | | Default page orientation 27 | | P = portrait, L = landscape 28 | | 29 | */ 30 | 31 | 'page_orientation' => 'P', 32 | 33 | /* 34 | |-------------------------------------------------------------------------- 35 | | Page Unit 36 | |-------------------------------------------------------------------------- 37 | | Default unit of measure 38 | | mm = millimeters, cm = centimeters, pt = points, in = inches 39 | | 1 point = 1/72 in = ~0.35 mm 40 | | 1 inch = 2.54 cm 41 | | 42 | */ 43 | 44 | 'page_unit' => 'mm', 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | Auto page break 49 | |-------------------------------------------------------------------------- 50 | | Enables automatic flowing of content to the next page if you 51 | | run out of room on the current page. 52 | | 53 | */ 54 | 55 | 'page_break_auto' => true, 56 | 57 | /* 58 | |-------------------------------------------------------------------------- 59 | | Default font settings 60 | |-------------------------------------------------------------------------- 61 | | Page font, font size, HTML font size ratio 62 | | 63 | */ 64 | 65 | 'page_font' => 'helvetica', 66 | 'page_font_size' => 10, 67 | 'small_font_ratio' => 2 / 3, 68 | 'font_monospaced' => 'courier', 69 | 70 | /* 71 | |-------------------------------------------------------------------------- 72 | | Default document creator and author settings 73 | |-------------------------------------------------------------------------- 74 | */ 75 | 76 | 'creator' => 'Laravel-TCPDF by Markus Schober', 77 | 'author' => 'Laravel-TCPDF by Markus Schober', 78 | 79 | /* 80 | |-------------------------------------------------------------------------- 81 | | Default page margin 82 | |-------------------------------------------------------------------------- 83 | | Top, bottom, left, right margin settings 84 | | in the default unit of measure. 85 | */ 86 | 87 | 'margin_top' => 30, 88 | 'margin_bottom' => 30, // currently not used 89 | 'margin_left' => 20, 90 | 'margin_right' => 20, 91 | 92 | /* 93 | |-------------------------------------------------------------------------- 94 | | Header settings 95 | |-------------------------------------------------------------------------- 96 | | Enable the header, set the font, default text, margin, description string 97 | | and logo 98 | | 99 | */ 100 | 101 | 'header_on' => true, 102 | 'header_title' => 'Laravel-TCPDF', 103 | 'header_string' => 'by Markus Schober', 104 | 'header_font' => 'helvetica', 105 | 'header_font_size' => 10, 106 | 'header_logo' => '', 107 | 'header_logo_width' => 30, 108 | 'header_margin' => 5, 109 | 'head_magnification' => 1.1, 110 | 'title_magnification' => 1.3, 111 | 112 | /* 113 | |-------------------------------------------------------------------------- 114 | | Footer settings 115 | |-------------------------------------------------------------------------- 116 | | Enable the footer, set the font and default margin 117 | | 118 | */ 119 | 120 | 'footer_on' => true, 121 | 'footer_font' => 'helvetica', 122 | 'footer_font_size' => 8, 123 | 'footer_margin' => 10, 124 | 125 | /* 126 | |-------------------------------------------------------------------------- 127 | | Text encoding 128 | |-------------------------------------------------------------------------- 129 | | Specify TRUE if the input text you will be using is unicode, and 130 | | specify the default encoding. 131 | | 132 | */ 133 | 134 | 'unicode' => true, 135 | 'encoding' => 'UTF-8', 136 | 137 | /* 138 | |-------------------------------------------------------------------------- 139 | | TCPDF default image directory 140 | |-------------------------------------------------------------------------- 141 | | This is the image directory for TCPDF. This is where you can store 142 | | images to use in your PDF files. 143 | | Relative path from the 'public' directory 144 | | 145 | */ 146 | 147 | 'image_directory' => 'packages/maxxscho/laravel-tcpdf/images/', 148 | 149 | /* 150 | |-------------------------------------------------------------------------- 151 | | TCPDF default (blank) image 152 | |-------------------------------------------------------------------------- 153 | | This is the path and filename to the default (blank) image, 154 | | relative to the image directory, set above. 155 | | Publish the assets of this package and you're good to go 156 | | $ php artisan asset:publish maxxscho/laravel-tcpdf 157 | | 158 | */ 159 | 160 | 'blank_image' => '_blank.png', 161 | 162 | /* 163 | |-------------------------------------------------------------------------- 164 | | TCPDF Fonts directory 165 | |-------------------------------------------------------------------------- 166 | | This is the fonts directory for TCPDF relative to the public directory. 167 | | Leave it blank to use the default fonts of TCPDF. 168 | */ 169 | 170 | 'fonts_directory' => '', 171 | 172 | /* 173 | |-------------------------------------------------------------------------- 174 | | TCPDF image scale ratio 175 | |-------------------------------------------------------------------------- 176 | | Image scale ratio (decimal format) used to adjust 177 | | the conversion of pixels to user units 178 | | 179 | */ 180 | 181 | 'image_scale' => 4, 182 | 183 | /* 184 | |-------------------------------------------------------------------------- 185 | | TCPDF cell settings 186 | |-------------------------------------------------------------------------- 187 | | Fontsize-to-height ratio, cell padding 188 | | 189 | */ 190 | 191 | 'cell_height_ratio' => 1.25, 192 | 'cell_padding' => 0, 193 | 194 | /* 195 | |-------------------------------------------------------------------------- 196 | | TCPDF disk cache settings 197 | |-------------------------------------------------------------------------- 198 | | Enable caching 199 | | If you enable caching, you have to define a cache directory for TCPDF 200 | | (make sure that it is writable by the webserver) 201 | | 202 | | ADD TRAILING SLASH! 203 | | 204 | */ 205 | 206 | 'enable_disk_cache' => false, 207 | 'cache_directory' => '', 208 | 209 | /* 210 | |-------------------------------------------------------------------------- 211 | | Thai Chars setting 212 | |-------------------------------------------------------------------------- 213 | | Set to true to enable the special procedure used to avoid 214 | | the overlappind of symbols on Thai language. 215 | | 216 | */ 217 | 218 | 'thai_topchars' => true, 219 | 220 | /* 221 | |-------------------------------------------------------------------------- 222 | | Calls for methods in HTML Syntax 223 | |-------------------------------------------------------------------------- 224 | | If true allows to call TCPDF methods using HTML syntax 225 | | IMPORTANT: For security reason, disable this feature if you are printing user HTML content. 226 | | 227 | */ 228 | 229 | 'tcpdf_calls_in_html' => true, 230 | 231 | /* 232 | |-------------------------------------------------------------------------- 233 | | Error settings 234 | |-------------------------------------------------------------------------- 235 | | If true and PHP version is greater than 5, then the Error() method 236 | | throw new exception instead of terminating the execution. 237 | | 238 | */ 239 | 240 | 'tcpdf_throw_exception_error' => false, 241 | ]; 242 | -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/maxxscho/laravel-tcpdf/cb92f1110c5221ad64c50ff004c660ec7129d9a1/tests/.gitkeep --------------------------------------------------------------------------------