I would like to generate a custom pdf upon sale and attach it to the sales email all within a custom module.

I think I need to extend Magento\Sales\Model\Order\Pdf\AbstractPdf to generate the pdf but I am not certain.

Has anyone done this before or has any idea if I'm on the right track?

// edit:

As per the comments, here is a basic layout as to how I included http://www.fpdf.org/ into my custom module:

First I downloaded the fpdf package. I extracted the files and placed them all into my Modules Helper folder, which now looks like this:

- Helper -- font (folder) -- makefont (folder) -- FPDF.php -- Data.php

Data.php is the only file I already had in this folder as it contains my helper Data class.

Next I edited the FPDF.php and added my modules namespace to it.

I edited my helper Data.php file and use my namespaces FPDF class:

use Company\ModuleName\Helper\FPDF as FPDF;

I defined any pdf infos I need in the data Class - you do not need this:

protected $pdfFileDir = 'someDir/someSubDir/';
protected $logo1 = 'myLogo.jpg';
protected $logo2 = 'myExtraLogo.jpg';

 * @var \Company\ModuleName\Helper\FPDF
protected $_pdf;

and construct it

 * @param \Company\ModuleName\Helper\FPDF $pdf
 * @param array $data
public function __construct(
    FPDF $pdf,
    array $data = []
) {
    $this->_pdf = $pdf;

I then created my method and generate the pdf:

public function generatePdf($productId, $someOtherVars)
    #start the pdf
    $pdf = new FPDF();

    $pdf->SetAutoPageBreak(0, 5);
    $pdf->SetFont('Arial', 'B', 16);

    // do your FPDF stuff here
    // eg: $pdf->Cell('', '', iconv('UTF-8', 'windows-1252', 'Page Header Title'), 0, 1);

    // $pdf->Ln(8);
    // $pdf->Cell('', '', $today, 0, 1, 'L');
    // and so forth .. see the FPDF documentation

    // render pdf
    $filename = 'somename' . $productId . '.pdf';
    $pdf->Output($this->getPdfBaseDir() . $filename, 'F');

    return $this->getPdfBaseDir() . $filename;

Then you call your method in your controller via your helper (which you need to include of course):


  • Did you get anywhere with this? Jul 6, 2016 at 9:41
  • 1
    I didn't go that route after all. I tried but in the end it seemed too complex to try and use the AbstractPdf class. I ended up including FPDF with my module (in the Helper) and used it to generate the pdf files. fpdf.org
    – tecjam
    Jul 6, 2016 at 10:21
  • 1
    Brilliant. Did you consider dompdf? github.com/dompdf/dompdf Jul 6, 2016 at 10:25
  • 1
    I did, but one reason as to why I didn't use it was that I didn't want to have to render the html first and then have to render the html into the pdf. There were other reasons too, but I can not recall what they were. For your use it may be better, but only you can decide that.
    – tecjam
    Jul 6, 2016 at 11:39
  • Hello, any updates on this question. I am trying to generate PFD for my custom module- Magento2.
    – Dev
    Nov 14, 2016 at 7:09

4 Answers 4


You can use FPDF or TCPDF for creating PDF.

Try to run one of the following command from M2 root directory

composer require setasign/fpdf:1.8.1

It will add fpdf library in vendor folder as setasign/fpdf. Once fpdf install you can use FPDF in you custom module for creating PDF.

$pdf= new \FPDF();


composer require tecnickcom/tcpdf

It will add tcpdf library in vendor folder as tecnickcom/tcpdf. Once tcpdf install you can use TCPDF in you custom module for creating PDF.



Magento2: How to create a PDF in a custom module

public function execute()
    $file = $this->getRequest()->getParam('file');

    if(isset($file) && !empty($file)) {

        $pdf = \Zend_Pdf::load($this->directory_list->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . $file);
        $fileName = test.pdf;
        str_replace('/Annot /Subtype /Link', '/Annot /Subtype /Link /Border[0 0 0]', $pdf->render()),

You Can Check Below Link






namespace VendoreName\RmaPdfPrint\Controller\Rma;

use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Registry;
use VendoreName\Rma\Api\Repository\RmaRepositoryInterface;
use VendoreName\Rma\Api\Service\Rma\RmaManagement\SaveInterface;
use VendoreName\Rma\Helper\Controller\Rma\GuestStrategy;
use VendoreName\Rma\Helper\Controller\Rma\StrategyFactory;
use VendoreName\Rma\Service\Config\FrontendConfig;
use VendoreName\RmaPdfPrint\Block\DownloadRmaPdf;

class PrintAction extends \Magento\Framework\App\Action\Action;
    protected $rmaData;

    private $guestStrategy;
    private $rmaRepository;
    private $rmaSaveManagement;
    private $registry;

    public function __construct(
        Context $context,
        GuestStrategy $guestStrategy,
        RmaRepositoryInterface $rmaRepository,
        SaveInterface $rmaSaveManagement,
        Registry $registry,
        StrategyFactory $strategyFactory,
        Session $customerSession,
        FrontendConfig $frontendConfig,
        DownloadRmaPdf $rmaData
    ) {
        $this->guestStrategy     = $guestStrategy;
        $this->rmaRepository     = $rmaRepository;
        $this->rmaSaveManagement = $rmaSaveManagement;
        $this->registry          = $registry;
        $this->rmaData           = $rmaData;

    public function execute()
        $rma = $this->getRequest()->getPost();
        $this->registry->register('current_rma', $rma);
        $pdf = $this->rmaData->getPdf($rma); // Here We set RMA Data
        $pdfData = $pdf->render();

        header('Content-type: application/pdf');
        header('Content-Disposition: inline; filename=result.pdf');
        header('Content-Transfer-Encoding: binary');
        header('Accept-Ranges: bytes');

        echo $pdfData;

Must Open link whole code are in the file


I think the answer can be found in profile articles on this topic such as these: https://ecomitize.com/category/magento-guides/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.