Skip to content

Commit

Permalink
Merge pull request #24 from EaDesgin/feature/1799-mpdf-71
Browse files Browse the repository at this point in the history
Feature/1799 mpdf 71
  • Loading branch information
eadesignro authored Oct 30, 2017
2 parents b53c107 + ea88a37 commit 235ee0e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sudo: false

php:
- 7.0
- 7.1

install:
- echo "{\"http-basic\":{\"repo.magento.com\":{\"username\":\"${MAGENTO_USERNAME}\",\"password\":\"${MAGENTO_PASSWORD}\"}}}" > ~/.composer/auth.json
Expand All @@ -13,7 +14,6 @@ install:

script:
- php magento-coding-standard/vendor/bin/phpcs --standard=Test/Php/_files/phpcs/ruleset.xml Api/ Block/ Controller/ Helper/ Model/ Setup/ Test/ Ui/ view/
- php vendor/bin/phpmd Api/,Block/,Controller/,Helper/,Model/,Setup/,Test/,Ui/,view/ text Test/Php/_files/phpmd/ruleset.xml

env:
global:
Expand Down
3 changes: 2 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Magento\Framework\App\Helper\Context;
use Magento\Sales\Model\Order\Invoice;
use Magento\Store\Model\ScopeInterface;
use Mpdf\Mpdf;

/**
* Handles the config and other settings
Expand Down Expand Up @@ -98,7 +99,7 @@ public function isEnable()
*/
private function mPDFExists()
{
if (class_exists('mPDF')) {
if (class_exists(Mpdf::class)) {
return true;
}
return false;
Expand Down
130 changes: 81 additions & 49 deletions Helper/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use Eadesigndev\Pdfgenerator\Model\Pdfgenerator;
use Eadesigndev\Pdfgenerator\Model\Source\TemplatePaperOrientation;
use Eadesigndev\Pdfgenerator\Model\Source\TemplatePaperForm;
use Eadesigndev\Pdfgenerator\Model\Template\Processor;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Model\Order;
Expand All @@ -29,7 +30,8 @@
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Sales\Model\Order\Invoice;
use mPDF;
use Magento\Framework\App\Filesystem\DirectoryList;
use Mpdf\Mpdf;

/**
* Class Pdf
Expand Down Expand Up @@ -95,6 +97,17 @@ class Pdf extends AbstractHelper
*/
public $processor;

/**
* @var TemplatePaperForm
*/
private $templatePaperForm;
/**
* @var TemplatePaperOrientation
*/
private $templatePaperOrientation;

private $directoryList;

/**
* Pdf constructor.
* @param Context $context
Expand All @@ -108,12 +121,18 @@ public function __construct(
Renderer $addressRenderer,
PaymentHelper $paymentHelper,
InvoiceIdentity $identityContainer,
Processor $templateFactory
Processor $templateFactory,
DirectoryList $directoryList,
TemplatePaperForm $templatePaperForm,
TemplatePaperOrientation $templatePaperOrientation
) {
$this->processor = $templateFactory;
$this->paymentHelper = $paymentHelper;
$this->identityContainer = $identityContainer;
$this->addressRenderer = $addressRenderer;
$this->processor = $templateFactory;
$this->paymentHelper = $paymentHelper;
$this->identityContainer = $identityContainer;
$this->addressRenderer = $addressRenderer;
$this->directoryList = $directoryList;
$this->templatePaperForm = $templatePaperForm;
$this->templatePaperOrientation = $templatePaperOrientation;
parent::__construct($context);
}

Expand Down Expand Up @@ -210,44 +229,18 @@ public function _eaPDFSettings($parts)

$templateModel = $this->template;

if (!$templateModel->getTemplateCustomForm()) {
$oldErrorReporting = error_reporting();
error_reporting(0);

if (!$templateModel->getTemplateCustomForm()) {
/** @var mPDF $pdf */
//@codingStandardsIgnoreLine
$pdf = new mPDF(
'',
$this->paperFormat(
$templateModel->getTemplatePaperForm(),
$templateModel->getTemplatePaperOri()
),
$default_font_size = 0,
$default_font = '',
$mgl = $templateModel->getTemplateCustomL(),
$mgr = $templateModel->getTemplateCustomR(),
$mgt = $templateModel->getTemplateCustomT(),
$mgb = $templateModel->getTemplateCustomB(),
$mgh = 9,
$mgf = 9
);
$pdf = new Mpdf($this->config($templateModel));
}

if ($templateModel->getTemplateCustomForm()) {
//@codingStandardsIgnoreLine
$pdf = new mPDF(
'',
[
$templateModel->getTemplateCustomW(),
$templateModel->getTemplateCustomH()
],
$default_font_size = 0,
$default_font = '',
$mgl = $templateModel->getTemplateCustomL(),
$mgr = $templateModel->getTemplateCustomR(),
$mgt = $templateModel->getTemplateCustomT(),
$mgb = $templateModel->getTemplateCustomB(),
$mgh = 9,
$mgf = 9
);
$pdf = new Mpdf($this->config($templateModel));
}

$pdf->SetHTMLHeader($parts['header']);
Expand All @@ -259,27 +252,66 @@ public function _eaPDFSettings($parts)
$pdf->WriteHTML('<body>' . html_entity_decode($parts['body']) . '</body>');
$pdfToOutput = $pdf->Output('', 'S');

error_reporting($oldErrorReporting);

return $pdfToOutput;
}

