I have this part of a php program:
$docx_filename = "/var/www/editeur/docx/".$file1
$command5 = "python3 conv_docx_pdf.py "$docx_filename"";
$output = shell_exec($command5);
The script conv_docx_pdf.py:
import subprocess
import os
import sys
def docx_to_pdf(input_file):
# Get the file name without extension
file_name, _ = os.path.splitext(input_file)
# Construct the output file path with .pdf extension
output_file = file_name + ".pdf"
# Run unoconv to convert DOCX to PDF
subprocess.run(["unoconv", "-f", "pdf", input_file])
return output_file
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <input_file>")
sys.exit(1)
input_file = sys.argv[1] # Get the input file from command-line argument
output_file = docx_to_pdf(input_file)
print("Output PDF:", output_file)
The command (python3 conv_docx_pdf.py filename1.docx
) works fine when I execute it in ubuntu terminal. However, php is unable to execute it.
rights for folder docx:
drwxr-xr-x 2 www-data www-data 4096 Mar 26 12:58 docx
rights for file $file1
-rw-r–r– 1 www-data www-data 11738 Mar 26 11:45 status29024.docx
I have other python programs (that don’t use subprocess) that are working fine when called from php.
Any help is appreciated.
2
Answers
I thank you all for your help. I finally found the issue to be related to Unoconv and www-data permissions. I used the recommendations from this site.
The command that is generated by the code you provided is not
python3 conv_docx_pdf.py filename1.docx
butpython3 conv_docx_pdf.py "filename1.docx"
(please note the extra quotes).Try this code instead after adding a semicolon at the end of line 2: