diff --git a/class.paperless.php b/class.paperless.php index 5c1e3ac..4546dae 100644 --- a/class.paperless.php +++ b/class.paperless.php @@ -993,15 +993,38 @@ class SeedDMS_ExtPaperless_RestAPI_Controller { /* {{{ */ if($document->getAccessMode($userobj) >= M_READ) { $lc = $document->getLatestContent(); if($lc) { - if (pathinfo($document->getName(), PATHINFO_EXTENSION) == $lc->getFileType()) - $filename = $document->getName(); - else - $filename = $document->getName().$lc->getFileType(); - $file = $dms->contentDir . $lc->getPath(); - if(!($fh = @fopen($file, 'rb'))) { - return $response->withJson(array('success'=>false, 'message'=>'', 'data'=>''), 500); + if(empty($settings->_extensions['paperless']['converttopdf']) || $lc->getMimeType() == 'application/pdf') { + if (pathinfo($document->getName(), PATHINFO_EXTENSION) == $lc->getFileType()) + $filename = $document->getName(); + else + $filename = $document->getName().$lc->getFileType(); + $file = $dms->contentDir . $lc->getPath(); + + if(!($fh = @fopen($file, 'rb'))) { + return $response->withJson(array('success'=>false, 'message'=>'', 'data'=>''), 500); + } + $filesize = filesize($dms->contentDir . $lc->getPath()); + } else { + $previewer = new SeedDMS_Preview_PdfPreviewer($settings->_cacheDir); + if($conversionmgr) + $previewer->setConversionMgr($conversionmgr); + else + $previewer->setConverters(isset($settings->_converters['pdf']) ? $settings->_converters['pdf'] : array()); + if(!$previewer->hasPreview($lc)) + $previewer->createPreview($lc); + if(!$previewer->hasPreview($lc)) { + $logger->log('Creating pdf preview failed', PEAR_LOG_ERR); + return $response->withJson('', 500); + } else { + $filename = $document->getName().".pdf"; + $file = $previewer->getFileName($lc).".pdf"; + $filesize = filesize($file); + if(!($fh = @fopen($file, 'rb'))) { + $logger->log('Creating pdf preview failed', PEAR_LOG_ERR); + return $response->withJson('', 500); + } + } } - $filesize = filesize($dms->contentDir . $lc->getPath()); $stream = new \Slim\Http\Stream($fh); // create a stream instance for the response body return $response->withHeader('Content-Type', $lc->getMimeType()) diff --git a/conf.php b/conf.php index 464ff13..40ceb34 100644 --- a/conf.php +++ b/conf.php @@ -66,6 +66,11 @@ $EXT_CONF['paperless'] = array( 'objtype'=>'2', 'allow_empty'=>true, ), + 'converttopdf' => array( + 'title'=>'Convert to PDF', + 'type'=>'checkbox', + 'help'=>"Enable, if you want non pdf documents to be converted to PDF if they are downloaded. Usually, this is only done for preview.", + ), ), 'constraints' => array( 'depends' => array('php' => '7.4.0-', 'seeddms' => array('5.1.31-5.1.99', '6.0.24-6.0.99')),