Commit 96fc8329 authored by xorti's avatar xorti

[FIX] Fix issue with unoconv file conversion to pdf

- Now supports files stored in filesystem
- Use binary path based on preference
parent 6fdd3fe1
Pipeline #49884855 failed with stages
in 27 minutes
......@@ -27,7 +27,14 @@ class UnoconvLib
return;
}
$this->unoconv = Unoconv::create();
global $prefs;
$config = [
'unoconv.binaries' => $prefs['alchemy_unoconv_path'],
'unoconv.timeout' => 60,
];
$this->unoconv = Unoconv::create($config);
}
/**
......
......@@ -30,27 +30,35 @@ class PDFHelper
/**
* Converts a tiki file to PDF
*
* @param $file
* @param $fileId
* @return null|string
* @throws \Exception
*/
public static function convertToPDF($file)
public static function convertToPDF($fileId)
{
global $user, $tikidomain;
if (! self::canConvertToPDF($file['filetype'])) {
$userLib = \TikiLib::lib('user');
$file = \Tiki\FileGallery\File::id($fileId);
if (! $file->exists() || ! $userLib->user_has_perm_on_object($user, $file->fileId, 'file', 'tiki_p_download_files')) {
return null;
}
if (! self::canConvertToPDF($file->filetype)) {
$error = 'File type not supported.';
$message = sprintf("Failed to convert document %s (id: %s) to pdf. Error: %s", $file['filename'], $file['filename'], $error);
$message = sprintf("Failed to convert document %s (id: %s) to pdf. Error: %s", $file->filename, $fileId, $error);
$logsLib = \TikiLib::lib('logs');
$logsLib->add_log('File', $message);
return null;
}
$sourceFile = implode(DIRECTORY_SEPARATOR, ['temp', 'cache', $tikidomain, 'source_' . $file['fileId']]);
$targetFile = implode(DIRECTORY_SEPARATOR, ['temp', 'cache', $tikidomain, 'target_' . $file['fileId'] . '.pdf']);
file_put_contents($sourceFile, $file['data']);
$sourceFile = $file->getWrapper()->getReadableFile();
$targetFile = implode(DIRECTORY_SEPARATOR, ['temp', 'cache', $tikidomain, 'target_' . $fileId . '.pdf']);
try {
$convertFail = false;
$unoconv = new UnoconvLib();
$unoconv->convertFile($sourceFile, $targetFile);
} catch (\Exception $e) {
......@@ -60,10 +68,8 @@ class PDFHelper
$logsLib->add_log('Unoconv', $message);
}
unlink($sourceFile);
if ((empty($targetFile) && file_exists($targetFile)) || $convertFail) {
$message = tr('Failed to convert document %0 to pdf.', $file['filename']);
$message = tr('Failed to convert document %0 to pdf.', $file->filename);
$userlib = \TikiLib::lib('user');
if ($userlib->user_has_permission($user, 'tiki_p_view_actionlog')) {
......
......@@ -258,7 +258,7 @@ if (isset($_GET['preview']) || isset($_GET['thumbnail']) || isset($_GET['display
if ($build_content) {
if ($convertToPdf) {
$content = PDFHelper::convertToPDF($info);
$content = PDFHelper::convertToPDF($_REQUEST['fileId']);
} elseif (! isset($_GET['display']) || isset($_GET['x']) || isset($_GET['y']) || $scale || isset($_GET['max']) || isset($_GET['format']) || isset($_GET['thumbnail'])) {
// Modify the original image if needed
if (! Image::isAvailable()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment