2017 © Pedro Peláez
 

magento2-module magento2-module-pdf

Staempfli PDF generator.

image

staempfli/magento2-module-pdf

Staempfli PDF generator.

  • Thursday, March 22, 2018
  • by mhauri
  • Repository
  • 11 Watchers
  • 34 Stars
  • 1,719 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 7 Versions
  • 11 % Grown

The README.md

Magento 2 PDF generator

Project Status: Abandoned – Initial development has started, but there has not yet been a stable, usable release; the project has been abandoned and the author(s) do not intend on continuing development., (*1)

Magento 2 module to ease the pdf generation using wkhtmltopdf features, (*2)

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:, (*3)

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

Module configuration

Stores > Configuration > Advanced PDF Generation, (*5)

Admin Configuration, (*6)

Usage

This module can generate a PDF from any frontControllerAction, (*7)

  1. Create you Controller path with the correspinding Blocks and .phtml templates, (*8)

    <!-- 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, (*9)

    <?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 html can be added as follows:, (*10)

  1. Create your header.html and footer.html files inside your Module template dir, (*11)

    <!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>, (*12)

  2. Attach those files to the Controller pdf generation, (*13)

    <?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., (*14)

Troubleshooting

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

  • wkhtmltopdf should not be installed via apt-get. See:
    • http://stackoverflow.com/questions/18758589/wkhtmltopdf-installation-error-on-ubuntu

Tiny or very small output on Mac:

  • It seems to be a bug on wkhtmltopdf version 0.12.4. It can be fixed by installing 0.12.3
    • https://stackoverflow.com/questions/40814680/wkhtmltopdf-generates-tiny-output-on-mac

Requirements

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

Support

If you have any issues with this extension, open an issue on GitHub., (*15)

Contribution

Any contribution is highly appreciated. The best way to contribute code is to open a pull request on GitHub., (*16)

Developer

Staempfli Webteam, Fabian Schmengler, integer_net and all other contributors, (*17)

License

Open Software License ("OSL") v. 3.0, (*18)

(c) 2018, Stämpfli AG, (*19)

The Versions

22/03 2018

dev-master

9999999-dev

Staempfli PDF generator.

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

  • mikey179/vfsstream ^1.6

22/03 2018

dev-develop

dev-develop

Staempfli PDF generator.

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

  • mikey179/vfsstream ^1.6

22/03 2018

1.1

1.1.0.0

Staempfli PDF generator.

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

  • mikey179/vfsstream ^1.6

22/03 2018

dev-magento-2-2

dev-magento-2-2

Staempfli PDF generator.

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

  • mikey179/vfsstream ^1.6

06/02 2018

1.0.1

1.0.1.0

Staempfli PDF generator.

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

  • mikey179/vfsstream ^1.6

18/01 2018

1.0

1.0.0.0

Staempfli PDF generator.

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

  • mikey179/vfsstream ^1.6

01/12 2016

v0.1

0.1.0.0

Staempfli PDF generator.

  Sources   Download

OSL-3.0

The Requires

 

The Development Requires

  • mikey179/vfsstream ^1.6