<?php // BISMILLAHIRRAHMANIRRAHEEM
error_reporting(E_ALL);
ini_set("display_errors", 1);
// tried the following with soffice, soffice.com, soffice.exe, soffice.bin
// MS-DOS: soffice, soffice.com, soffice.bin all work successufully and generate the pdf
$ex = "soffice.com --headless --convert-to html \"" . getcwd() . DIRECTORY_SEPARATOR . "sources" . DIRECTORY_SEPARATOR . "test.docx\" --outdir \"" . getcwd() . DIRECTORY_SEPARATOR . "results\"";// 2>&1";
chdir("C:\Program Files\LibreOffice\program");
echo $ex . "<br />";
exec($ex, $output);
//exec("mkdir rr", $output); // generates directory
//exec("dir soff*", $output);
//exec("a.bat", $output); // doesn't generate pdf
print_r($output);
?>嗨,我使用的是带有Windows 7的IIS,PHP 5.6.39。我无法通过调用LibreOffice将docx文档转换为pdf。如果在MS窗口中执行,则代码示例中屏幕上的命令输出可以工作,但在php的exec()或shell_exec()中不工作。该文件夹似乎具有适当的权限,因为我的上载脚本和exec("mkdir newdir")正在工作。exec("dir");还输出上述路径的目录列表,因此DOS导航命令似乎也能工作。
我想在避免使用解决方案上做另一个用户。不优雅。这不应该是困难的安沙阿拉。
如果不工作,我的意思是exec()既不生成文档,也不生成输出/错误,这给调试带来了困难。
谢谢您抽时间见我。
在我发布的链接中,似乎需要创建新用户。是否有可能IIS对目录有权限,但没有对PHP exec()的权限?
77,权限被拒绝?发布于 2020-04-21 03:07:51
比斯米拉
ALHAMDOLILLAH经过多次搜索,终于找到了答案。
Windows:
我在前面做了一些权限等等,对于IIS来说应该是微不足道的。但最终解决问题的是这。
显然,需要指定-env:UserInstallation=file:///C:\some\path,因为如果不指定不同的配置文件文件夹(参考文献),就不能同时运行多个LibreOffice实例。
另外,请参见相同的解决方案这里。
通过PHP通过IIS执行程序时,Windows的用户帐户是IUSR帐户。
所以,
C:\inetpub\wwwroot\path目录。soffice.com目录更改为soffice.com路径(如果在执行时提供完整路径,则不需要):chdir("C:\\Program Files\\LibreOffice\\program");wwwroot (这个步骤可以是可选的):putenv("HOME=C:\\inetpub\\wwwroot\\temp");exec("\"C:\\Program Files\\LibreOffice\\program\\soffice.com\" --headless \"-env:UserInstallation=file:///C:/inetpub/wwwroot/LibreOfficeProfilePath1\" --convert-to pdf:writer_pdf_Export --outdir \"C:\\inetpub\\wwwroot\\result\" \"C:\\inetpub\\wwwroot\\source\\file.docx\"", $output, $ret);
// Displaying the command output
print_r($output);
echo "<br />\n\n<br />"; // Line breaks for view-source
echo "Return code: " . $ret;exec("rmdir /S /Q \"C:\\inetpub\\wwwroot\\LibreOfficeProfilePath1\"");您可以自动生成此配置文件路径,例如:
// Do this before executing soffice command and pass it to -env
$profilepath = "C:/inetpub/wwwroot/LibreOfficeProfilePath" . date("YmdHis") . rand(0, 999999);
// Make sure to replace '/' with '\' when deleting
exec("rmdir /S /Q \"" . str_replace("/", "\\", $profilepath) . "\""); // Windows
exec("rm -rf \"" . str_replace("/", "\\", $profilepath) . "\"); // Linux或者使用PHP删除包含文件的目录,如这里所示。
作为服务运行的LibreOffice和请求文档转换的PHP可能是一个更好的解决方案,但这篇文章涉及到每次需要文档转换时调用LibreOffice,然后让它关闭。还可以使用单独的流程方法来避免等待LibreOffice完成转换。
Linux:
www-data也有类似的步骤,因为这是Linux中apache的用户帐户。请参阅解决方案这里。env路径将类似于:\"-env:UserInstallation=file:///var/www/tmp\"请注意,尽管Linux中的路径以/开头,或者可能忽略了file:中的斜杠,但在我看来,不管是Windows还是Linux,都需要注意三个file:之后的路径。
安沙安拉,在那之后它会起作用的。
https://stackoverflow.com/questions/60895795
复制相似问题