Last active
June 17, 2020 10:13
-
-
Save vgrish/81e349b60fe001d4c20c to your computer and use it in GitHub Desktop.
exp.Pdf.create - сниппет для формирования pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** @var array $scriptProperties */ | |
$pdfTpl = $scriptProperties['pdfTpl'] = trim($modx->getOption('pdfTpl', $scriptProperties, '@INLINE <a href="[[+file]]" target="_blank">download the pdf</a>', true)); | |
$tplEmpty = $scriptProperties['tplEmpty'] = trim($modx->getOption('tplEmpty', $scriptProperties, '', true)); | |
$pageEncoding = $scriptProperties['pageEncoding'] = trim($modx->getOption('pageEncoding', $scriptProperties, 'utf-8', true)); | |
$pageFormat = $scriptProperties['pageFormat'] = trim($modx->getOption('pageFormat', $scriptProperties, 'A4', true)); | |
$pageOrientation = $scriptProperties['pageOrientation'] = trim($modx->getOption('pageOrientation', $scriptProperties, 'L', true)); | |
$pageMargins = $scriptProperties['pageMargins'] = trim($modx->getOption('pageMargins', $scriptProperties, '10,10,10,10', true)); | |
$pageFont = $scriptProperties['pageFont'] = trim($modx->getOption('pageFont', $scriptProperties, 'courier', true)); | |
$pageFontSize = $scriptProperties['pageFontSize'] = trim($modx->getOption('pageFontSize', $scriptProperties, '14', true)); | |
$creator = $scriptProperties['creator'] = $modx->getOption('creator', $scriptProperties, 'ExportPage', true); | |
$author = $scriptProperties['author'] = $modx->getOption('author', $scriptProperties, 'ExportPage', true); | |
$prepareCss = $scriptProperties['prepareCss'] = (boolean)$modx->getOption('prepareCss', $scriptProperties, false, true); | |
$prepareImg = $scriptProperties['prepareImg'] = (boolean)$modx->getOption('prepareImg', $scriptProperties, false, true); | |
$showLog = $scriptProperties['showLog'] = (boolean)$modx->getOption('showLog', $scriptProperties, false, true); | |
$toPlaceholder = $scriptProperties['toPlaceholder'] = (boolean)$modx->getOption('toPlaceholder', $scriptProperties, false, true); | |
$path = $scriptProperties['path'] = trim($modx->getOption('path', $scriptProperties, MODX_BASE_PATH . 'tmp/', true)); | |
$content = $scriptProperties['content'] = $modx->getOption('content', $scriptProperties, '444', true); | |
$hash = $scriptProperties['hash'] = $modx->getOption('hash', $scriptProperties, sha1(serialize($scriptProperties)), true); | |
/** @var exportpage $exportpage */ | |
if (!$exportpage = $modx->getService('exportpage', 'exportpage', $modx->getOption('exportpage_core_path', null, $modx->getOption('core_path') . 'components/exportpage/') . 'model/exportpage/', $scriptProperties)) { | |
return 'Could not load exportpage class!'; | |
} | |
$exportpage->initialize($modx->context->key, $scriptProperties); | |
$exportpage->loadPdfTools(); | |
$pdfTools = $exportpage->pdfTools; | |
$pdfTools->setConfig($scriptProperties); | |
$row = array_merge($scriptProperties, array( | |
'file' => '', | |
)); | |
$output = ''; | |
if (!$stream = $pdfTools->getCompileFile()) { | |
$output = !empty($tplEmpty) ? $exportpage->pdoTools->getChunk($tplEmpty, $row) : ''; | |
} else { | |
$row['file'] = $path . $hash . '.pdf'; | |
if (!file_put_contents($row['file'], $stream)) { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[ExportPage] Could not save export file ' . $output); | |
} | |
$row['file'] = $modx->getOption('site_url') . ltrim(str_replace(MODX_BASE_PATH, MODX_BASE_URL, $row['file']), '/'); | |
$output = !empty($pdfTpl) ? $exportpage->pdoTools->getChunk($pdfTpl, $row) : $exportpage->pdoTools->getChunk('', $row); | |
} | |
if ($toPlaceholder) { | |
$modx->setPlaceholder($toPlaceholder, $output); | |
} else { | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment