8

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:

// PDF INFOS
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;
    (...)
    parent::__construct($context);
}

I then created my method and generate the pdf:

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

    $pdf->AddPage();
    $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):

$this->dataHelper->generatePdf($yourVars);

  • Did you get anywhere with this? – LM_Fielding Jul 6 '16 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 '16 at 10:21
  • 1
    Brilliant. Did you consider dompdf? github.com/dompdf/dompdf – LM_Fielding Jul 6 '16 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 '16 at 11:39
  • Hello, any updates on this question. I am trying to generate PFD for my custom module- Magento2. – Dev Nov 14 '16 at 7:09
1

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();

OR

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.

$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

0

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;
        $this->fileFactory->create(
        $fileName,
        str_replace('/Annot /Subtype /Link', '/Annot /Subtype /Link /Border[0 0 0]', $pdf->render()),
        \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
        'application/pdf'
        );
    }

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.