问题
I'm trying to convert an Excel File to PDF from a NodeJS App. It works when I start Node in command line, but when I start my app.bat as Windows Service through NSSM or directly with the Tasks Scheduler, it doesn't work anymore.
With the script bellow, I can only see the first row in the log.txt which is the program arguments. If everything was going well, I should see 0, then 1, and 2. Sadly, when Node runs in background, I don't have any number, so i'm guessing the problem is CreateObject("Excel.Application")
but i don't know why and how to resolve it.
VBScript :
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile="C:\Users\admin\Documents\Projects\tools\log.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write Wscript.Arguments.Item(0) & " | " & WScript.Arguments.Item(1) & vbCrLf
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
objFile.Write "0" & vbCrLf
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
objFile.Write "1" & vbCrLf
oBook.ExportAsFixedFormat xlTypePDF, WScript.Arguments.Item(1)
objFile.Write "2" & vbCrLf
oBook.Close True
NodeJS:
const {exec} = require('child_process');
exec('"' + Meteor.settings.directories.tools + 'convertToPDF.vbs" "' + outputServerFilePathChild + '.xlsx" "' + outputServerFilePathChild + '.pdf"', (err, stdout, stderr) => {
if (err) {
console.log(stderr);
reject(new Error(stderr));
} else {
if (fs.existsSync(outputServerFilePathChild.replace(/ /g, "%20") + ".pdf")) {
console.log('rename "' + outputServerFilePathChild.replace(/ /g, "%20") + '.pdf" "' + outputServerFilePathChild + '.pdf"');
exec('rename "' + outputServerFilePathChild.replace(/ /g, "%20") + '.pdf" "' + outputServerFilePathChild + '.pdf"', (err, stdout, stderr) => {
console.log("File generated: " + outputServerFilePathChild + ".pdf");
resolve(outputClientFilePathChild + ".pdf");
});
} else {
console.log("File generated: " + outputServerFilePathChild + ".pdf");
resolve(outputClientFilePathChild + ".pdf");
}
}
});
I also tried things like this :
const child2 = spawn('start', [
'"PDFConverter"',
'cscript',
'"C:\\Users\\admin\\Documents\\Projects\\tools\\convertToPDF.vbs"',
'"C:\\Users\\admin\\Documents\\Projects\\reports\\admin\\rebates\\customer1 2019-09-30.xlsx"',
'"C:\\Users\\admin\\Documents\\Projects\\reports\\admin\\rebates\\customer1 2019-09-30.pdf"'
], {
shell: true,
windowsHide: true,
});
However, it's not working either. Does someone have an idea ?
EDIT: This problem seems unresolvable. Some people have asked the same question many years ago, and there is still no answer ...
Node.js as service, exec doesn't work
nodejs service -in windows system ,unable to execute jar files
来源:https://stackoverflow.com/questions/58750660/execute-vbs-from-nodejs-in-background-mode-tasks-scheduler-or-windows-service