我正在尝试使用Unoconv将.docx文件转换为.pdf文件。Libreoffice安装在我的服务器上,该脚本适用于服务器上的另一个网站。
使用行use Unoconv\Unoconv;会生成一个HTTP ERROR 500。
有人知道我为什么要买HTTP ERROR 500吗?
下面是我的脚本:
<?php
require './Unoconv.php';
use Unoconv\Unoconv;
$originFilePath = './uf/invoice/17/word/202100021.docx';
$outputDirPath = './uf/invoice/17/pdf/202100021.pdf';
Unoconv::convertToPdf($originFilePath, $outputDirPath);
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=202100021.pdf");
?>下面是我的Unoconv.php脚本:
<?php
namespace Unoconv;
class Unoconv {
public static function convert($originFilePath, $outputDirPath, $toFormat)
{
$command = 'unoconv --format %s --output %s %s';
$command = sprintf($command, $toFormat, $outputDirPath, $originFilePath);
system($command, $output);
return $output;
}
public static function convertToPdf($originFilePath, $outputDirPath)
{
return self::convert($originFilePath, $outputDirPath, 'pdf');
}
public static function convertToTxt($originFilePath, $outputDirPath)
{
return self::convert($originFilePath, $outputDirPath, 'txt');
}
}
?>发布于 2021-09-13 14:53:38
从使用try...catch包装代码开始,首先获取错误消息:
<?php
try {
require 'Unoconv.php';
use Unoconv\Unoconv;
$map1 = $_SESSION['companyid'];
$filename = $result1['filename'];
$originFilePath = './uf/doc/'.$map1.'/word/'.$filename.'.docx';
$outputDirPath = './uf/doc/'.$map1.'/pdf/'.$filename.'.pdf';
Unoconv::convertToPdf($originFilePath, $outputDirPath);
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=".$filename.".pdf");
readfile($outputDirPath);
} catch (\Exception $e) {
die($e->getMessage());
}发布于 2021-09-14 04:22:45
@Alex关于首先在try/catch中包装是正确的,但是语法应该是:
...
} catch(\Exception $e){
...发布于 2021-09-19 18:46:26
我观察到LibreOffice在进行转换时可能有点奇怪,特别是在when服务器帐户的无头模式下运行时。
最简单的尝试是修改unoconv,使其使用LibreOffice附带的相同Python二进制文件:
#!/usr/bin/env python应该是(在检查libreoffice安装位置之后)
#!/opt/libreoffice7.1/program/python另外,我已经通过直接调用libreoffice解决了这个问题(没有Unoconv的):
$dir = dirname($docfile);
// Libreoffice saves here
$pdf = $dir . DIRECTORY_SEPARATOR . basename($docfile, '.docx').'.pdf';
$ret = shell_exec("export HOME={$dir} && /usr/bin/libreoffice --headless --convert-to pdf --outdir '{$dir}' '{$docfile}' 2>&1");
if (file_exists($pdf)) {
rename($pdf, $realPDFName);
} else {
return false;
}
return true;所以你的代码会变成:
$originFilePath = './uf/invoice/17/word/202100021.docx';
$outputDirPath = './uf/invoice/17/pdf/202100021.pdf';
$dir = dirname($originFilePath);
$pdf = $dir . DIRECTORY_SEPARATOR . basename($originFilePath, '.docx').'.pdf';
$ret = shell_exec("export HOME={$dir} && /usr/bin/libreoffice --headless --convert-to pdf --outdir '{$dir}' '{$originFilePath}' 2>&1");
// $ret will contain any errors
if (!file_exists($pdf)) {
die("Conversion error: " . htmlentities($ret));
}
rename($pdf, $outputDirPath);
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=202100021.pdf");
readfile($outputDirPath);我假设libreoffice出现在通常的替代链接"/usr/bin/libreoffice“中,否则您需要使用"which libreoffice”的终端命令检索它的路径。或者,在php脚本中,
<?php
header('Content-Type: text/plain');
print "If this works:\n";
system('which libreoffice 2>&1');
print "\n-- otherwise a different attempt, returning too much information --\n";
system('locate libreoffice');https://stackoverflow.com/questions/69143646
复制相似问题