Skip to content

Commit bec5b80

Browse files
committed
Implement PDFEngine
- Add PDFWrapper and PDFEngine base. - Add MPDF default engine. - Refactor legacy MPDF code to use new engines. Signed-off-by: Dillon-Brown <[email protected]>
1 parent 42fd6a9 commit bec5b80

File tree

11 files changed

+928
-107
lines changed

11 files changed

+928
-107
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ custom/modules/unified_search_modules_display.php
1616
# Ignore AOD indexes
1717
modules/AOD_Index/Index/*
1818
install/status.json
19+
# Ignore PDF Fonts
20+
modules/AOS_PDF_Templates/PDF_Lib/ttfontdata/
1921
# Configuration files should be ignored.
2022
/config.php
2123
/config_override.php
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* SugarCRM Community Edition is a customer relationship management program developed by
4+
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5+
*
6+
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
7+
* Copyright (C) 2011 - 2021 SalesAgility Ltd.
8+
*
9+
* This program is free software; you can redistribute it and/or modify it under
10+
* the terms of the GNU Affero General Public License version 3 as published by the
11+
* Free Software Foundation with the addition of the following permission added
12+
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13+
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14+
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15+
*
16+
* This program is distributed in the hope that it will be useful, but WITHOUT
17+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18+
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19+
* details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License along with
22+
* this program; if not, see http://www.gnu.org/licenses or write to the Free
23+
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24+
* 02110-1301 USA.
25+
*
26+
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27+
* SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28+
*
29+
* The interactive user interfaces in modified source and object code versions
30+
* of this program must display Appropriate Legal Notices, as required under
31+
* Section 5 of the GNU Affero General Public License version 3.
32+
*
33+
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34+
* these Appropriate Legal Notices must retain the display of the "Powered by
35+
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36+
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
37+
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38+
*/
39+
40+
namespace SuiteCRM\PDF\Exceptions;
41+
42+
if (!defined('sugarEntry') || !sugarEntry) {
43+
die('Not A Valid Entry Point');
44+
}
45+
46+
/**
47+
* Class SearchEngineNotFoundException
48+
* @package SuiteCRM\PDF\Exceptions
49+
*/
50+
class PDFEngineNotFoundException extends PDFException
51+
{
52+
}

lib/PDF/Exceptions/PDFException.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* SugarCRM Community Edition is a customer relationship management program developed by
4+
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5+
*
6+
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
7+
* Copyright (C) 2011 - 2021 SalesAgility Ltd.
8+
*
9+
* This program is free software; you can redistribute it and/or modify it under
10+
* the terms of the GNU Affero General Public License version 3 as published by the
11+
* Free Software Foundation with the addition of the following permission added
12+
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13+
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14+
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15+
*
16+
* This program is distributed in the hope that it will be useful, but WITHOUT
17+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18+
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19+
* details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License along with
22+
* this program; if not, see http://www.gnu.org/licenses or write to the Free
23+
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24+
* 02110-1301 USA.
25+
*
26+
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27+
* SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28+
*
29+
* The interactive user interfaces in modified source and object code versions
30+
* of this program must display Appropriate Legal Notices, as required under
31+
* Section 5 of the GNU Affero General Public License version 3.
32+
*
33+
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34+
* these Appropriate Legal Notices must retain the display of the "Powered by
35+
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36+
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
37+
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38+
*/
39+
40+
namespace SuiteCRM\PDF\Exceptions;
41+
42+
use RuntimeException;
43+
44+
if (!defined('sugarEntry') || !sugarEntry) {
45+
die('Not A Valid Entry Point');
46+
}
47+
48+
/**
49+
* Class PDFException
50+
* @package SuiteCRM\PDF\Exceptions
51+
*/
52+
class PDFException extends RuntimeException
53+
{
54+
}

