NickNaso / ghostscript4js

Ghostscript4JS binds the Ghostscript C API to the Node.JS world.
http://www.nacios.it
Apache License 2.0
66 stars 19 forks source link

undefinedfilename error on Windows if file path contains spaces #33

Closed letalumil closed 6 years ago

letalumil commented 6 years ago

Let's assume we have these input and output file paths:

const output = "C:\\Users\\letalumil\\Test folder\\node-love-ghostscript.png";
const input = "C:\\Users\\letalumil\\Test folder\\node-love-ghostscript.pdf";

And we try to convert the pdf this way:

const cmd = `-sDEVICE=pngalpha -o ${output} -sDEVICE=pngalpha -r144 ${input}`;

Or that way (quotes around the paths):

const cmd = `-sDEVICE=pngalpha -o "${output}" -sDEVICE=pngalpha -r144 "${input}"`;

In boths cases the operation fails with corresponding errors:

GPL Ghostscript 9.21 (2017-03-16)
Copyright (C) 2017 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefinedfilename in (folder\\node-love-ghostscript.png)
Operand stack:
Execution stack:
%interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
 Dictionary stack:
--dict:1201/1684(
GPL Ghostscript 9.21: Unrecoverable error, exit code 1
ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--
Current allocation mode is local
Last OS error: No such file or directory

And

GPL Ghostscript 9.21 (2017-03-16)
Copyright (C) 2017 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefinedfilename in (folder\\node-love-ghostscript.png")
Operand stack:
Execution stack:
%interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --n
ostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
Dictionary stack:
--dict:1201/1684(
GPL Ghostscript 9.21: Unrecoverable error, exit code 1
ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--
Current allocation mode is local
Last OS error: No such file or directory

If I remove the space in the Test folder name and run the command without quotes around paths, then everything works. If The folder is renamed, but the quotes are still there, then I get a similar error:

GPL Ghostscript 9.21 (2017-03-16)
Copyright (C) 2017 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefinedfilename
GPL Ghostscript 9.21: Unrecoverable error, exit code 1
in ("C:\\Users\\letalumil\\Test_folder\\node-love-ghostscript.pdf")
Operand stack:
Execution stack:
%interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --n
ostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
Dictionary stack:
 --dict:1204/1684(ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--
Current allocation mode is local
Last OS error: Invalid argument
letalumil commented 6 years ago

Just to add some more info regarding the problem. I tried to execute the commands above via gswin64.exe in the command line.

This one fails:

C:\gs\bin>gswin64.exe -sDEVICE=pngalpha -o C:\Users\letalumil\Test folder\node-love-ghostscript.png -sDEVICE=pngalpha -r144 C:\Users\letalumil\Test folder\node-love-ghostscript.pdf

This one works well:

C:\gs\bin>gswin64.exe -sDEVICE=pngalpha -o "C:\Users\letalumil\Test folder\node-love-ghostscript.png" -sDEVICE=pngalpha -r144 "C:\Users\letalumil\Test folder\node-love-ghostscript.pdf"
NickNaso commented 6 years ago

Hi @letalumil , thanks for your feedback for sure I will try to fix it in next days. I think that the problem is in the parsing of the Ghostscript comand on the C/C++ side.

george-g commented 6 years ago

Hello! I made pull request #35 for this bug.

NickNaso commented 6 years ago

Hi @george-g I saw and thank you for your work. Now I'm out, but your code seems good. Tomorrow I came back and I will test all and I will provide to create a new version of the add-on. On the pull request feel free to insert you on the contributors in package.json file. Did you test on Windows machine?

george-g commented 6 years ago

Did you test on Windows machine?

Yes, I did. I made this fix on Windows )

NickNaso commented 6 years ago

Thank you @george-g to solve this issue. Great work. Thank you @letalumil to report the problem. I just created and published a new version of the module.