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.