I have a custom LAMP stack application on a DigitalOcean droplet. But I now need to run a python script to do a PDF conversion :
python src/exportpdf/export_pdf_to_docx.py
How do I set this up in such a way that, on clicking a button on the PHP served webpage, I get this python script to be called and download the DOCX ?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi there,
You can call the Python script from PHP using
shell_exec()
orexec()
, but you should be very careful. Running system commands from a web app has security implications.But a basic example of this would be:
To let users download the DOCX, have the Python script save it to a secure, known directory, and then use PHP to serve the file using
header()
:On the security side, make sure to always use absolute paths in both PHP and Python to avoid unexpected behavior.
Also, never, but really never, pass user input directly to the shell command.
You could also make sure the Python script can’t be modified by external users. For example, rRun the script under limited permissions, eg. avoid giving
www-data
full access to your system.You can also store the generated files outside of your web root if possible.
Also double-check that your Droplet has Python, the required packages, and environment variables configured properly for the PDF SDK to work:
- Bobby