What is causing Ghostscript to return an error of -100?
Asked Answered
R

3

18

So, I am using Matthew Ephraim's GhostscriptSharp, which is a simple C# wrapper for the unmanaged Win32 Ghostscript DLL in my ASP.Net MVC project. Some background:

What I am attempting to do is have a user upload a PDF, and then convert that document into an image that I can then save off into whatever directory I choose (as well as do some other OOP to tie that new image to my site).

I decided to use Mr. Ephraim's wrapper class (GhostscriptSharp) because it was simple enough to use, and it gives me relatively clean access to the DLL's API.

To test it, I created a dummy C# console application to make sure I could load the DLL, access it, hand it a PDF file on the local disk and then have it write a JPG to the same local disk. After a few learning experiences, I had success. I would hand it C:\INPUT.pdf, it would hand me C:\OUTPUT.jpg.

However, after integrating the GhostScriptSharp code that I had in the console application into my ASP.NET MVC project to the point of where I was calling the DLL with P/invoke, Ghostscript is returning with the int/error code -100, which is a fatal error (is called E_Fatal in the GhostScript source code). I get the same result with both the file that is uploaded through the HTML form, and if I hand it the exact same hard-coded paths that I used in my working console application.

For reference, the lines which the exception is thrown are 93-97 in GhostScriptSharp.cs (which is in the CallApi function):

int result = InitAPI(gsInstancePtr, args.Length, args);

if (result < 0) {
  throw new ExternalException("Ghostscript conversion error", result);
}

Obviously the exception is thrown because result is -100.

When InitAPI is called, the instance ptr is a valid int (though I don't know if the instance of GS is correct or not), args has a length of 20 (is a string[]) of valid GhostScript options (including the correctly-escaped paths to my input & output files).

Long story short, what am I doing wrong? The error code -100 seems like a catch-all because there is no documentation that states what could possibly be going wrong here.

Any help is much appreciated, thank you in advance.

Rica answered 2/12, 2010 at 21:58 Comment(0)
R
4

So, ended up being an ID10T error that was derailing me here in this specific instance.

In Matthew Ephraim's GhostscriptSharp code, he uses a couple of enums to define the options set forth for Ghostscript, and two in particular were the GhostscriptDevices and GhostscriptPageSizes enums. Problem is, the way they're written Resharper (Jetbrains Visual Studio plugin) has default rules for naming Enum members. Not thinking, I fixed all of these definitions to please Resharper not realizing that these are passed directly to Ghostscript, so instead of getting a7 for -sPAPERSIZE GS was getting A7, and for -sDEVICE it was getting Jpeg instead of jpeg.

For the time being permissions weren't a problem on my end, but only because I run the Cassini web dev test server in Visual Studio.

Thanks to @MarkRedman and @tvanfosson for their helpful suggestions!

Rica answered 6/12, 2010 at 17:8 Comment(0)
V
13

The -100 error is a generic "fatal error" in GhostScript.

A few things to check:

1) Permissions (al operations require file access)

2) Scope, you want to add the GS bin folder to the PATH variables

3) Consider not calling GhostScript directly from asp.net, GS can be very CPU intensive, rather process files in a separate service

I have also created a wrapper, send me an email (address on profile) and I will send it you. It allows one to pass in the GS bin folder which helps.

Vagrant answered 2/12, 2010 at 22:14 Comment(5)
Which user accounts would I need to check permissions for? Something like those in the IIS_IUSRS user group?Rica
Yes the user account used to run the application, the same one in the Application Pool (we use a named user added to the IIS Users/Workgroup. This user should have the correct permissions on the folders where file are generated etc (try all/full permissions to test) HTHVagrant
Also, when I install GS, I tick the " for all Users" in the setup and also add the GS bin folder to the %PATH% environment variables on the machine...Vagrant
What if I did not formally install Ghostscript as a standalone executable, and instead only downloaded the Win32 DLL to add to my project? It worked in my Win32 Console app, I'm a bit frustrated that it's not working in my web app with just about everything else the same. Thanks again for your help.Rica
The installer for the most part just copies the files into the folder GS folder (i think) and I assume you mean the whole GS folder structure not just one of the dlls? When you run the Win32 Console app, it runs under the context of the logged in user, this is probabably the Administrator or a user that has permissions to all the folders, running under the context of the website may not have all these permissions (especially if you running the Default Application Pool using LocalSystem or similar) I would also check any temp folder you may use.Vagrant
R
4

So, ended up being an ID10T error that was derailing me here in this specific instance.

In Matthew Ephraim's GhostscriptSharp code, he uses a couple of enums to define the options set forth for Ghostscript, and two in particular were the GhostscriptDevices and GhostscriptPageSizes enums. Problem is, the way they're written Resharper (Jetbrains Visual Studio plugin) has default rules for naming Enum members. Not thinking, I fixed all of these definitions to please Resharper not realizing that these are passed directly to Ghostscript, so instead of getting a7 for -sPAPERSIZE GS was getting A7, and for -sDEVICE it was getting Jpeg instead of jpeg.

For the time being permissions weren't a problem on my end, but only because I run the Cassini web dev test server in Visual Studio.

Thanks to @MarkRedman and @tvanfosson for their helpful suggestions!

Rica answered 6/12, 2010 at 17:8 Comment(0)
D
3

Most likely the process running the web application does not have permission to write to the directories that you are using. I'd suggest creating some specific directory for the app to use and a local id to use to run the app pool, then give that id enough privileges to read/write the directory you've created.

Deconsecrate answered 2/12, 2010 at 22:2 Comment(2)
One, I've heard that if I am running the web application with the ASP.Net Development server that it operates with my local user's permissions, which I am Administrator on my local machine and that's where the files are... Two, earlier in the application before I even use GhostscriptSharp my application creates the specific directories the PDF and JPGs go into, so that would also indicate it has proper Write permissions, correct?Rica
@Rica - that's true as long as you are running in Cassini. Not sure what could be the problem.Deconsecrate

© 2022 - 2024 — McMap. All rights reserved.