2

In my custom module, I have HTML code and I want to convert HTML code into PDF and after conversion download PDF.

Please suggest me Magento-2 default functions or classes for Create PDF and Download.

  • Please help me on this – BornCoder Jan 4 '17 at 12:01
3

Place below code in your module controller for create and download PDF in Magento-2 and for more information follow this link

$pdf = new \Zend_Pdf(); //Create new PDF file

$page = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);

$pdf->pages[] = $page; 

$page->setFont(\Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA), 20);  //Set Font 

$page->drawText('Hello world!', 100, 510); 

$pdfData = $pdf->render(); // Get PDF document as a string 

header("Content-Disposition: inline; filename=result.pdf"); 

header("Content-type: application/x-pdf"); 

echo $pdfData; 
1

I will suggest Dompdf to convert HTML to PDF in magento2. This is an awesome library used for conversion. Follow this link for more information.

  • 1
    Yes , Now i am using Dompdf only and its working fine. Thanks . – BornCoder Jan 12 '17 at 9:39
  • @BornCoder please assist me . how to use with magento2 – Ask Bytes Dec 16 '18 at 13:39
  • @AskBytes : what problem are you facing? – BornCoder Dec 17 '18 at 14:48
  • i want step by step procedures for using Dompdf for invoice printing – Ask Bytes Dec 17 '18 at 15:44
1

You Can also create generate pdf programmatically using below code :

Vendor/Module/registration.php put below code.

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

add the module.xml file in Vendor/Module/etc/module.xml put below code.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0"></module>
</config>

add Controller file in Vendor/Module/Controller/Index/Index.php put below code.

<?php
namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Index extends Action
{
    /**
     * @var \Magento\Framework\App\Response\Http\FileFactory
     */
    protected $fileFactory;

    /**
     * @param Context            $context
     */
    public function __construct(
        Context $context,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory
    ) {
        $this->fileFactory = $fileFactory;
        parent::__construct($context);
    }

    /**
     * to generate pdf
     *
     * @return void
     */
    public function execute()
    {
        $pdf = new \Zend_Pdf();
        $pdf->pages[] = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
        $page = $pdf->pages[0]; // this will get reference to the first page.
        $style = new \Zend_Pdf_Style();
        $style->setLineColor(new \Zend_Pdf_Color_Rgb(0,0,0));
        $font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES);
        $style->setFont($font,15);
        $page->setStyle($style);
        $width = $page->getWidth();
        $hight = $page->getHeight();
        $x = 30;
        $pageTopalign = 850; //default PDF page height
        $this->y = 850 - 100; //print table row from page top – 100px
        //Draw table header row’s
        $style->setFont($font,16);
        $page->setStyle($style);
        $page->drawRectangle(30, $this->y + 10, $page->getWidth()-30, $this->y +70, \Zend_Pdf_Page::SHAPE_DRAW_STROKE);
        $style->setFont($font,15);
        $page->setStyle($style);
        $page->drawText(__("Cutomer Details"), $x + 5, $this->y+50, 'UTF-8');
        $style->setFont($font,11);
        $page->setStyle($style);
        $page->drawText(__("Name : %1", "John Smith"), $x + 5, $this->y+33, 'UTF-8');
        $page->drawText(__("Email : %1","test@example.com"), $x + 5, $this->y+16, 'UTF-8');

        $style->setFont($font,12);
        $page->setStyle($style);
        $page->drawText(__("PRODUCT NAME"), $x + 60, $this->y-10, 'UTF-8');
        $page->drawText(__("PRICE"), $x + 200, $this->y-10, 'UTF-8');
        $page->drawText(__("QUANTITY"), $x + 310, $this->y-10, 'UTF-8');
        $page->drawText(__("TOTAL"), $x + 440, $this->y-10, 'UTF-8');

        $style->setFont($font,10);
        $page->setStyle($style);
        $add = 9;
        $page->drawText("$10.00", $x + 210, $this->y-30, 'UTF-8');
        $page->drawText(5, $x + 330, $this->y-30, 'UTF-8');
        $page->drawText("$50.00", $x + 470, $this->y-30, 'UTF-8');
        $pro = "ABC product";
        $page->drawText($pro, $x + 65, $this->y-30, 'UTF-8');
        $page->drawRectangle(30, $this->y -62, $page->getWidth()-30, $this->y + 10, \Zend_Pdf_Page::SHAPE_DRAW_STROKE);
        $page->drawRectangle(30, $this->y -62, $page->getWidth()-30, $this->y - 100, \Zend_Pdf_Page::SHAPE_DRAW_STROKE);
        $style->setFont($font,15);
        $page->setStyle($style);
        $page->drawText(__("Total : %1", "$50.00"), $x + 435, $this->y-85, 'UTF-8');
        $style->setFont($font,10);
        $page->setStyle($style);
        $page->drawText(__("ABC Footer example"), ($page->getWidth()/2)-50, $this->y-200);

        $fileName = 'example.pdf';

        $this->fileFactory->create(
           $fileName,
           $pdf->render(),
           \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR, // this pdf will be saved in var directory with the name example.pdf
           '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.