Don't check for windows/linux, just call soffice (without the extension) will work on windows too.

This commit is contained in:
Gerhard Stephan 2018-02-15 14:38:21 +01:00
commit 520f581481

View File

@ -561,7 +561,7 @@ IMG;
* Convert the ODT file to PDF and export the file as attached file by HTTP * Convert the ODT file to PDF and export the file as attached file by HTTP
* Note: you need to have JODConverter and OpenOffice or LibreOffice installed and executable on the same system as where this php script will be executed. You also need to chmod +x odt2pdf.sh * Note: you need to have JODConverter and OpenOffice or LibreOffice installed and executable on the same system as where this php script will be executed. You also need to chmod +x odt2pdf.sh
* *
* @param string $name (optional) * @param string $name Name of ODT file to generate before generating PDF
* @throws OdfException * @throws OdfException
* @return void * @return void
*/ */
@ -574,22 +574,19 @@ IMG;
dol_syslog(get_class($this).'::exportAsAttachedPDF $name='.$name, LOG_DEBUG); dol_syslog(get_class($this).'::exportAsAttachedPDF $name='.$name, LOG_DEBUG);
$this->saveToDisk($name); $this->saveToDisk($name);
// Export to PDF using LibreOffice
if ($conf->global->MAIN_ODT_AS_PDF == 'libreoffice')
{
// Executing convert to PDF using libreoffice 5 (using windows libreoffice must be in path)
$command ='soffice.exe -headless -convert-to pdf -outdir '. escapeshellarg(dirname($name)). " ".escapeshellarg($name);
exec($command, $output_arr, $retval);
// Split extension from name to allow deleting the source using MAIN_ODT_AS_PDF_DEL_SOURCE
$name=preg_replace('/\.odt/i', '', $name);
}
else
{
$execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2); // 1 or 2 $execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2); // 1 or 2
// Method 1 sometimes hang the server. // Method 1 sometimes hang the server.
if (preg_match('/unoconv/', $conf->global->MAIN_ODT_AS_PDF))
// Export to PDF using LibreOffice
if ($conf->global->MAIN_ODT_AS_PDF == 'libreoffice')
{
// using windows libreoffice that must be in path
// using linux/mac libreoffice that must be in path
// Note PHP Config "fastcgi.impersonate=0" must set to 0 - Default is 1
$command ='soffice -headless -convert-to pdf -outdir '. escapeshellarg(dirname($name)). " ".escapeshellarg($name);
}
elseif (preg_match('/unoconv/', $conf->global->MAIN_ODT_AS_PDF))
{ {
// If issue with unoconv, see https://github.com/dagwieers/unoconv/issues/87 // If issue with unoconv, see https://github.com/dagwieers/unoconv/issues/87
@ -611,8 +608,8 @@ IMG;
//DEBUG: Terminating LibreOffice instance. //DEBUG: Terminating LibreOffice instance.
//DEBUG: Waiting for LibreOffice instance to exit //DEBUG: Waiting for LibreOffice instance to exit
// It fails: // If it fails:
// - set shel of user to bash instead of nologin. // - set shell of user to bash instead of nologin.
// - set permission to read/write to user on home directory /var/www so user can create the libreoffice , dconf and .cache dir and files then set permission back // - set permission to read/write to user on home directory /var/www so user can create the libreoffice , dconf and .cache dir and files then set permission back
$command = $conf->global->MAIN_ODT_AS_PDF.' '.escapeshellcmd($name); $command = $conf->global->MAIN_ODT_AS_PDF.' '.escapeshellcmd($name);
@ -621,16 +618,16 @@ IMG;
else else
{ {
// deprecated old method // deprecated old method
$name=preg_replace('/\.odt/i', '', $name); $tmpname=preg_replace('/\.odt/i', '', $name);
if (!empty($conf->global->MAIN_DOL_SCRIPTS_ROOT)) if (!empty($conf->global->MAIN_DOL_SCRIPTS_ROOT))
{ {
$command = $conf->global->MAIN_DOL_SCRIPTS_ROOT.'/scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($name).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF); $command = $conf->global->MAIN_DOL_SCRIPTS_ROOT.'/scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($tmpname).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF);
} }
else else
{ {
dol_syslog(get_class($this).'::exportAsAttachedPDF is used but the constant MAIN_DOL_SCRIPTS_ROOT with path to script directory was not defined.', LOG_WARNING); dol_syslog(get_class($this).'::exportAsAttachedPDF is used but the constant MAIN_DOL_SCRIPTS_ROOT with path to script directory was not defined.', LOG_WARNING);
$command = '../../scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($name).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF); $command = '../../scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($tmpname).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF);
} }
} }
@ -664,7 +661,6 @@ IMG;
} }
if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK)); if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
} }
}
if ($retval == 0) if ($retval == 0)
{ {
@ -674,12 +670,15 @@ IMG;
} }
if (!empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) { if (!empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
$name=preg_replace('/\.od(x|t)/i', '', $name);
header('Content-type: application/pdf'); header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$name.'.pdf"'); header('Content-Disposition: attachment; filename="'.$name.'.pdf"');
readfile("$name.pdf"); readfile($name.".pdf");
} }
if (!empty($conf->global->MAIN_ODT_AS_PDF_DEL_SOURCE)) if (!empty($conf->global->MAIN_ODT_AS_PDF_DEL_SOURCE))
unlink("$name.odt"); {
unlink($name);
}
} else { } else {
dol_syslog(get_class($this).'::exportAsAttachedPDF $ret_val='.$retval, LOG_DEBUG); dol_syslog(get_class($this).'::exportAsAttachedPDF $ret_val='.$retval, LOG_DEBUG);
dol_syslog(get_class($this).'::exportAsAttachedPDF $output_arr='.var_export($output_arr,true), LOG_DEBUG); dol_syslog(get_class($this).'::exportAsAttachedPDF $output_arr='.var_export($output_arr,true), LOG_DEBUG);