Export to PDF using LibreOffice 5 command line
This commit is contained in:
parent
f93934905f
commit
51028c845d
@ -574,83 +574,96 @@ 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);
|
||||||
|
|
||||||
$execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2); // 1 or 2
|
// Export to PDF using LibreOffice
|
||||||
// Method 1 sometimes hang the server.
|
if ($conf->global->MAIN_ODT_AS_PDF == 'libreoffice')
|
||||||
|
|
||||||
if (preg_match('/unoconv/', $conf->global->MAIN_ODT_AS_PDF))
|
|
||||||
{
|
{
|
||||||
// If issue with unoconv, see https://github.com/dagwieers/unoconv/issues/87
|
// 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);
|
||||||
|
|
||||||
// MAIN_ODT_AS_PDF should be "sudo -u unoconv /usr/bin/unoconv" and userunoconv must have sudo to be root by adding file /etc/sudoers.d/unoconv with content www-data ALL=(unoconv) NOPASSWD: /usr/bin/unoconv .
|
// Split extension from name to allow deleting the source using MAIN_ODT_AS_PDF_DEL_SOURCE
|
||||||
|
$name=preg_replace('/\.odt/i', '', $name);
|
||||||
// Try this with www-data user: /usr/bin/unoconv -vvvv -f pdf /tmp/document-example.odt
|
|
||||||
// It must return:
|
|
||||||
//Verbosity set to level 4
|
|
||||||
//Using office base path: /usr/lib/libreoffice
|
|
||||||
//Using office binary path: /usr/lib/libreoffice/program
|
|
||||||
//DEBUG: Connection type: socket,host=127.0.0.1,port=2002;urp;StarOffice.ComponentContext
|
|
||||||
//DEBUG: Existing listener not found.
|
|
||||||
//DEBUG: Launching our own listener using /usr/lib/libreoffice/program/soffice.bin.
|
|
||||||
//LibreOffice listener successfully started. (pid=9287)
|
|
||||||
//Input file: /tmp/document-example.odt
|
|
||||||
//unoconv: file `/tmp/document-example.odt' does not exist.
|
|
||||||
//unoconv: RuntimeException during import phase:
|
|
||||||
//Office probably died. Unsupported URL <file:///tmp/document-example.odt>: "type detection failed"
|
|
||||||
//DEBUG: Terminating LibreOffice instance.
|
|
||||||
//DEBUG: Waiting for LibreOffice instance to exit
|
|
||||||
|
|
||||||
// It fails:
|
|
||||||
// - set shel 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
|
|
||||||
|
|
||||||
$command = $conf->global->MAIN_ODT_AS_PDF.' '.escapeshellcmd($name);
|
|
||||||
//$command = '/usr/bin/unoconv -vvv '.escapeshellcmd($name);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// deprecated old method
|
$execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2); // 1 or 2
|
||||||
$name=preg_replace('/\.odt/i', '', $name);
|
// Method 1 sometimes hang the server.
|
||||||
|
|
||||||
if (!empty($conf->global->MAIN_DOL_SCRIPTS_ROOT))
|
if (preg_match('/unoconv/', $conf->global->MAIN_ODT_AS_PDF))
|
||||||
{
|
{
|
||||||
$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);
|
// If issue with unoconv, see https://github.com/dagwieers/unoconv/issues/87
|
||||||
|
|
||||||
|
// MAIN_ODT_AS_PDF should be "sudo -u unoconv /usr/bin/unoconv" and userunoconv must have sudo to be root by adding file /etc/sudoers.d/unoconv with content www-data ALL=(unoconv) NOPASSWD: /usr/bin/unoconv .
|
||||||
|
|
||||||
|
// Try this with www-data user: /usr/bin/unoconv -vvvv -f pdf /tmp/document-example.odt
|
||||||
|
// It must return:
|
||||||
|
//Verbosity set to level 4
|
||||||
|
//Using office base path: /usr/lib/libreoffice
|
||||||
|
//Using office binary path: /usr/lib/libreoffice/program
|
||||||
|
//DEBUG: Connection type: socket,host=127.0.0.1,port=2002;urp;StarOffice.ComponentContext
|
||||||
|
//DEBUG: Existing listener not found.
|
||||||
|
//DEBUG: Launching our own listener using /usr/lib/libreoffice/program/soffice.bin.
|
||||||
|
//LibreOffice listener successfully started. (pid=9287)
|
||||||
|
//Input file: /tmp/document-example.odt
|
||||||
|
//unoconv: file `/tmp/document-example.odt' does not exist.
|
||||||
|
//unoconv: RuntimeException during import phase:
|
||||||
|
//Office probably died. Unsupported URL <file:///tmp/document-example.odt>: "type detection failed"
|
||||||
|
//DEBUG: Terminating LibreOffice instance.
|
||||||
|
//DEBUG: Waiting for LibreOffice instance to exit
|
||||||
|
|
||||||
|
// It fails:
|
||||||
|
// - set shel 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
|
||||||
|
|
||||||
|
$command = $conf->global->MAIN_ODT_AS_PDF.' '.escapeshellcmd($name);
|
||||||
|
//$command = '/usr/bin/unoconv -vvv '.escapeshellcmd($name);
|
||||||
}
|
}
|
||||||
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);
|
// deprecated old method
|
||||||
$command = '../../scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($name).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF);
|
$name=preg_replace('/\.odt/i', '', $name);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//$dirname=dirname($name);
|
if (!empty($conf->global->MAIN_DOL_SCRIPTS_ROOT))
|
||||||
//$command = DOL_DOCUMENT_ROOT.'/includes/odtphp/odt2pdf.sh '.$name.' '.$dirname;
|
|
||||||
|
|
||||||
dol_syslog(get_class($this).'::exportAsAttachedPDF $execmethod='.$execmethod.' Run command='.$command,LOG_DEBUG);
|
|
||||||
if ($execmethod == 1)
|
|
||||||
{
|
|
||||||
exec($command, $output_arr, $retval);
|
|
||||||
}
|
|
||||||
if ($execmethod == 2)
|
|
||||||
{
|
|
||||||
$outputfile = DOL_DATA_ROOT.'/odt2pdf.log';
|
|
||||||
|
|
||||||
$ok=0;
|
|
||||||
$handle = fopen($outputfile, 'w');
|
|
||||||
if ($handle)
|
|
||||||
{
|
|
||||||
dol_syslog(get_class($this)."Run command ".$command,LOG_DEBUG);
|
|
||||||
fwrite($handle, $command."\n");
|
|
||||||
$handlein = popen($command, 'r');
|
|
||||||
while (!feof($handlein))
|
|
||||||
{
|
{
|
||||||
$read = fgets($handlein);
|
$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);
|
||||||
fwrite($handle, $read);
|
}
|
||||||
$output_arr[]=$read;
|
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);
|
||||||
|
$command = '../../scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($name).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF);
|
||||||
}
|
}
|
||||||
pclose($handlein);
|
|
||||||
fclose($handle);
|
|
||||||
}
|
}
|
||||||
if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
|
|
||||||
|
//$dirname=dirname($name);
|
||||||
|
//$command = DOL_DOCUMENT_ROOT.'/includes/odtphp/odt2pdf.sh '.$name.' '.$dirname;
|
||||||
|
|
||||||
|
dol_syslog(get_class($this).'::exportAsAttachedPDF $execmethod='.$execmethod.' Run command='.$command,LOG_DEBUG);
|
||||||
|
if ($execmethod == 1)
|
||||||
|
{
|
||||||
|
exec($command, $output_arr, $retval);
|
||||||
|
}
|
||||||
|
if ($execmethod == 2)
|
||||||
|
{
|
||||||
|
$outputfile = DOL_DATA_ROOT.'/odt2pdf.log';
|
||||||
|
|
||||||
|
$ok=0;
|
||||||
|
$handle = fopen($outputfile, 'w');
|
||||||
|
if ($handle)
|
||||||
|
{
|
||||||
|
dol_syslog(get_class($this)."Run command ".$command,LOG_DEBUG);
|
||||||
|
fwrite($handle, $command."\n");
|
||||||
|
$handlein = popen($command, 'r');
|
||||||
|
while (!feof($handlein))
|
||||||
|
{
|
||||||
|
$read = fgets($handlein);
|
||||||
|
fwrite($handle, $read);
|
||||||
|
$output_arr[]=$read;
|
||||||
|
}
|
||||||
|
pclose($handlein);
|
||||||
|
fclose($handle);
|
||||||
|
}
|
||||||
|
if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($retval == 0)
|
if ($retval == 0)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user