Skip to content
Snippets Groups Projects
Commit 0c018750 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Merge branch 'B11509-PDF-splitting-fails' into 'develop'

Fixes #11509. Implement a) detection of broken 'split PDF to JPEG', b) fire...

See merge request !292
parents a78ec8dc 8ee650f7
No related branches found
No related tags found
3 merge requests!297F6793 rename files in zip,!295Develop,!292Fixes #11509. Implement a) detection of broken 'split PDF to JPEG', b) fire...
Pipeline #4212 passed
......@@ -46,6 +46,8 @@ The following features are only tested / supported on linux hosts:
* PDF decrypt (used for merge with pdfunite) - command `qpdf`.
* PDF decrypt (used for merge with pdfunite) - command `gs` - in case `qpdf` is not successful.
* Mime type detection for uploads - command `file`.
* Split PDF into JPG - command `convert`.
* Repair PDF - command `pdftocairo`.
.. _`preparation`:
......@@ -401,7 +403,11 @@ Extension Manager: QFQ Configuration
+-----------------------------------+-------------------------------------------------------+----------------------------------------------------------------------------+
| cmdInkscape | inkscape | If inkscape is not available, specify an empty string. |
+-----------------------------------+-------------------------------------------------------+----------------------------------------------------------------------------+
| cmdConvert | convert | GraphicsMagics 'convert' is recommended. |
| cmdConvert | convert | Image-/GraphicsMagics 'convert' is recommended. |
+-----------------------------------+-------------------------------------------------------+----------------------------------------------------------------------------+
| cmdPdf2svg | pdf2svg | Convert PDF to SVG. |
+-----------------------------------+-------------------------------------------------------+----------------------------------------------------------------------------+
| cmdPdftocairo | pdftocairo | Poppler based. |
+-----------------------------------+-------------------------------------------------------+----------------------------------------------------------------------------+
| cmdWkhtmltopdf | /usr/bin/wkhtmltopdf | PathFilename of wkhtmltopdf. Optional variables like LD_LIBRARY_PATH=... |
+-----------------------------------+-------------------------------------------------------+----------------------------------------------------------------------------+
......
......@@ -259,7 +259,7 @@ const ERROR_IO_CHMOD = 1314;
const ERROR_IO_READ_FILE = 1315;
const ERROR_IO_WRITE_FILE = 1316;
const ERROR_PDF2SVG = 1320;
const ERROR_PDF_SPLIT = 1320;
const ERROR_PDF2JPEG = 1321;
//Report
......@@ -647,6 +647,8 @@ const SYSTEM_SHOW_ID_IN_FORM_TITLE = 'showIdInFormTitle';
const SYSTEM_CMD_WKHTMLTOPDF = 'cmdWkhtmltopdf';
const SYSTEM_CMD_INKSCAPE = 'cmdInkscape';
const SYSTEM_CMD_CONVERT = 'cmdConvert';
const SYSTEM_CMD_PDF2SVG = 'cmdPdf2svg';
const SYSTEM_CMD_PDFTOCAIRO = 'cmdPdftocairo';
const SYSTEM_CMD_QPDF = 'cmdQpdf';
const SYSTEM_CMD_GS = 'cmdGs';
const SYSTEM_CMD_PDFUNITE = 'cmdPdfunite';
......
......@@ -336,7 +336,7 @@ class HelperFile {
public static function rename($oldname, $newname) {
if (false === @rename($oldname, $newname)) {
$msg = self::errorGetLastAsString() . " - rename($oldname ,$newname)";
$msg = self::errorGetLastAsString() . " - rename($oldname, $newname)";
throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'unlink failed', ERROR_MESSAGE_TO_DEVELOPER => $msg]), ERROR_IO_RENAME);
}
......
......@@ -1262,13 +1262,10 @@ class Save {
switch ($fileSplitType) {
case FE_FILE_SPLIT_SVG:
$cmd = 'pdf2svg "' . $newSrc . '" "' . $fileNameDest . '" all';
$cmd = $this->buildPdf2svg($newSrc, $fileNameDest);
break;
case FE_FILE_SPLIT_JPEG:
if ($fileSplitTypeOptions == '') {
$fileSplitTypeOptions = FE_FILE_SPLIT_OPTIONS_JPEG;
}
$cmd = "convert $fileSplitTypeOptions '$newSrc' '$fileNameDest'";
$cmd = $this->buildConvertSplit($newSrc, $fileNameDest, $fileSplitTypeOptions);
break;
default:
throw new \UserFormException("Unknown 'fileSplit' type: " . $formElement[FE_FILE_SPLIT], ERROR_UNKNOWN_TOKEN);
......@@ -1276,13 +1273,30 @@ class Save {
// Split PDF
HelperFile::chdir($tempDir);
$output = Support::qfqExec($cmd, $rc);
$cnt = 0;
do {
$output = Support::qfqExec($cmd, $rc);
$cnt++;
if ($rc != 0) {
// Split failed
$cmdRepair = $this->buildPdftocairo($newSrc, $newSrc . '.1');
$output .= PHP_EOL . $cmdRepair . PHP_EOL;
$output .= Support::qfqExec($cmdRepair, $rc1);
if ($rc1 != 0) {
// Repair failed too: stop here.
break;
}
HelperFile::rename($newSrc . '.1', $newSrc);
}
} while ($rc != 0 && $cnt < 2);
HelperFile::chdir($cwd);
if ($rc != 0) {
throw new \UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'pdf2svg failed', ERROR_MESSAGE_TO_DEVELOPER => "[$cwd][cmd=$cmd]$output"]),
ERROR_PDF2SVG);
json_encode([ERROR_MESSAGE_TO_USER => 'pdf split failed', ERROR_MESSAGE_TO_DEVELOPER => "[$cwd][cmd=$cmd]$output"]),
ERROR_PDF_SPLIT);
}
$files = Helperfile::getSplitFileNames($tempDir);
......@@ -1326,6 +1340,54 @@ class Save {
HelperFile::rmdir($tempDir);
}
/**
* @param $src
* @param $dest
* @return string
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
private function buildPdf2svg($src, $dest) {
$cmd = $this->store->getVar(SYSTEM_CMD_PDF2SVG, STORE_SYSTEM);
return "$cmd '$src' '$dest' all";
}
/**
* @param $src
* @param $dest
* @return string
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
private function buildPdftocairo($src, $dest) {
$cmd = $this->store->getVar(SYSTEM_CMD_PDFTOCAIRO, STORE_SYSTEM);
return "$cmd -pdf '$src' '$dest'";
}
/**
* @param $src
* @param $dest
* @param $options
* @return string
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
private function buildConvertSplit($src, $dest, $options) {
if ($options == '') {
$options = FE_FILE_SPLIT_OPTIONS_JPEG;
}
$cmd = $this->store->getVar(SYSTEM_CMD_CONVERT, STORE_SYSTEM);
return "$cmd $options '$src' '$dest'";
}
/**
* Create/update or delete the slave record.
*
......
......@@ -374,6 +374,8 @@ class Config {
SYSTEM_CMD_WKHTMLTOPDF => '/opt/wkhtmltox/bin/wkhtmltopdf',
SYSTEM_CMD_INKSCAPE => 'inkscape',
SYSTEM_CMD_CONVERT => 'convert',
SYSTEM_CMD_PDF2SVG => 'pdf2svg',
SYSTEM_CMD_PDFTOCAIRO => 'pdftocairo',
SYSTEM_CMD_QPDF => 'qpdf',
SYSTEM_CMD_GS => 'gs',
SYSTEM_CMD_PDFUNITE => 'pdfunite',
......
......@@ -345,6 +345,8 @@ class StoreTest extends TestCase {
SYSTEM_THUMBNAIL_DIR_PUBLIC => SYSTEM_THUMBNAIL_DIR_PUBLIC_DEFAULT,
SYSTEM_CMD_INKSCAPE => 'inkscape',
SYSTEM_CMD_CONVERT => 'convert',
SYSTEM_CMD_PDF2SVG => 'pdf2svg',
SYSTEM_CMD_PDFTOCAIRO => 'pdftocairo',
SYSTEM_CMD_WKHTMLTOPDF => '/opt/wkhtmltox/bin/wkhtmltopdf',
SYSTEM_DATE_FORMAT => 'yyyy-mm-dd',
SYSTEM_THROW_GENERAL_ERROR => 'no',
......
......@@ -22,9 +22,15 @@ thumbnailDirPublic = typo3temp/qfqThumbnail
# cat=config/config; type=string; label=Command 'inkscape':Default is 'inkscape'. Will be used to convert SVG to images (png). An empty string disables `inkscape`. If it is not available, `convert` will be used instead.
cmdInkscape = inkscape
# cat=config/config; type=string; label=Command 'convert':Default is 'convert'. Will be used to convert images to thumbnails. GraphicsMagics 'convert' is recommended.
# cat=config/config; type=string; label=Command 'convert':Default is 'convert'. Will be used to convert images to thumbnails and to split PDF to JPEG.
cmdConvert = convert
# cat=config/config; type=string; label=Command 'pdf2svg':Default is 'pdf2svg'. Will be used to convert PDF to SVG.
cmdPdf2svg = pdf2svg
# cat=config/config; type=string; label=Command 'pdftocairo':Default is 'pdftocairo'. Will be used to repair broken PDFs to be splitted via convert.
cmdPdftocairo = pdftocairo
# cat=config/config; type=string; label=Command 'wkhtmltopdf':Default is '/opt/wkhtmltox/bin/wkhtmltopdf'. Command to convert a HTML page to a PDF.
cmdWkhtmltopdf = /opt/wkhtmltox/bin/wkhtmltopdf
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment