├── .gitignore ├── code-of-conduct.md ├── Resources ├── config │ └── services.xml └── meta │ └── LICENSE ├── composer.json ├── Controller └── TCPDFController.php ├── DependencyInjection ├── WhiteOctoberTCPDFExtension.php └── Configuration.php ├── WhiteOctoberTCPDFBundle.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | /vendor/ 3 | -------------------------------------------------------------------------------- /code-of-conduct.md: -------------------------------------------------------------------------------- 1 | This project's code-of-conduct can be found at [https://github.com/whiteoctober/open-source-code-of-conduct/blob/master/code_of_conduct.md](https://github.com/whiteoctober/open-source-code-of-conduct/blob/master/code_of_conduct.md). 2 | -------------------------------------------------------------------------------- /Resources/config/services.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | %white_october_tcpdf.file% 11 | %white_october_tcpdf.class% 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "whiteoctober/tcpdf-bundle", 3 | "type": "symfony-bundle", 4 | "description": "A bundle to easily integrate TCPDF into Symfony2", 5 | "keywords": ["pdf", "tcpdf"], 6 | "homepage": "https://github.com/whiteoctober/WhiteOctoberTCPDFBundle", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Rich Sage", 11 | "email": "rich.sage@gmail.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.2", 16 | "symfony/framework-bundle": ">=2.0", 17 | "tecnickcom/tcpdf": "*" 18 | }, 19 | "autoload": { 20 | "psr-4": { "WhiteOctober\\TCPDFBundle\\": "" } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Resources/meta/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 White October Ltd 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /Controller/TCPDFController.php: -------------------------------------------------------------------------------- 1 | setClassName($className); 21 | } 22 | 23 | /** 24 | * Creates a new instance of TCPDF/the class name to use as supplied 25 | * Any arguments passed here will be passed directly 26 | * to the TCPDF class as constructor arguments 27 | * 28 | * @return TCPDF 29 | */ 30 | public function create() 31 | { 32 | $rc = new ReflectionClass($this->className); 33 | return $rc->newInstanceArgs(func_get_args()); 34 | } 35 | 36 | /** 37 | * Sets the class name to use for instantiation 38 | * 39 | * @param $className 40 | * @throws \LogicException if the class is not, or does not inherit from, TCPDF 41 | */ 42 | public function setClassName($className) 43 | { 44 | $rc = new ReflectionClass($className); 45 | if (!$rc->isSubclassOf('TCPDF') && $rc->getName() != 'TCPDF') 46 | { 47 | throw new \LogicException("Class '{$className}' must inherit from TCPDF"); 48 | } 49 | $this->className = $className; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /DependencyInjection/WhiteOctoberTCPDFExtension.php: -------------------------------------------------------------------------------- 1 | processConfiguration($configuration, $configs); 28 | 29 | $container->setParameter('white_october_tcpdf.file', $config['file']); 30 | $container->setParameter('white_october_tcpdf.class', $config['class']); 31 | $container->setParameter('white_october_tcpdf.tcpdf', $config['tcpdf']); 32 | 33 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 34 | $loader->load('services.xml'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /WhiteOctoberTCPDFBundle.php: -------------------------------------------------------------------------------- 1 | container->hasParameter('white_october_tcpdf.tcpdf')) { 17 | return; 18 | } 19 | 20 | // Define our TCPDF variables 21 | $config = $this->container->getParameter('white_october_tcpdf.tcpdf'); 22 | 23 | // TCPDF needs some constants defining if our configuration 24 | // determines we should do so (default true) 25 | // Set tcpdf.k_tcpdf_external_config to false to use the TCPDF 26 | // core defaults 27 | if ($config['k_tcpdf_external_config']) 28 | { 29 | foreach ($config as $k => $v) 30 | { 31 | $constKey = strtoupper($k); 32 | 33 | if (!defined($constKey)) 34 | { 35 | $value = $this->container->getParameterBag()->resolveValue($v); 36 | 37 | // All K_ constants are required 38 | if (preg_match("/^k_/i", $k)) 39 | { 40 | 41 | if (($k === 'k_path_cache' || $k === 'k_path_url_cache') && !is_dir($value)) { 42 | $this->createDir($value); 43 | } 44 | 45 | if(in_array($constKey, ['K_PATH_URL','K_PATH_MAIN','K_PATH_FONTS','K_PATH_CACHE','K_PATH_URL_CACHE','K_PATH_IMAGES'])) { 46 | $value .= (substr($value, -1) == '/' ? '' : '/'); 47 | } 48 | 49 | } 50 | define($constKey, $value); 51 | } 52 | } 53 | } 54 | } 55 | 56 | /** 57 | * Create a directory 58 | * 59 | * @param string $filePath 60 | * 61 | * @throws \RuntimeException 62 | */ 63 | private function createDir($filePath) 64 | { 65 | $filesystem = new Filesystem(); 66 | if (false === $filesystem->mkdir($filePath)) { 67 | throw new \RuntimeException(sprintf( 68 | 'Could not create directory %s', $filePath 69 | )); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | _This project is not actively maintained. For Symfony 3.4 and above, you may wish to use [this fork](https://github.com/Qipsius/QipsiusTCPDFBundle)._ 2 | 3 | WhiteOctoberTCPDFBundle 4 | ======================= 5 | 6 | This bundle facilitates easy use of the TCPDF PDF generation library in 7 | Symfony2 applications. 8 | 9 | Installation 10 | ------------ 11 | 12 | ### Step 1: Setup Bundle and dependencies 13 | ``` 14 | composer require whiteoctober/tcpdf-bundle 15 | ``` 16 | 17 | #### Version constraining (optional) 18 | 19 | By default, this bundle does not constrain the version of TCPDF that composer installs. 20 | ([An explanation of this unusual decision is here](https://github.com/whiteoctober/WhiteOctoberTCPDFBundle/issues/53)). 21 | This means that a `composer update` could update to a new major version of TCPDF. 22 | Since this bundle is only a thin wrapper around TCPDF, you can normally do such an upgrade without issue. 23 | 24 | However, if you do wish to constrain the TCPDF version, find out what version you currently have installed with: 25 | 26 | ```bash 27 | composer show tecnickcom/tcpdf 28 | ``` 29 | 30 | And amend your project's `composer.json` to add a TCPDF version constraint in the `requires` section. 31 | For example, if TCPDF version `6.2.17` was installed, `"tecnickcom/tcpdf": "^6.2.17"` will allow anything < 7 when upgrading. 32 | 33 | ### Step 2: Enable the bundle in the kernel 34 | 35 | Add the bundle to the `registerBundles()` method in your kernel: 36 | 37 | In Symfony < 4: 38 | 39 | ``` php 40 | // app/AppKernel.php 41 | ['all' => true], 59 | // ... 60 | ]; 61 | ``` 62 | 63 | (This project is not yet configured with Symfony Flex, so this change to `config/bundles.php` won't be done automatically.) 64 | 65 | If you want to do service autowiring, you'll need to add an alias for the service: 66 | 67 | ```yaml 68 | # app/config/services.yml (Symfony 3) 69 | # config/services.yaml (Symfony 4) 70 | services: 71 | # ... 72 | 73 | # the `white_october.tcpdf` service will be injected when a 74 | # `WhiteOctober\TCPDFBundle\Controller\TCPDFController` type-hint is detected 75 | WhiteOctober\TCPDFBundle\Controller\TCPDFController: '@white_october.tcpdf' 76 | ``` 77 | 78 | Using TCPDF 79 | ----------- 80 | 81 | You can obtain the `white_october.tcpdf` service from the container, 82 | and then create a new TCPDF object via the service: 83 | 84 | ``` php 85 | $pdfObj = $container->get("white_october.tcpdf")->create(); 86 | ``` 87 | 88 | From hereon in, you are using a TCPDF object to work with as normal. 89 | 90 | Configuration 91 | -------------- 92 | 93 | ### Configuration values 94 | 95 | You can pass parameters to TCPDF like this: 96 | 97 | ```yaml 98 | # app/config/config.yml (Symfony < 4) 99 | # config/packages/white_october_tcpdf.yaml (Symfony 4) 100 | white_october_tcpdf: 101 | tcpdf: 102 | k_title_magnification: 2 103 | ``` 104 | 105 | You can see the default parameter values in 106 | `WhiteOctober\TCPDFBundle\DependencyInjection\Configuration::addTCPDFConfig`. 107 | 108 | If you want, you can use TCPDF's own defaults instead: 109 | 110 | ```yaml 111 | white_october_tcpdf: 112 | tcpdf: 113 | k_tcpdf_external_config: false # the values set by this bundle will be ignored 114 | ``` 115 | 116 | ### Using a custom class 117 | 118 | If you want to use your own custom TCPDF-based class, you can use 119 | the `class` parameter in your configuration: 120 | 121 | ```yaml 122 | # app/config/config.yml (Symfony < 4) 123 | # config/packages/white_october_tcpdf.yaml (Symfony 4) 124 | white_october_tcpdf: 125 | class: 'Acme\MyBundle\MyTCPDFClass' 126 | ``` 127 | 128 | The class must extend from the `TCPDF` class; an exception will be 129 | thrown if this is not the case. 130 | 131 | License 132 | ------- 133 | 134 | This bundle is under the MIT license. See the complete license in the bundle: 135 | 136 | Resources/meta/LICENSE 137 | 138 | _This project was originally located at https://github.com/whiteoctober/WhiteOctoberTCPDFBundle_ 139 | -------------------------------------------------------------------------------- /DependencyInjection/Configuration.php: -------------------------------------------------------------------------------- 1 | getRootNode(); 22 | } else { 23 | $rootNode = $treeBuilder->root('white_october_tcpdf'); 24 | } 25 | 26 | $rootNode 27 | ->children() 28 | ->scalarNode('file')->defaultValue('%kernel.root_dir%/../vendor/tecnickcom/tcpdf/tcpdf.php')->end() 29 | ->scalarNode('class')->defaultValue('TCPDF')->end() 30 | ->end() 31 | ; 32 | 33 | $this->addTCPDFConfig($rootNode); 34 | 35 | return $treeBuilder; 36 | } 37 | 38 | /** 39 | * Adds the core TCPDF configuration 40 | * 41 | * @param $rootNode 42 | */ 43 | protected function addTCPDFConfig(ArrayNodeDefinition $rootNode) 44 | { 45 | $rootNode 46 | ->children() 47 | ->arrayNode('tcpdf') 48 | ->addDefaultsIfNotSet() 49 | ->children() 50 | 51 | // Core configuration values 52 | // These get defined when the TCPDF bundle is booted 53 | ->scalarNode('k_path_url')->defaultValue('%kernel.root_dir%/../vendor/tecnickcom/tcpdf/')->end() 54 | ->scalarNode('k_path_main')->defaultValue('%kernel.root_dir%/../vendor/tecnickcom/tcpdf/')->end() 55 | ->scalarNode('k_path_fonts')->defaultValue('%kernel.root_dir%/../vendor/tecnickcom/tcpdf/fonts/')->end() 56 | ->scalarNode('k_path_cache')->defaultValue('%kernel.cache_dir%/tcpdf')->end() 57 | ->scalarNode('k_path_url_cache')->defaultValue('%kernel.cache_dir%/tcpdf')->end() 58 | ->scalarNode('k_path_images')->defaultValue('%kernel.root_dir%/../vendor/tecnickcom/tcpdf/examples/images/')->end() 59 | ->scalarNode('k_blank_image')->defaultValue('%kernel.root_dir%/../vendor/tecnickcom/tcpdf/examples/images/_blank.png')->end() 60 | ->scalarNode('k_cell_height_ratio')->defaultValue(1.25)->end() 61 | ->scalarNode('k_title_magnification')->defaultValue(1.3)->end() 62 | ->scalarNode('k_small_ratio')->defaultValue(2/3)->end() 63 | ->scalarNode('k_thai_topchars')->defaultTrue()->end() 64 | ->scalarNode('k_tcpdf_calls_in_html')->defaultFalse()->end() 65 | ->scalarNode('k_tcpdf_external_config')->defaultTrue()->end() 66 | ->scalarNode('k_tcpdf_throw_exception_error')->defaultTrue()->end() 67 | 68 | // Optional nice-to-have values 69 | ->scalarNode('head_magnification')->defaultValue(1.1)->end() 70 | ->scalarNode('pdf_page_format')->defaultValue('A4')->end() 71 | ->scalarNode('pdf_page_orientation')->defaultValue('P')->end() 72 | ->scalarNode('pdf_creator')->defaultValue('TCPDF')->end() 73 | ->scalarNode('pdf_author')->defaultValue('TCPDF')->end() 74 | ->scalarNode('pdf_header_title')->defaultValue('')->end() 75 | ->scalarNode('pdf_header_string')->defaultValue('')->end() 76 | ->scalarNode('pdf_header_logo')->defaultValue('')->end() 77 | ->scalarNode('pdf_header_logo_width')->defaultValue('')->end() 78 | ->scalarNode('pdf_unit')->defaultValue('mm')->end() 79 | ->scalarNode('pdf_margin_header')->defaultValue(5)->end() 80 | ->scalarNode('pdf_margin_footer')->defaultValue(10)->end() 81 | ->scalarNode('pdf_margin_top')->defaultValue(27)->end() 82 | ->scalarNode('pdf_margin_bottom')->defaultValue(25)->end() 83 | ->scalarNode('pdf_margin_left')->defaultValue(15)->end() 84 | ->scalarNode('pdf_margin_right')->defaultValue(15)->end() 85 | ->scalarNode('pdf_font_name_main')->defaultValue('helvetica')->end() 86 | ->scalarNode('pdf_font_size_main')->defaultValue(10)->end() 87 | ->scalarNode('pdf_font_name_data')->defaultValue('helvetica')->end() 88 | ->scalarNode('pdf_font_size_data')->defaultValue(8)->end() 89 | ->scalarNode('pdf_font_monospaced')->defaultValue('courier')->end() 90 | ->scalarNode('pdf_image_scale_ratio')->defaultValue(1.25)->end() 91 | ->end() 92 | ->end() 93 | ->end() 94 | ; 95 | } 96 | } 97 | --------------------------------------------------------------------------------