0

I have a python script that generates a PDF file according to the information provided. I created an application in nodejs so that at each POST request, the script is executed generating a new PDF file. The file is saved inside the "public" folder and in response, the request returns this PDF file. This file is displayed on an HTML page where it is possible to provide the necessary information for generating the PDF.

NODE JS

router.post('/', (req, res) => {
    var data = req.body;
    console.log(data)
    const python = spawn('python3', ['public/python/pdf.py', data.nSchool, data.nSubject, data.nProf, data.nQue]);

    python.on('close', (code) => {
         console.log(`${code}`);

        var file = fs.createReadStream("./public/pdf/gab.pdf");
        res.contentType('application/pdf')
        file.pipe(res);
    });
});

JAVASCRIPT

document.querySelector('#submit').onclick = function(){
    fetch('/makepdf', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            nSchool: document.querySelector('#nSchool').value,
            nSubject: document.querySelector('#nSubject').value,
            nProf: document.querySelector('#nProf').value,
            nQue: document.querySelector('#nQue').value
        })
    }).then(function(res) {
        res.blob().then(function(resq){
            var fileURL = URL.createObjectURL(resq)
            document.querySelector('#img').src = fileURL
        });
    });
}

The application works perfectly while it is running locally, but when it is hosted, the PDF file is not applied.

Where is the problem? When generating the file? (Maybe the server is not running the python script correctly) Or saving the file? (As it is a static folder it is not possible to solve the problem in the way I imagined?)

What would be the most efficient way to create such an application?

Sorry for the bad English, I'm using a translator.

Edubgr
  • 13
  • 1
  • 5
  • What error did the server output when hosted on? Please add it to the post. – Akihito KIRISAKI Dec 26 '20 at 13:49
  • make a public directory manually on the server. – Ahmed Magdy Dec 26 '20 at 13:54
  • No error is generated, the file is simply not displayed. Check the server hosted on this link https://morning-gorge-14494.herokuapp.com/users The displayed file was the one that already existed, not the new one that was supposedly generated. – Edubgr Dec 26 '20 at 13:56
  • How do I do that? – Edubgr Dec 26 '20 at 13:58
  • If it works locally, but it doesn't on the server, the problem most probably is in difference in configuration between your local environment and the server. Check if the output from `console.log(${code});` is `0` – Beniamin H Dec 26 '20 at 14:04
  • I am receiving code 1 for the hosted server and code 0 for the location. Does this mean that the script is not being executed correctly? – Edubgr Dec 26 '20 at 14:19
  • Exactly - `0` means ok, `>0` means error. Can you put contents of `public/python/pdf.py` file into the question ? – Beniamin H Dec 26 '20 at 14:47
  • The code is large and very poorly written, but you can get an idea. https://github.com/Edubgr/Corretor.git – Edubgr Dec 26 '20 at 16:05
  • Can you run `python3 public/python/pdf.py` in the server shell ? – Beniamin H Dec 26 '20 at 16:44
  • I get it stderr: Traceback (most recent call last): File "public/python/pdf.py", line 1, in from reportlab.pdfgen import canvas ModuleNotFoundError: No module named 'reportlab' – Edubgr Dec 26 '20 at 17:45

1 Answers1

0

You can capture error stream from spawned process like this:

python.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

This should tell you why the python script execution fails.

Btw. you should move your python file from public/ directory, as it makes it unnecessarily public available.

Beniamin H
  • 2,048
  • 1
  • 11
  • 17
  • 2020-12-26T17:40:04.904938+00:00 app[web.1]: stderr: Traceback (most recent call last): 2020-12-26T17:40:04.904941+00:00 app[web.1]: File "public/python/pdf.py", line 1, in 2020-12-26T17:40:04.904941+00:00 app[web.1]: from reportlab.pdfgen import canvas 2020-12-26T17:40:04.904942+00:00 app[web.1]: ModuleNotFoundError: No module named 'reportlab' – Edubgr Dec 26 '20 at 17:41
  • The error is that the library does not exist on the server. How should I fix it? – Edubgr Dec 26 '20 at 17:42
  • https://pypi.org/project/reportlab/ - but you need an access to install on the server (like command line access + virtual env). – Beniamin H Dec 26 '20 at 17:45
  • How can I do this? I created an environment for python and installed the reportlab library, but it still doesn't work. I updated the repository, please check. – Edubgr Dec 26 '20 at 18:08
  • Now you need to use python from the virt environment. This may help you: https://stackoverflow.com/questions/57875421/activating-an-virtual-environment-from-python-in-nodejs – Beniamin H Dec 26 '20 at 18:14
  • stderr: environments/my_env/bin/python3: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by environments/my_env/bin/python3) a new mistake – Edubgr Dec 26 '20 at 18:38
  • Did you install virtual env in the server or in your local machine ? Where do you type commands - locally or on the server ? – Beniamin H Dec 26 '20 at 18:44
  • Installed on the local machine but sent the folder to the server. – Edubgr Dec 26 '20 at 18:47
  • Sorry for the ignorance, I'm still a beginner. – Edubgr Dec 26 '20 at 18:49
  • Unfortunately it doesn't work that way - you need an access to the server command line, to execute commands to create virt env and install dependencies there. – Beniamin H Dec 26 '20 at 18:49
  • Np :) nice try anyway :) - you need a server with a possibility to install python packages on it - it won't work in another way. – Beniamin H Dec 26 '20 at 18:51
  • Actually there may be some workarounds I'm not aware of - like: https://stackoverflow.com/questions/48746494/how-to-use-a-packed-python-package-without-installing-it - but changing the server may turn out to be simpler. – Beniamin H Dec 26 '20 at 18:56
  • To create the environment I need to install python-venv. However, I can't get through the server terminal, should I look for another hosting? – Edubgr Dec 26 '20 at 19:06
  • Yes, unfortunately. – Beniamin H Dec 26 '20 at 19:10
  • I understand, I will work on it, now I have a better idea of ​​my problem. Thank you very much :) Now one last thing, is the way the file is saved by the python script and read by the node until it is sent to the html page efficient and functional? – Edubgr Dec 26 '20 at 19:17
  • It seems to be ok - as long as you need that file to be kept - if not - you can just "pipe" pdf content to nodejs by returning it from python script (to stdout for example) instead of saving to disk. Alternatively you don't need to transfer the file by nodejs at all - just let your frontend js know, that it's done and serve the file by your static file server - just like an image or css. – Beniamin H Dec 26 '20 at 19:27
  • Please accept my posted answer, if it's okay with you :) – Beniamin H Dec 26 '20 at 19:46