NEW The preview of PDF files generates 1 png file even if several pages.

This commit is contained in:
Laurent Destailleur 2019-03-24 14:53:26 +01:00
parent 949ef79227
commit 614b4afeb0
2 changed files with 14 additions and 17 deletions

View File

@ -1823,16 +1823,19 @@ function deleteFilesIntoDatabaseIndex($dir, $file, $mode = 'uploaded')
* @param string $fileinput Input file name
* @param string $ext Format of target file (It is also extension added to file if fileoutput is not provided).
* @param string $fileoutput Output filename
* @param string $page Page number if we convert a PDF into png
* @return int <0 if KO, 0=Nothing done, >0 if OK
*/
function dol_convert_file($fileinput, $ext = 'png', $fileoutput = '')
function dol_convert_file($fileinput, $ext = 'png', $fileoutput = '', $page='')
{
global $langs;
if (class_exists('Imagick'))
{
$image=new Imagick();
try {
$ret = $image->readImage($fileinput);
$filetoconvert=$fileinput.(($page != '')?'['.$page.']':'');
//var_dump($filetoconvert);
$ret = $image->readImage($filetoconvert);
} catch(Exception $e) {
$ext = pathinfo($fileinput, PATHINFO_EXTENSION);
dol_syslog("Failed to read image using Imagick (Try to install package 'apt-get install php-imagick ghostscript' and check there is no policy to disable ".$ext." convertion in /etc/ImageMagick*/policy.xml): ".$e->getMessage(), LOG_WARNING);

View File

@ -1386,26 +1386,28 @@ function dol_banner_tab($object, $paramid, $morehtml = '', $shownav = 1, $fieldi
$relativepath = $subdir.'/'.$objectref.'.pdf';
// Define path to preview pdf file (preview precompiled "file.ext" are "file.ext_preview.png")
$fileimage = $file.'_preview.png'; // If PDF has 1 page
$fileimagebis = $file.'_preview-0.png'; // If PDF has more than one page
$fileimage = $file.'_preview.png';
$relativepathimage = $relativepath.'_preview.png';
$pdfexists = file_exists($file);
// If PDF file exists
if (file_exists($file))
if ($pdfexists)
{
// Conversion du PDF en image png si fichier png non existant
if ( (! file_exists($fileimage) || (filemtime($fileimage) < filemtime($file)))
&& (! file_exists($fileimagebis) || (filemtime($fileimagebis) < filemtime($file)))
)
if (! file_exists($fileimage) || (filemtime($fileimage) < filemtime($file)))
{
if (empty($conf->global->MAIN_DISABLE_PDF_THUMBS)) // If you experience trouble with pdf thumb generation and imagick, you can disable here.
{
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$ret = dol_convert_file($file, 'png', $fileimage);
$ret = dol_convert_file($file, 'png', $fileimage, '0'); // Convert first page of PDF into a file _preview.png
if ($ret < 0) $error++;
}
}
}
if ($pdfexists && ! $error)
{
$heightforphotref=70;
if (! empty($conf->dol_optimize_smallscreen)) $heightforphotref=60;
// Si fichier png PDF d'1 page trouve
@ -1415,14 +1417,6 @@ function dol_banner_tab($object, $paramid, $morehtml = '', $shownav = 1, $fieldi
$phototoshow.= '<img height="'.$heightforphotref.'" class="photo photowithmargin photowithborder" src="'.DOL_URL_ROOT . '/viewimage.php?modulepart=apercu'.$modulepart.'&amp;file='.urlencode($relativepathimage).'">';
$phototoshow.= '</div></div>';
}
// Si fichier png PDF de plus d'1 page trouve
elseif (file_exists($fileimagebis))
{
$preview = preg_replace('/\.png/', '', $relativepathimage) . "-0.png";
$phototoshow = '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref">';
$phototoshow.= '<img height="'.$heightforphotref.'" class="photo photowithmargin photowithborder" src="'.DOL_URL_ROOT . '/viewimage.php?modulepart=apercu'.$modulepart.'&amp;file='.urlencode($preview).'"><p>';
$phototoshow.= '</div></div>';
}
}
}
elseif (! $phototoshow)