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: * @package Modules
16: */
17: class Module extends \Aurora\System\Module\AbstractModule
18: {
19: /**
20: * Initializes Mail Module.
21: *
22: * @ignore
23: */
24: public function init()
25: {
26: $this->aErrors = [
27: Enums\ErrorCodes::LibraryNoFound => $this->i18N('ERROR_NO_PDF_GENERATOR_FOUND'),
28: ];
29: }
30: /**
31: * @param int $UserId
32: * @param string $FileName
33: * @param string $Html
34: * @return boolean
35: */
36: public function GeneratePdfFile($UserId, $FileName, $Html)
37: {
38: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
39:
40: $sFileName = $FileName . '.pdf';
41:
42: $sUUID = \Aurora\System\Api::getUserUUIDById($UserId);
43: $sTempName = md5($sUUID.$sFileName.microtime(true));
44:
45: $sExec = \Aurora\System\Api::DataPath().'/system/wkhtmltopdf/linux/wkhtmltopdf';
46: if (!\file_exists($sExec)) {
47: $sExec = \Aurora\System\Api::DataPath().'/system/wkhtmltopdf/win/wkhtmltopdf.exe';
48: if (!\file_exists($sExec)) {
49: $sExec = '';
50: }
51: }
52:
53: if (0 < \strlen($sExec)) {
54: $oSnappy = new \Knp\Snappy\Pdf($sExec);
55: $oSnappy->setOption('quiet', true);
56: $oSnappy->setOption('disable-javascript', true);
57: $oSnappy->setOption('encoding', 'utf-8');
58:
59: $oApiFileCache = new \Aurora\System\Managers\Filecache();
60:
61: $oSnappy->generateFromHtml(
62: $Html,
63: $oApiFileCache->generateFullFilePath($sUUID, $sTempName, '', self::GetName()),
64: array(),
65: true
66: );
67:
68: return \Aurora\System\Utils::GetClientFileResponse(
69: self::GetName(),
70: $UserId,
71: $sFileName,
72: $sTempName,
73: $oApiFileCache->fileSize($sUUID, $sTempName, '', self::GetName())
74: );
75: } else {
76: throw new \Aurora\System\Exceptions\ApiException(Enums\ErrorCodes::LibraryNoFound);
77: }
78:
79: return false;
80: }
81: }
82: