Magento 2 Module for creating PDF's based on wkhtmltopdf
Clone or download
mhauri Merge pull request #5 from staempfli/magento-2-2
Compatibility to Magento 2.2
Latest commit 97576c4 Mar 22, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Adapter BDK-69: Docblocks and comments Nov 29, 2016
Api
Block edit grumphp, add Jenkinsfile and fix phpcs and phpmd Jun 8, 2017
Model Change interface for response Mar 22, 2018
Service edit grumphp, add Jenkinsfile and fix phpcs and phpmd Jun 8, 2017
Test edit grumphp, add Jenkinsfile and fix phpcs and phpmd Jun 8, 2017
docs/img Update inherited class declaration to Magento 2.2 Mar 22, 2018
etc
Jenkinsfile Update inherited class declaration to Magento 2.2 Mar 22, 2018
README.md
composer.json Declare compatibility to Magento 2.2 Mar 22, 2018
grumphp.yml
phpunit.xml.dist BDK-69 Implement domain objects Nov 9, 2016
registration.php Update inherited class declaration to Magento 2.2 Mar 22, 2018

README.md

Magento 2 PDF generator

Magento 2 module to ease the pdf generation using wkhtmltopdf features

Installation

composer require "staempfli/magento2-module-pdf":"~1.0"

Setup

Install wkhtmltopdf

This module needs wkhtmltopdf installed on your computer. You can download and install it from here:

NOTE: Do not install it using apt-get on Linux systems. See troubleshooting section for more info.

Module configuration

Stores > Configuration > Advanced PDF Generation

Admin Configuration

Usage

This module can generate a PDF from any frontControllerAction

  1. Create you Controller path with the correspinding Blocks and .phtml templates

    <!-- route_actions_generatePdf.xml -->
    <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
       <container name="root">
          <block class="Vendor\Package\Block\YourBlock" name="vendor.package.somename">
      </container>
    </layout>
    
  2. Return pdf instance on Controller

    <?php
    namespace Vendor\Package\Controller\Actions;
    
    use Magento\Framework\App\Action\Action;
    use Staempfli\Pdf\Model\View\PdfResult;
    use Staempfli\Pdf\Service\PdfOptions;
    
    class GeneratePdf extends Action
    {
    	public function execute()
    	{
        	return $this->renderPdfResult();
    	}
    
    	protected function renderPdfResult()
    	{
        	/** @var PdfResult $result */
        	$result = $this->resultFactory->create(PdfResult::TYPE);
        	$result->addGlobalOptions(
          	  new PdfOptions(
             	   [
             	       PdfOptions::KEY_GLOBAL_TITLE => __('Return PDF'),
             	       PdfOptions::KEY_PAGE_ENCODING => PdfOptions::ENCODING_UTF_8,
             	       PdfOptions::KEY_GLOBAL_ORIENTATION => PdfOptions::ORIENTATION_PORTRAIT,
             	       PdfOptions::FLAG_PAGE_PRINT_MEDIA_TYPE,
             	   ]
            	)
        	);
        	$result->addPageOptions(
          	  new PdfOptions(
             	   [
             	       PdfOptions::KEY_PAGE_COOKIES => ${'_COOKIE'},
             	   ]
            	)
        	);
        	return $result;
    	}
    }
    

Header and Footer

Header and footer html can be added as follows:

  1. Create your header.html and footer.html files inside your Module template dir

    <!doctype html>
    <!-- app/code/Vendor/Package/view/frontend/templates/pdf/header.html -->
    <html>
    	<body>
    		Header text
        	</body>
    </html>
    
    <!doctype html>
    <!-- app/code/Vendor/Package/view/frontend/templates/pdf/footer.html -->
    <html>
    		<body>
    		Footer text
    		</body>
    </html>
    
  2. Attach those files to the Controller pdf generation

    <?php
    namespace Vendor\Package\Controller\Actions;
    
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\View\Element\Template\File\Resolver as TemplateResolver;
    use Staempfli\Pdf\Model\View\PdfResult;
    use Staempfli\Pdf\Service\PdfOptions;
    
    class GeneratePdf extends Action
    {
        /**
     	     * @var TemplateResolver
    	     */
    	    private $templateResolver;
    
    	    public function __construct(
        	TemplateResolver $templateResolver,
        	Context $context
    	    ) {
       	        parent::__construct($context);
    	$this->templateResolver = $templateResolver;
    	    }
    
            public function execute()
    	    {
                return $this->renderPdfResult();
    	    }
    
    	    protected function renderPdfResult()
    	    {
        	/** @var PdfResult $result */
        	$result = $this->resultFactory->create(PdfResult::TYPE);
        	$result->addGlobalOptions(
          	  new PdfOptions(
             	   [
             	       PdfOptions::KEY_GLOBAL_TITLE => __('Return PDF'),
             	       PdfOptions::KEY_PAGE_ENCODING => PdfOptions::ENCODING_UTF_8,
             	       PdfOptions::KEY_GLOBAL_ORIENTATION => PdfOptions::ORIENTATION_PORTRAIT,
             	       PdfOptions::FLAG_PAGE_PRINT_MEDIA_TYPE,
             	       PdfOptions::KEY_PAGE_HEADER_SPACING => "10",
             	   ]
            	)
        	);
        	$result->addPageOptions(
          	  new PdfOptions(
             	   [
             	       PdfOptions::KEY_PAGE_COOKIES => ${'_COOKIE'},
             	       PdfOptions::KEY_PAGE_HEADER_HTML_URL => $this->templateResolver
                        ->getTemplateFileName('Vendor_Package::pdf/header.html'),
                    PdfOptions::KEY_PAGE_FOOTER_HTML_URL => $this->templateResolver
                        ->getTemplateFileName('Vendor_ Package::pdf/footer.html'),
             	   ]
            	)
        	);
        	return $result;
    	    }
    }
    

NOTE: Only the header or only the footer as html is not possible, either both or none. Otherwise top and bottom margins are ignored.

Troubleshooting

The switch --print-media, is not support using unpatched qt:

Tiny or very small output on Mac:

Requirements

  • PHP >= 7.0.*
  • Magento >= 2.1.*

Support

If you have any issues with this extension, open an issue on GitHub.

Contribution

Any contribution is highly appreciated. The best way to contribute code is to open a pull request on GitHub.

Developer

Staempfli Webteam, Fabian Schmengler, integer_net and all other contributors

License

Open Software License ("OSL") v. 3.0

Copyright

(c) 2018, Stämpfli AG