Try With MPDF Code
<?php
namespace VendoreName\ModuleName\Controller\Index;
use Magento\Framework\App\Action\Action;
use Zend_Loader;
class GetPdfData extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @var \Magento\Framework\Filesystem\DirectoryList
*/
protected $dir;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Filesystem\DirectoryList $dir,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->dir = $dir;
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
$this->initializePDF();
}
/**
* @inheritdoc
*/
public function execute()
{
$pdfData = [];
$pdfData["body_data"] = "<h1><b>TEST Data</b></h1>";
$config = [
'mode' => 'utf-8',
'format' => 'A4',
'default_font_size' => 10,
'default_font' => "dejavusans",
'margin_left' => 1.5,
'margin_right' => 1.5,
'margin_top' => 1.5,
'margin_bottom' => 1.5,
'allow_charset_conversion' => true,
'orientation' => 'P',
'showBarcodeNumbers' => false,
'tempDir' => $this->dir->getPath('var'),
];
$pdf = new \Mpdf\Mpdf($config);
$pdf->WriteHTML(html_entity_decode($pdfData["body_data"]));
$outputData = $pdf->Output('test.pdf', 'I'); // D , F , S
}
/**
* Initialization of mpdf library
*/
protected function initializePDF()
{
error_reporting(0);
$path = $this->dir->getRoot() . "/vendor/mpdf/mpdf/src/";
$this->pdf = $path . 'Mpdf.php';
//include_once $this->pdf;
Zend_Loader::loadFile($this->pdf, null, true);
$this->config = $path . 'Config/ConfigVariables.php';
// require_once $this->config;
Zend_Loader::loadFile($this->config, null, true);
$this->ucdn = $path . 'Ucdn.php';
// require_once $this->ucdn;
Zend_Loader::loadFile($this->ucdn, null, true);
$this->defaultCss = $path . 'Css/DefaultCss.php';
// require_once $this->defaultCss;
Zend_Loader::loadFile($this->defaultCss, null, true);
$this->sizeConvertor = $path . 'SizeConverter.php';
// require_once $this->sizeConvertor;
Zend_Loader::loadFile($this->sizeConvertor, null, true);
$this->colorconverter = $path . 'Color/ColorConverter.php';
// require_once $this->colorconverter;
Zend_Loader::loadFile($this->colorconverter, null, true);
$this->gradient = $path . 'Gradient.php';
// require_once $this->gradient;
Zend_Loader::loadFile($this->gradient, null, true);
$this->tableOfContents = $path . 'TableOfContents.php';
// require_once $this->tableOfContents;
Zend_Loader::loadFile($this->tableOfContents, null, true);
$this->cache = $path . 'Cache.php';
// require_once $this->cache;
Zend_Loader::loadFile($this->cache, null, true);
$this->fontCache = $path . 'Fonts/FontCache.php';
// require_once $this->fontCache;
Zend_Loader::loadFile($this->fontCache, null, true);
$this->fontsFileFinder = $path . 'Fonts/FontFileFinder.php';
// require_once $this->fontsFileFinder;
Zend_Loader::loadFile($this->fontsFileFinder, null, true);
$this->cssManager = $path . 'CssManager.php';
// require_once $this->cssManager;
Zend_Loader::loadFile($this->cssManager, null, true);
$this->otl = $path . 'Otl.php';
// require_once $this->otl;
Zend_Loader::loadFile($this->otl, null, true);
$this->form = $path . 'Form.php';
// require_once $this->form;
Zend_Loader::loadFile($this->form, null, true);
$this->hyphenator = $path . 'Hyphenator.php';
// require_once $this->hyphenator;
Zend_Loader::loadFile($this->hyphenator, null, true);
$this->tag = $path . 'Tag.php';
// require_once $this->tag;
Zend_Loader::loadFile($this->tag, null, true);
$this->namedcolors = $path . 'Color/NamedColors.php';
// require_once $this->namedcolors;
Zend_Loader::loadFile($this->namedcolors, null, true);
$this->pageformat = $path . 'PageFormat.php';
// require_once $this->pageformat;
Zend_Loader::loadFile($this->pageformat, null, true);
$this->fontVariables = $path . 'Config/FontVariables.php';
// require_once $this->fontVariables;
Zend_Loader::loadFile($this->fontVariables, null, true);
$this->textvars = $path . 'Css/TextVars.php';
// require_once $this->textvars;
Zend_Loader::loadFile($this->textvars, null, true);
$this->border = $path . 'Css/Border.php';
// require_once $this->border;
Zend_Loader::loadFile($this->border, null, true);
$this->logcontext = $path . 'Log/Context.php';
// require_once $this->logcontext;
Zend_Loader::loadFile($this->logcontext, null, true);
$this->ttfontfile = $path . 'TTFontFile.php';
// require_once $this->ttfontfile;
Zend_Loader::loadFile($this->ttfontfile, null, true);
$this->destination = $path . 'Output/Destination.php';
// require_once $this->destination;
Zend_Loader::loadFile($this->destination, null, true);
$this->mpdfexception = $path . 'MpdfException.php';
// require_once $this->mpdfexception;
Zend_Loader::loadFile($this->mpdfexception, null, true);
$this->metricsgenerator = $path . 'Fonts/MetricsGenerator.php';
// require_once $this->metricsgenerator;
Zend_Loader::loadFile($this->metricsgenerator, null, true);
$this->GlyphOperator = $path . 'Fonts/GlyphOperator.php';
// require_once $this->GlyphOperator;
Zend_Loader::loadFile($this->GlyphOperator, null, true);
$this->barcode = $path . 'Barcode.php';
// require_once $this->barcode;
Zend_Loader::loadFile($this->barcode, null, true);
}
}
Note: MPdf is installing via composer require mpdf/mpdf command which placed into the vendor directory.