lib/PDF/MPDF/MPDFEngine.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
/**
3+
* SugarCRM Community Edition is a customer relationship management program developed by
4+
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5+
*
6+
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
7+
* Copyright (C) 2011 - 2021 SalesAgility Ltd.
8+
*
9+
* This program is free software; you can redistribute it and/or modify it under
10+
* the terms of the GNU Affero General Public License version 3 as published by the
11+
* Free Software Foundation with the addition of the following permission added
12+
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13+
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14+
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15+
*
16+
* This program is distributed in the hope that it will be useful, but WITHOUT
17+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18+
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19+
* details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License along with
22+
* this program; if not, see http://www.gnu.org/licenses or write to the Free
23+
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24+
* 02110-1301 USA.
25+
*
26+
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27+
* SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28+
*
29+
* The interactive user interfaces in modified source and object code versions
30+
* of this program must display Appropriate Legal Notices, as required under
31+
* Section 5 of the GNU Affero General Public License version 3.
32+
*
33+
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34+
* these Appropriate Legal Notices must retain the display of the "Powered by
35+
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36+
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
37+
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38+
*/
39+
40+
namespace SuiteCRM\PDF\MPDF;
41+
42+
if (!defined('sugarEntry') || !sugarEntry) {
43+
die('Not A Valid Entry Point');
44+
}
45+
46+
use mPDF;
47+
use SuiteCRM\PDF\PDFEngine;
48+
49+
require_once __DIR__ . '/../../../modules/AOS_PDF_Templates/PDF_Lib/mpdf.php';
50+
51+
/**
52+
* Class MPDFEngine
53+
* @package SuiteCRM\PDF\MPDF
54+
*/
55+
class MPDFEngine extends PDFEngine
56+
{
57+
/**
58+
* @var mPDF
59+
*/
60+
public $pdf;
61+
62+
/**
63+
* MPDFEngine constructor.
64+
* @param mPDF|null $pdf
65+
*/
66+
public function __construct(mPDF $pdf = null)
67+
{
68+
@$this->pdf = $pdf ?? new mPDF();
69+
}
70+
71+
/**
72+
* @param string $html
73+
* @param int $section 0=default; 1=headerCSS; 2=HTML body; 3=HTML parses; 4=HTML headers;
74+
* @param bool $init Leaves buffers, etc, in current state so that it can continue a block.
75+
* @param bool $close Clears and sets buffers to top level block.
76+
* @return void
77+
*/
78+
public function writeHTML(string $html, int $section = 0, bool $init = true, bool $close = true): void
79+
{
80+
@$this->pdf->WriteHTML($html, $section, $init, $close);
81+
82+
if ($section === 1) {
83+
@$this->pdf->SetDefaultBodyCSS('background-color', '#FFFFFF');
84+
unset($this->pdf->cssmgr->CSS['INPUT']['FONT-SIZE']);
85+
}
86+
}
87+
88+
/**
89+
* @param string $name
90+
* @param string $destination
91+
* @return void|string
92+
*/
93+
public function outputPDF(string $name, string $destination): ?string
94+
{
95+
@$output = $this->pdf->Output($name, $destination);
96+
97+
if (is_string($output)) {
98+
return $output;
99+
}
100+
}
101+
102+
/**
103+
* @param string|array $html
104+
* @return void
105+
*/
106+
public function writeHeader(string $html): void
107+
{
108+
@$this->pdf->setHeader($html);
109+
}
110+
111+
/**
112+
* @param string|array $html
113+
* @return void
114+
*/
115+
public function writeFooter(string $html): void
116+
{
117+
@$this->pdf->setFooter($html);
118+
}
119+
120+
public function writeBlankPage(): void
121+
{
122+
@$this->pdf->AddPage();
123+
}
124+
125+
/**
126+
* @param array $options
127+
* @return void
128+
*/
129+
public function configurePDF(array $options): void
130+
{
131+
$configOptions = [
132+
'mode' => $options['mode'] ?? '',
133+
'page_size' => $options['page_size'] ?? 'A4',
134+
'default_font_size' => $options['fontSize'] ?? 0,
135+
'default_font' => $options['font'] ?? '',
136+
'mgl' => $options['mgl'] ?? 15,
137+
'mgr' => $options['mgr'] ?? 15,
138+
'mgt' => $options['mgt'] ?? 16,
139+
'mgb' => $options['mgb'] ?? 16,
140+
'mgh' => $options['mgh'] ?? 9,
141+
'mgf' => $options['mgf'] ?? 9,
142+
'orientation' => $options['orientation'] ?? 'P'
143+
];
144+
145+
@$this->pdf = new mPDF(
146+
$configOptions['mode'],
147+
$configOptions['page_size'],
148+
$configOptions['default_font_size'],
149+
$configOptions['default_font'],
150+
$configOptions['mgl'],
151+
$configOptions['mgr'],
152+
$configOptions['mgt'],
153+
$configOptions['mgb'],
154+
$configOptions['mgh'],
155+
$configOptions['mgf'],
156+
$configOptions['orientation'],
157+
);
158+
159+
@$this->pdf->SetAutoFont();
160+
}
161+
}

lib/PDF/PDFEngine.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* SugarCRM Community Edition is a customer relationship management program developed by
4+
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5+
*
6+
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
7+
* Copyright (C) 2011 - 2021 SalesAgility Ltd.
8+
*
9+
* This program is free software; you can redistribute it and/or modify it under
10+
* the terms of the GNU Affero General Public License version 3 as published by the
11+
* Free Software Foundation with the addition of the following permission added
12+
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13+
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14+
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15+
*
16+
* This program is distributed in the hope that it will be useful, but WITHOUT
17+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18+
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19+
* details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License along with
22+
* this program; if not, see http://www.gnu.org/licenses or write to the Free
23+
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24+
* 02110-1301 USA.
25+
*
26+
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27+
* SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28+
*
29+
* The interactive user interfaces in modified source and object code versions
30+
* of this program must display Appropriate Legal Notices, as required under
31+
* Section 5 of the GNU Affero General Public License version 3.
32+
*
33+
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34+
* these Appropriate Legal Notices must retain the display of the "Powered by
35+
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36+
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
37+
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38+
*/
39+
40+
namespace SuiteCRM\PDF;
41+
42+
if (!defined('sugarEntry') || !sugarEntry) {
43+
die('Not A Valid Entry Point');
44+
}
45+
46+
/**
47+
* Class PDFEngine
48+
* @package SuiteCRM\PDF
49+
*/
50+
abstract class PDFEngine
51+
{
52+
/**
53+
* @param string $html
54+
* @return void
55+
*/
56+
abstract public function writeHTML(string $html): void;
57+
58+
/**
59+
* @param string|array $html
60+
* @return void
61+
*/
62+
abstract public function writeFooter(string $html): void;
63+
64+
/**
65+
* @param string|array $html
66+
* @return void
67+
*/
68+
abstract public function writeHeader(string $html): void;
69+
70+
/**
71+
* @return void
72+
*/
73+
abstract public function writeBlankPage(): void;
74+
75+
/**
76+
* @param string $name
77+
* @param string $destination
78+
* @return string|void
79+
*/
80+
abstract public function outputPDF(string $name, string $destination): ?string;
81+
82+
/**
83+
* @param array $options
84+
* @return void
85+
*/
86+
abstract public function configurePDF(array $options): void;
87+
}

0 commit comments

Comments
 (0)