/**
* @param null $path
* @return null|string|void
* @throws \Exception
*/
public function getPdf($path = null)
{
if ($path) {
$path = $this->preparePath($path);
}
$pdfPath = null;
if (!$path && $this->path) {
$path = $this->path;
}
try {
// if the document is already an PDF, delegate the call directly to parent::getPdf() (Ghostscript)
$pdfPath = parent::getPdf($path);
return $pdfPath;
} catch (\Exception $e) {
// nothing to do, delegate to libreoffice
}
$pdfFile = PIMCORE_TEMPORARY_DIRECTORY . "/document-pdf-cache/document_" . md5($path . filemtime($path)) . "__libreoffice.pdf";
if (!is_dir(dirname($pdfFile))) {
File::mkdir(dirname($pdfFile));
}
$lockKey = "soffice";
if (!file_exists($pdfFile)) {
// a list of all available filters is here:
// http://cgit.freedesktop.org/libreoffice/core/tree/filter/source/config/fragments/filters
$cmd = self::getLibreOfficeCli() . " --headless --nologo --nofirststartwizard --norestore --convert-to pdf:writer_web_pdf_Export --outdir " . escapeshellarg(PIMCORE_SYSTEM_TEMP_DIRECTORY) . " " . escapeshellarg($path);
Model\Tool\Lock::acquire($lockKey);
// avoid parallel conversions
$out = Console::exec($cmd, PIMCORE_LOG_DIRECTORY . "/libreoffice-pdf-convert.log", 240);
Model\Tool\Lock::release($lockKey);
Logger::debug("LibreOffice Output was: " . $out);
$tmpName = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/" . preg_replace("/\\." . File::getFileExtension($path) . "\$/", ".pdf", basename($path));
if (file_exists($tmpName)) {
File::rename($tmpName, $pdfFile);
$pdfPath = $pdfFile;
} else {
$message = "Couldn't convert document to PDF: " . $path . " with the command: '" . $cmd . "'";
Logger::error($message);
throw new \Exception($message);
}
} else {
$pdfPath = $pdfFile;
}
return $pdfPath;
}