1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\MailSaveMessageAsPdfPlugin;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2023, Afterlogic Corp.
14: *
15: * @property Settings $oModuleSettings
16: *
17: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractModule
20: {
21: /**
22: * Initializes Mail Module.
23: *
24: * @ignore
25: */
26: public function init()
27: {
28: $this->aErrors = [
29: Enums\ErrorCodes::LibraryNoFound => $this->i18N('ERROR_NO_PDF_GENERATOR_FOUND'),
30: ];
31: }
32:
33: /**
34: * @return Module
35: */
36: public static function getInstance()
37: {
38: return parent::getInstance();
39: }
40:
41: /**
42: * @return Module
43: */
44: public static function Decorator()
45: {
46: return parent::Decorator();
47: }
48:
49: /**
50: * @return Settings
51: */
52: public function getModuleSettings()
53: {
54: return $this->oModuleSettings;
55: }
56:
57: /**
58: * @param int $UserId
59: * @param string $FileName
60: * @param string $Html
61: * @return boolean
62: */
63: public function GeneratePdfFile($UserId, $FileName, $Html)
64: {
65: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
66:
67: $sFileName = $FileName . '.pdf';
68:
69: $sUUID = \Aurora\System\Api::getUserUUIDById($UserId);
70: $sTempName = md5($sUUID . $sFileName . microtime(true));
71:
72: $sExec = \Aurora\System\Api::DataPath() . '/system/wkhtmltopdf/linux/wkhtmltopdf';
73: if (!\file_exists($sExec)) {
74: $sExec = \Aurora\System\Api::DataPath() . '/system/wkhtmltopdf/win/wkhtmltopdf.exe';
75: if (!\file_exists($sExec)) {
76: $sExec = '';
77: }
78: }
79:
80: if (0 < \strlen($sExec)) {
81: $oSnappy = new \Knp\Snappy\Pdf($sExec);
82: $oSnappy->setOption('quiet', true);
83: $oSnappy->setOption('disable-javascript', true);
84: $oSnappy->setOption('encoding', 'utf-8');
85:
86: $oApiFileCache = new \Aurora\System\Managers\Filecache();
87:
88: $oSnappy->generateFromHtml(
89: $Html,
90: $oApiFileCache->generateFullFilePath($sUUID, $sTempName, '', self::GetName()),
91: array(),
92: true
93: );
94:
95: return \Aurora\System\Utils::GetClientFileResponse(
96: self::GetName(),
97: $UserId,
98: $sFileName,
99: $sTempName,
100: $oApiFileCache->fileSize($sUUID, $sTempName, '', self::GetName())
101: );
102: } else {
103: throw new \Aurora\System\Exceptions\ApiException(Enums\ErrorCodes::LibraryNoFound);
104: }
105:
106: return false;
107: }
108: }
109: