Magento 2 Module for creating PDF's based on wkhtmltopdf

Overview

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.

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

You might also like...
This package will generate PDFs and PNGs from websites.

This package will generate PDFs and PNGs from websites.

Generate PDFs in Laravel with Mpdf.

Laravel Mpdf: Using Mpdf in Laravel for generate Pdfs Easily generate PDF documents from HTML right inside of Laravel using this mpdf wrapper. Importa

Convert image types. Take samples of pdfs or psd, do some filters as SEPIA, resizes. Save to WEBP
Convert image types. Take samples of pdfs or psd, do some filters as SEPIA, resizes. Save to WEBP

img2img. PHP Converter, sampler of pdfs & psd, do resizes & some filters in images as SEPIA. Save to WEBP V.1.0.3 This class in pure PHP try to manage

Programmatically create and render barcodes as images or in PDFs

laminas-barcode 🇷🇺 Русским гражданам Мы, участники Laminas, родились и живем в разных странах. У многих из нас есть друзья, родственники и коллеги к

PDF API. JSON to PDF. PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data
PDF API. JSON to PDF. PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data

PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data PDF ENGINE VERSION: development: This is a prerelease version

A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.
A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

In order to use the Korean Language on your Magento 2 store, it is time to start with Magento 2 Korean Language Pack in the set of informative documentations by Mageplaza. Magento 2 Korean Language Package is published by Magento 2 Translation Project at Crowdin, so all phrases will be replaced by the Korean language according to the contribution to that project. Please following up the guides in this post to convert the language with ease!
Magento 2 Module Experius Page Not Found 404. This module saves all 404 url to a database table

Magento 2 Module Experius Page Not Found 404 This module saves all 404 urls to a database table. Adds an admin grid with 404s It includes a count so y

The Laravel eCommerce DHL Shipping module module calculates the shipping rates based on DHL API for product shipping.

Introduction DHL Shipping Add-on provides DHL Shipping methods for shipping the product. It packs in lots of demanding features that allows your busin

A Magento 2 module that adds a CLI bin/magento cms:dump to dump all CMS pages and CMS blocks to a folder var/cms-output.

A Magento 2 module that adds a CLI bin/magento cms:dump to dump all CMS pages and CMS blocks to a folder var/cms-output.

Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 1.9.x and earlier.

Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 1.9.x and earlier.

Mobile detect change theme and redirect based on device type. Magento 2 module.