/**
* Get the format and orientation, ex: A4-L
* @param $form
* @param $ori
* @return string
* @param Pdfgenerator $templateModel
* @return array
*/
private function paperFormat($form, $ori)
private function config($templateModel): array
{
$size = self::PAPER_SIZE;
$oris = self::PAPER_ORI;

if ($ori == TemplatePaperOrientation::TEMAPLATE_PAPER_PORTRAIT) {
return str_replace('-', '', $size[$form]);
$ori = $templateModel->getTemplatePaperOri();
$orientation = $this->templatePaperOrientation->getAvailable();
$finalOri = $orientation[$ori][0];
$marginTop = $templateModel->getTemplateCustomT();
$marginBottom = $templateModel->getTemplateCustomB();
$paperForms = $this->templatePaperForm->getAvailable();
$templatePaperForm = $templateModel->getTemplatePaperForm();
if (!$templatePaperForm) {
$templatePaperForm = 1;
}
$form = $paperForms[$templatePaperForm];
if ($ori == TemplatePaperOrientation::TEMAPLATE_PAPER_LANDSCAPE) {
$form = $paperForms[$templateModel->getTemplatePaperForm()] . '-' . $finalOri;
}

$config = [
'mode' => '',
'format' => $form,
'default_font_size' => '',
'default_font' => '',
'margin_left' => $templateModel->getTemplateCustomL(),
'margin_right' => $templateModel->getTemplateCustomR(),
'margin_top' => $marginTop,
'margin_bottom' => $marginBottom,
'margin_header' => 0,
'margin_footer' => 0,
'tempDir' => $this->directoryList->getPath('tmp')
];

$format = $size[$form] . $oris[$ori];
if ($templateModel->getTemplateCustomForm()) {
$config = [
'mode' => '',
'format' => [
$templateModel->getTemplateCustomW(),
$templateModel->getTemplateCustomH()
],
'default_font_size' => '',
'default_font' => '',
'margin_left' => $templateModel->getTemplateCustomL(),
'margin_right' => $templateModel->getTemplateCustomR(),
'margin_top' => $marginTop,
'margin_bottom' => $marginBottom,
'margin_header' => 0,
'margin_footer' => 0,
'tempDir' => $this->directoryList->getPath('tmp')
];
}

return $format;
return $config;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions Model/Source/TemplatePaperForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public function __construct(BuilderInterface $pageLayoutBuilder)
public function getAvailable()
{
return [
self::TEMAPLATE_PAPER_FORM_A4 => __('A4'),
self::TEMAPLATE_PAPER_FORMAT_A3 => __('A3'),
self::TEMAPLATE_PAPER_FORMAT_A5 => __('A5'),
self::TEMAPLATE_PAPER_FORMAT_A6 => __('A6'),
self::TEMAPLATE_PAPER_FORMAT_LETTER => __('Letter'),
self::TEMAPLATE_PAPER_FORMAT_LEGAL => __('Legal'),
self::TEMAPLATE_PAPER_FORM_A4 => 'A4',
self::TEMAPLATE_PAPER_FORMAT_A3 => 'A3',
self::TEMAPLATE_PAPER_FORMAT_A5 => 'A5',
self::TEMAPLATE_PAPER_FORMAT_A6 => 'A6',
self::TEMAPLATE_PAPER_FORMAT_LETTER => 'Letter',
self::TEMAPLATE_PAPER_FORMAT_LEGAL => 'Legal',
];
}
}
4 changes: 2 additions & 2 deletions Model/Source/TemplatePaperOrientation.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function __construct(BuilderInterface $pageLayoutBuilder)
public function getAvailable()
{
return [
self::TEMAPLATE_PAPER_PORTRAIT => __('Portrait'),
self::TEMAPLATE_PAPER_LANDSCAPE => __('Landscape'),
self::TEMAPLATE_PAPER_PORTRAIT => 'Portrait',
self::TEMAPLATE_PAPER_LANDSCAPE =>'Landscape',
];
}
}
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
}
],
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/module-backend": "100.2.*",
"magento/module-sales": "101.0.*",
"magento/framework": "101.0.*",
"php": "~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0",
"magento/module-backend": "^100.1.0| ^100.2.0",
"magento/module-sales": "^100.1.0| ^101.0.0",
"magento/framework": "^100.1.0| ^101.0.0",
"eadesignro/module-eacore": ">=0.2.6",
"eadesignro/mpdf": "7.0.*"
"mpdf/mpdf": "7.0.*"
},
"require-dev": {
"phpunit/phpunit": "~6.2.0",
Expand Down
7 changes: 0 additions & 7 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,4 @@
<argument name="filterPool" xsi:type="object" shared="false">TemplatesGirdFilterPool</argument>
</arguments>
</virtualType>

<virtualType name="Eadesigndev\Pdfgenerator\Model\ResourceModel\Pdfgenerator\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">eadesign_pdf_templates</argument>
<argument name="resourceModel" xsi:type="string">Eadesigndev\Pdfgenerator\Model\ResourceModel\Pdfgenerator</argument>
</arguments>
</virtualType>
</config>

0 comments on commit 235ee0e

Please sign in to comment.