CentOS7下浏览器运行PHP代码转换Docx到HTML失败求助
Hey there, let’s work through this Docx-to-PDF conversion issue you’re facing. I’ve debugged similar LibreOffice/PHP integration problems on CentOS before, so here’s a step-by-step breakdown of what might be going wrong and how to fix it:
1. Verify LibreOffice Installation & Command Line Functionality
First, make sure LibreOffice Headless is installed on your system—this is required for the soffice command to work. Run this in your terminal to install it:
yum install libreoffice-headless libreoffice-writer
Next, test the conversion command directly in the terminal (not via PHP) to rule out command-specific issues:
export HOME=/var/www/html/ && soffice --headless --convert-to pdf --outdir /var/www/html/ /var/www/html/xyz.docx
Check if the PDF file is generated in /var/www/html/. If this fails, note the error message—this will tell you if the issue is with LibreOffice itself, not PHP.
2. Fix File & Directory Permissions
PHP runs under the apache user (by default on CentOS 7), so this user needs read access to your .docx file and write access to the output directory. Run these commands to set the correct permissions:
# Give apache ownership of the input file chown apache:apache /var/www/html/xyz.docx # Set read permissions for the file chmod 644 /var/www/html/xyz.docx # Ensure the output directory is writable by apache chmod 755 /var/www/html/
Also, the HOME directory you’re setting (/var/www/html/) needs to be accessible to apache—make sure there are no restrictive permissions blocking access here.
3. Use Absolute Paths for soffice
Sometimes the soffice executable isn’t in the PATH of the apache user. Find its absolute path first:
which soffice
Then update your PHP code to use this full path (e.g., /usr/bin/soffice instead of just soffice).
4. Capture Error Output
Your original code only captures standard output, but error messages go to standard error. Modify the command to redirect errors to standard output so you can see what’s failing:
// Set the HOME environment variable directly in PHP putenv('HOME=/var/www/html/'); // Use the absolute path to soffice (replace with your actual path) $command = '/usr/bin/soffice --headless --convert-to pdf --outdir /var/www/html/ /var/www/html/xyz.docx 2>&1'; $result = shell_exec($command); // Print the full output (including errors) echo "<pre>" . htmlspecialchars($result) . "</pre>";
This will show you any specific errors (like missing dependencies, permission issues, or LibreOffice crashes).
5. Check for Running LibreOffice Processes & SELinux
- Kill Stuck Processes: LibreOffice headless can sometimes leave orphaned processes running, which block new conversions. Run this to clean them up:
pkill soffice - SELinux Restrictions: CentOS 7 has SELinux enabled by default, which might block
apachefrom executingsoffice. Test this by temporarily disabling SELinux:
If the conversion works after this, you’ll need to add a permanent SELinux rule instead of leaving it disabled. Run this to allowsetenforce 0apacheto execute memory-based binaries (required for LibreOffice):setsebool -P httpd_execmem on
Start with the command line test first—this is the fastest way to isolate whether the problem is with the conversion command itself or the PHP/apache setup. Once that works, move through the permission and environment checks.
内容的提问来源于stack exchange,提问作者Selva Saravanan




