1

I'm trying to run a python script from PHP. The python script reads the contents from a text file and creates a PDF accordingly. When I run the Python script from terminal, it works perfectly and I get the required PDF as expected. But when I try to run the python script from PHP it gives me the following error:

Traceback (most recent call last): File "/home/amogh/server/test.py", line 3, in from fpdf import FPDF ImportError: No module named fpdf 

PHP Code:

<?php
$output = shell_exec("/home/amogh/server/test.py 2>&1");
echo $output;
?>

Python code:

#! /usr/bin/python

from fpdf import FPDF
fp =  open('downloads/boot.txt', 'r')
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', '', 11)
line = fp.read() 
pdf.multi_cell(200, 5, line, 0, 1)
pdf.output('test.pdf', 'F')

I've installed fpdf using this command:

pip install fpdf 

I'm running my PHP files on lighttpd server. Can anyone tell me where am I going wrong?

toti08
  • 2,448
  • 5
  • 24
  • 36
Ryder389
  • 43
  • 1
  • 1
  • 12
  • @Pynchia yes, I've tried that as well. Still gives me the same error – Ryder389 Oct 09 '18 at 11:12
  • 1
    Have you tried to check which python interpreter is used when calling directly from the terminal and when running through PHP? Next step would be to compare the content of `sys.path` for both ways of execution – FlyingTeller Oct 09 '18 at 11:16
  • @FlyingTeller Both the ways (PHP script as well as Terminal) show the same path `/usr/bin/python`. I also compared the contents for `sys.path`, they are all same – Ryder389 Oct 09 '18 at 12:13
  • When running a python script with `import fpdf: print(fdpf.__file__)`, what does it print? Is the path listed also in the `sys.path` when you are running a python script using php? – FlyingTeller Oct 09 '18 at 13:22
  • @FlyingTeller the paths seem to be same.. My code works now.. I changed the user in my server from `www-data` to `amogh` – Ryder389 Oct 10 '18 at 05:33
  • what was the path? If switching users solved the issue, then the folder was probably not accessible for the other user – FlyingTeller Oct 10 '18 at 07:59

1 Answers1

1

It may be because of the different users running the script. I think that when you run it from php it is executed as user www-data

Maybe you ran pip from a virtualenv or for some other reason www-data user doesn't have access to fpdf module.

Edit:

Useful links from the comments:

How to install Python Package for global use by all users (incl. www-data)

http://nocurve.com/2018/04/23/running-python-script-from-php-as-www-data/

Santiago Bruno
  • 461
  • 2
  • 9
  • If that's the case then how do I change the user of fpdf module to `www-data`? – Ryder389 Oct 09 '18 at 12:17
  • I think one possibility could be to log in as www-data user in a terminal and run pip install fpdf Check this links, they seem related: https://stackoverflow.com/questions/39471295/how-to-install-python-package-for-global-use-by-all-users-incl-www-data http://nocurve.com/2018/04/23/running-python-script-from-php-as-www-data/ – Santiago Bruno Oct 09 '18 at 12:24
  • It worked! changed the user in my server.. thank youu :D – Ryder389 Oct 10 '18 at 05:31
  • 1
    Excellent! Can you mark the answer as correct? Someone downvoted it but it is correct. – Santiago Bruno Oct 10 '18 at 12:29