Magento 2 | Как включить стороннюю библиотеку, добавив в "lib/"?
Речь идет не о добавлении композитором. Речь идет о загрузке и последующем использовании. Например: Я загружаю библиотеку TCPDF и выполняю эти шаги, она должна быть установлена и использоваться, но она не работает так, как описано.
Когда нам нужно использовать эту строку?
require_once DIR . '/../../../../../lib/folder/folder/ntlmstream.php';
Какова роль автоматической загрузки?
Спасибо
1 answers
Вот как я понял для библиотеки TCPDF:
Загрузите TCPDF из https://github.com/tecnickcom/tcpdf
Измените имя корневого каталога файла после извлечения на TCPDF
Внутри этого каталога есть tcpdf.php файл, измените имя на TCPDF
Откройте файл и измените имя класса с tcpdf на TCPDF_TCPDF
Скопируйте файлы в вашу папку Magento "lib/internal" в корневой каталог установки Magento.
=>Таким образом, это должно выглядеть так: lib/внутренний/TCPDF
Теперь напишите ниже инструкцию в классе, в котором вы хотите создать объект tcpdf:
require_once('lib/internal/TCPDF/TCPDF.php');
И используйте этот класс как:
$tcpdf = new \TCPDF_TCPDF();
Пример кода:
<?php
namespace Vendor\Module\Controller\Adminhtml\Generatepdf;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
require_once('lib/internal/TCPDF/TCPDF.php');
class CreatePdf extends \Magento\Backend\App\Action {
protected $_dir;
protected $customerName;
protected $order;
public function __construct(
Context $context,
Order $order,
DirectoryList $dir
)
{
$this->order = $order;
$this->_dir = $dir;
parent::__construct($context);
}
public function execute() {
$tcpdf = new \TCPDF_TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$tcpdf->SetCreator(PDF_CREATOR);
$tcpdf->SetTitle('Title');
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
// set default monospaced font
$tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$tcpdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__) . '/lang/eng.php')) {
require_once(dirname(__FILE__) . '/lang/eng.php');
$tcpdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set default font subsetting mode
$tcpdf->setFontSubsetting(true);
//your htmls here
$html=$this->getHtml();
// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$tcpdf->setLanguageArray($lg);
// set font
//dejavusans & freesans For Indian Rupees symbol
$tcpdf->SetFont('freesans', '', 12);
// remove default header/footer
//$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
$tcpdf->AddPage();
$tcpdf->writeHTML($html, true, false, true, false, '');
$tcpdf->lastPage();
//$tcpdf->Output('report_per_route.pdf', 'I');
//$this->logger->debug('report_per_route');
$baseurl = $this->getDirPath();
$filename = $baseurl .'/export/'. 'Sample_pdf'. time().'.pdf';
$tcpdf->Output($filename, 'I');
}
}
?>
Счастливого кодирования:)