PHP code example of staempfli / magento2-module-pdf
1. Go to this page and download the library: Download staempfli/magento2-module-pdf library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
staempfli / magento2-module-pdf example snippets
namespace Vendor\Package\Controller\Actions;
use Magento\Framework\App\Action\Action;
use Staempfli\Pdf\Model\View\PdfResult;
use Staempfli\Pdf\Service\PdfOptions;
class GeneratePdf extends Action
{
public function execute()
{
return $this->renderPdfResult();
}
protected function renderPdfResult()
{
/** @var PdfResult $result */
$result = $this->resultFactory->create(PdfResult::TYPE);
$result->addGlobalOptions(
new PdfOptions(
[
PdfOptions::KEY_GLOBAL_TITLE => __('Return PDF'),
PdfOptions::KEY_PAGE_ENCODING => PdfOptions::ENCODING_UTF_8,
PdfOptions::KEY_GLOBAL_ORIENTATION => PdfOptions::ORIENTATION_PORTRAIT,
PdfOptions::FLAG_PAGE_PRINT_MEDIA_TYPE,
]
)
);
$result->addPageOptions(
new PdfOptions(
[
PdfOptions::KEY_PAGE_COOKIES => ${'_COOKIE'},
]
)
);
return $result;
}
}
namespace Vendor\Package\Controller\Actions;
use Magento\Framework\App\Action\Action;
use Magento\Framework\View\Element\Template\File\Resolver as TemplateResolver;
use Staempfli\Pdf\Model\View\PdfResult;
use Staempfli\Pdf\Service\PdfOptions;
class GeneratePdf extends Action
{
/**
* @var TemplateResolver
*/
private $templateResolver;
public function __construct(
TemplateResolver $templateResolver,
Context $context
) {
parent::__construct($context);
$this->templateResolver = $templateResolver;
}
public function execute()
{
return $this->renderPdfResult();
}
protected function renderPdfResult()
{
/** @var PdfResult $result */
$result = $this->resultFactory->create(PdfResult::TYPE);
$result->addGlobalOptions(
new PdfOptions(
[
PdfOptions::KEY_GLOBAL_TITLE => __('Return PDF'),
PdfOptions::KEY_PAGE_ENCODING => PdfOptions::ENCODING_UTF_8,
PdfOptions::KEY_GLOBAL_ORIENTATION => PdfOptions::ORIENTATION_PORTRAIT,
PdfOptions::FLAG_PAGE_PRINT_MEDIA_TYPE,
PdfOptions::KEY_PAGE_HEADER_SPACING => "10",
]
)
);
$result->addPageOptions(
new PdfOptions(
[
PdfOptions::KEY_PAGE_COOKIES => ${'_COOKIE'},
PdfOptions::KEY_PAGE_HEADER_HTML_URL => $this->templateResolver
->getTemplateFileName('Vendor_Package::pdf/header.html'),
PdfOptions::KEY_PAGE_FOOTER_HTML_URL => $this->templateResolver
->getTemplateFileName('Vendor_ Package::pdf/footer.html'),
]
)
);
return $result;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.