2017 © Pedro Peláez
 

magento2-module magento2-module-dompdf

Result class for rendering a PDF using Dompdf

image

weprovide/magento2-module-dompdf

Result class for rendering a PDF using Dompdf

  • Wednesday, April 18, 2018
  • by timneutkens
  • Repository
  • 0 Watchers
  • 7 Stars
  • 5,497 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 7 Versions
  • 37 % Grown

The README.md

Magento2 Module Dompdf

Result class for rendering a PDF using Dompdf, (*1)

Installation

  1. composer require weprovide/magento2-module-dompdf
  2. bin/magento setup:upgrade

Usage

Render using plain string

<?php
namespace YourNameSpace\YourModule\Controller\Download;

use Magento\Framework\App\Action\Context;
use WeProvide\Dompdf\Controller\Result\DompdfFactory;
use WeProvide\Dompdf\Controller\Result\Dompdf;

class Pdf extends \Magento\Framework\App\Action\Action
{
    protected $dompdfFactory;
    protected $layoutFactory;

    /**
     * Constructor
     *
     * @param Context $context
     * @param DompdfFactory $dompdfFactory
     *
     */
    public function __construct(
        Context $context,
        DompdfFactory $dompdfFactory
    ) {
        $this->dompdfFactory = $dompdfFactory;
        parent::__construct($context);
    }

    public function getHtmlForPdf() {
        return '
        <html>
            <body>
                <h1>Hello world</h1>
            </body>
        </html>';
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        /** @var Dompdf $response */
        $response = $this->dompdfFactory->create();
        $response->setData($this->getHtmlForPdf());

        return $response;
    }
}

Render using block and template

Create controller, (*2)

<?php
namespace YourNameSpace\YourModule\Controller\Download;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\LayoutFactory;
use WeProvide\Dompdf\Controller\Result\Dompdf;
use WeProvide\Dompdf\Controller\Result\DompdfFactory;

class Pdf extends \Magento\Framework\App\Action\Action
{
    protected $dompdfFactory;
    protected $layoutFactory;

    /**
     * Constructor
     *
     * @param Context $context
     * @param DompdfFactory $dompdfFactory
     *
     */
    public function __construct(
        Context $context,
        DompdfFactory $dompdfFactory,
        LayoutFactory $layoutFactory
    ) {
        $this->dompdfFactory = $dompdfFactory;
        $this->layoutFactory = $layoutFactory;
        parent::__construct($context);
    }

    public function getHtmlForPdf()
    {
        /** @var \Magento\Framework\View\Element\Template $block */
        $block = $this->layoutFactory->create()->createBlock('Magento\Framework\View\Element\Template');
        $block->setTemplate('YourNameSpace_YourModule::pdf.phtml');

        $data = [
            'foo' => 'bar'
        ];
        $block->setData($data);

        return $block->toHtml();
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        /** @var Dompdf $response */
        $response = $this->dompdfFactory->create();
        $response->setData($this->getHtmlForPdf());

        return $response;
    }
}

Create template file in YourNameSpace/YourModule/view/frontend/template, (*3)

Api

All public methods are listed below:, (*4)

public $pdf => Dompdf instance. Useful for setting custom options like orientation and paper size. List of all options can be found here., (*5)

public function setData($html) => Equal to $dompdf->loadHtml($html)., (*6)

public function setFileName($fileName) => Set output fileName., (*7)

public function setAttachment($mode) => Mode can either be attachment (download) or inline (no download)., (*8)

public function renderOutput() => Equal to running $dompdf->render() & $dompdf->output(). Will cache the output since Dompdf doesn't support rendering twice, (*9)

For reference also check the code, (*10)

The Versions

18/04 2018

dev-master

9999999-dev

Result class for rendering a PDF using Dompdf

  Sources   Download

MIT

The Requires

 

by Tim Neutkens

18/04 2018

1.2.1

1.2.1.0

Result class for rendering a PDF using Dompdf

  Sources   Download

MIT

The Requires

 

by Tim Neutkens

06/12 2017

1.2.0

1.2.0.0

Result class for rendering a PDF using Dompdf

  Sources   Download

MIT

The Requires

 

by Tim Neutkens

13/06 2017

1.1.1

1.1.1.0

Result class for rendering a PDF using Dompdf

  Sources   Download

MIT

The Requires

 

by Tim Neutkens

08/03 2017

1.1.0

1.1.0.0

Result class for rendering a PDF using Dompdf

  Sources   Download

MIT

The Requires

 

by Tim Neutkens

05/12 2016

1.0.1

1.0.1.0

Result class for rendering a PDF using Dompdf

  Sources   Download

MIT

The Requires

 

by Tim Neutkens

29/11 2016

1.0.0

1.0.0.0

Result class for rendering a PDF using Dompdf

  Sources   Download

MIT

The Requires

 

by Tim Neutkens