Magento 2 Mobile Detect Theme Change Magento 2 Mobile detect system can be used to load different themes base on the client device (desktop, tablet, m

Magento 2 Italian Language Pack is special gift for Magento 2 Community Edition. The data of the translation is supplied by Magento 2 Translation Project of Crowdin site, on which you will contribute into the project and download the zip file to install on your own store. This knowledge base will include full of the clear guides that supports you to convert the default language (English) into Italian language on your entire store.
A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2

Simple Import / Export tool A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. Table data

This Magento extension provides a Real Full Page Caching for Magento powered by Varnish with support of Session-Based information caching (Cart, Customer Accounts, ...) via ESI includes

This Magento extension provides a Real Full Page Caching (FPC) for Magento powered by Varnish with support of Session-Based information caching (Cart, Customer Accounts, ...) via ESI includes

AbanteCart is a free PHP based eCommerce solution for merchants to provide ability creating online business and sell products online quick and efficient.

AbanteCart is a free PHP based eCommerce solution for merchants to provide ability creating online business and sell products online quick and efficient. AbanteCart application is built and supported by experienced enthusiasts that are passionate about their work and contribution to rapidly evolving eCommerce industry. AbanteCart is more than just a shopping cart, it is rapidly growing eCommerce platform

A Magento 1.9's module to export products informations, inside a chosen date range, to a .XLSX file.
A Magento 1.9's module to export products informations, inside a chosen date range, to a .XLSX file.

Magento 1.9 Export products module A magento 1.9 module to export products informations to a .XLSX file. Module informations Package/Namespace: "Mathe

Magento 2.4.x module Sort Out Of Stock Product At last the product list

Magento 2.4.x module Sort Out Of Stock Product At last the product list composer require ghoster/module-outofstockatlast Extension on GitHub Direct d

Magento 2 module to only allow checkout when the number of items in the cart are a multiple of X.

Cart Quantity Multiple - Magento 2 Module Introduction This module allows to limit checkout only when the contents of the cart are a multiple of X

Comments
  • Can't generate a pdf

    Can't generate a pdf

    I am using Magento 2.1.3 and running in Ubuntu.

    The module can't generate the pdf after installation, I got the following error

     Failed without error message: /usr/bin/wkhtmltopdf.sh '/tmp/tmp_wkhtmlto_pdf_Mau6Ub.html' '/var/www/html/src/var/tmp/tmp_wkhtmlto_pdf_qezFwP.pdf'
    #0 /var/www/html/src/vendor/staempfli/magento2-module-pdf/Model/View/PdfResult.php(109): Staempfli\Pdf\Adapter\WkPdfFile->toString()
    #1 /var/www/html/src/vendor/staempfli/magento2-module-pdf/Model/View/PdfResult.php(74): Staempfli\Pdf\Model\View\PdfResult->renderPdf()
    #2 /var/www/html/src/vendor/magento/framework/Controller/AbstractResult.php(124): Staempfli\Pdf\Model\View\PdfResult->render(Object(Magento\Framework\App\Response\Http\Interceptor))
    #3 /var/www/html/src/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\Controller\AbstractResult->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
    #4 /var/www/html/src/vendor/magento/framework/Interception/Chain/Chain.php(70): Staempfli\Pdf\Model\View\PdfResult\Interceptor->___callParent('renderResult', Array)
    #5 /var/www/html/src/vendor/magento/framework/Interception/Chain/Chain.php(63): Magento\Framework\Interception\Chain\Chain->invokeNext('Staempfli\\Pdf\\M...', 'renderResult', Object(Staempfli\Pdf\Model\View\PdfResult\Interceptor), Array, 'result-varnish-...')
    #6 /var/www/html/src/vendor/magento/module-page-cache/Model/Controller/Result/VarnishPlugin.php(74): Magento\Framework\Interception\Chain\Chain->Magento\Framework\Interception\Chain\{closure}(Object(Magento\Framework\App\Response\Http\Interceptor))
    #7 /var/www/html/src/vendor/magento/framework/Interception/Chain/Chain.php(67): Magento\PageCache\Model\Controller\Result\VarnishPlugin->aroundRenderResult(Object(Staempfli\Pdf\Model\View\PdfResult\Interceptor), Object(Closure), Object(Magento\Framework\App\Response\Http\Interceptor))
    #8 /var/www/html/src/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Staempfli\\Pdf\\M...', 'renderResult', Object(Staempfli\Pdf\Model\View\PdfResult\Interceptor), Array, 'result-builtin-...')
    #9 /var/www/html/src/vendor/magento/module-page-cache/Model/Controller/Result/BuiltinPlugin.php(67): Staempfli\Pdf\Model\View\PdfResult\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Response\Http\Interceptor))
    #10 /var/www/html/src/vendor/magento/framework/Interception/Interceptor.php(142): Magento\PageCache\Model\Controller\Result\BuiltinPlugin->aroundRenderResult(Object(Staempfli\Pdf\Model\View\PdfResult\Interceptor), Object(Closure), Object(Magento\Framework\App\Response\Http\Interceptor))
    #11 /var/www/html/src/var/generation/Staempfli/Pdf/Model/View/PdfResult/Interceptor.php(26): Staempfli\Pdf\Model\View\PdfResult\Interceptor->___callPlugins('renderResult', Array, Array)
    #12 /var/www/html/src/vendor/magento/framework/App/Http.php(139): Staempfli\Pdf\Model\View\PdfResult\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
    #13 /var/www/html/src/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
    #14 /var/www/html/src/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
    #15 {main}
    

    During the genreate process, the module didn't generate the /tmp/tmp_wkhtmlto_pdf_Mau6Ub.html for wkhtmltopdf to convert to pdf file.

    bug 
    opened by gumplei 2
  • Fatal error Issue

    Fatal error Issue

    Hi

    I have faced this issue after I install the extension I am using Magento 2.1 PHP 7.0 Ubuntu server

    Fatal error: Declaration of Staempfli\Pdf\Model\View\PdfResult::render(Magento\Framework\App\Response\HttpInterface $response) must be compatible with Magento\Framework\Controller\AbstractResult::render(Magento\Framework\App\ResponseInterface $response) in /srv/users/sumayah/apps/magento/public/magento2/vendor/staempfli/magento2-module-pdf/Model/View/PdfResult.php on line 16

    How can I fix the issue?

    thanks

    screen shot 2018-10-18 at 1 23 40 am screen shot 2018-10-18 at 1 24 03 am

    opened by Sumayah90 0
  • Compatibility to Magento 2.2

    Compatibility to Magento 2.2

    The response needs a different interface with some added methods now which have been implemented as dummies. This should be compatible to Magento 2.1, but I haven't tested it.

    opened by avstudnitz 0
  • Exception #0 (Staempfli\Pdf\Adapter\WkPdfException): sh: 1: /usr/bin/xvfb-run: not found

    Exception #0 (Staempfli\Pdf\Adapter\WkPdfException): sh: 1: /usr/bin/xvfb-run: not found

    Hi there, I'm using the following System in a docker container:

    Distributor ID: Ubuntu
    Description: Ubuntu 18.04.3 LTS
    Release: 18.04
    Codename: bionic

    We have installed on the machine a magento application to produce PDF documents.

    However, we are getting the folling error regardless of the confuguration information. Exception #0 (Staempfli\Pdf\Adapter\WkPdfException): sh: 1: /usr/bin/xvfb-run: not found Exception #0 (Staempfli\Pdf\Adapter\WkPdfException): sh: 1: /usr/bin/wkhtmltopdf: not found

    wkhtmltox is already installed on the system and it's working from the bash correctly.

    What I'm missing? The paths to the exec file are all correct.

    Thanks in Advance.

    opened by lmuestak 0
Releases(v0.1)
Owner
Stämpfli AG
Web solutions for associations, small businesses and more.
Stämpfli AG
A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

PHP WkHtmlToPdf PHP WkHtmlToPdf provides a simple and clean interface to ease PDF and image creation with wkhtmltopdf. The wkhtmltopdf and - optionall

Michael Härtl 1.5k Dec 25, 2022
This package will generate PDFs and PNGs from websites.

This package will generate PDFs and PNGs from websites.

Appoly 0 Dec 3, 2021
Generate PDFs in Laravel with Mpdf.

Laravel Mpdf: Using Mpdf in Laravel for generate Pdfs Easily generate PDF documents from HTML right inside of Laravel using this mpdf wrapper. Importa

Carlos Meneses 264 Jan 4, 2023
Magento 2 Invoice PDF Generator - helps you to customize the pdf templates for Magento 2

Magento 2 Invoice PDF Generator - helps you to customize the pdf templates for Magento 2. If you have an enabled template and a default template for the store you need your template the system will print the pdf template.

EAdesign 64 Oct 18, 2021
A Laravel package for creating PDF files using LaTeX

LaraTeX A laravel package to generate PDFs using LaTeX · Report Bug · Request Feature For better visualization you can find a small Demo and the HTML

Ismael Wismann 67 Dec 28, 2022
Offers tools for creating pdf files.

baldeweg/pdf-bundle Offers tools for creating pdf files. Getting Started composer req baldeweg/pdf-bundle Activate the bundle in your config/bundles.p

André Baldeweg 0 Oct 13, 2022
DOMPDF module for Laravel 5

pdf-laravel5 DOMPDF module for Laravel 5. Export your views as PDFs - with css support. Instalation Add: "vsmoraes/laravel-pdf": "^2.0" To your compo

Vinicius Moraes 88 Jul 25, 2022
A PDF conversion and form utility based on pdftk

php-pdftk A PDF conversion and form utility based on pdftk. Features php-pdftk brings the full power of pdftk to PHP - and more. Fill forms, either fr

Michael Härtl 800 Jan 9, 2023
PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage

Snappy Snappy is a PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent webkit-based wkhtmltopd

KNP Labs 4.1k Dec 30, 2022
A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

PHP WkHtmlToPdf PHP WkHtmlToPdf provides a simple and clean interface to ease PDF and image creation with wkhtmltopdf. The wkhtmltopdf and - optionall

Michael Härtl 1.5k Dec 25, 2022