Syntax Highlighting

Friday 18 March 2011

Using GhostScriptSharp to create pdf thumbnails

It sounds like a simple task. Creating thumbnails of uploaded files for display in a repeating list. The project I am working on uses Kentico CMS, which provides the majority of this functionality out of the box. However, it did not create thumbnails for pdf files. The interim solution was to display the default pdf file icon. This worked for the first few weeks after which it became clear that the majority of the uploaded documents were pdf's. This made for a very uniteresting screen, so the decision was made to find a component or service to create pdf thumbnails.

After a little research I decided on GhostscriptSharp, a C# wrapper for the GhostScript library. Installed, up and running, and simple solution implemented beautifully on localhost within a couple of hours. And so to the Staging staging server.... a very different server. This did not take hours or sadly days, but a couple of weeks (albeit of on and off development) to get working.
I am now going to outline the major time-sinks I encountered when getting GhostscriptSharp working on a remote staging server;

Firstly, you do not need to install GhostScript on the server which your code is running. This is something that was not 100% clear from the documentation. I was also hampered by the suggestion from several other blog posts that this was indeed a prerequisite for getting GhostScriptSharp up and running. For me, this was simply not the case. Your application will only require the correct version (I'll come to this later!) of the GhostScript dll in it's bin folder.

The next problem I encountered was the following error message;
Message: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) Stack Trace: at GhostscriptSharp.GhostscriptWrapper.CreateAPIInstance(IntPtr& pinstance, IntPtr caller_handle)
My working solution was developed on a 64-bit machine. I was deploying to a 32bit machine. There are two versions of GhostScript a 64 bit and a 32 bit version (can you see where I'm going here?).
So, if you see this error message with your GhostScript application, check you are using the correct version of the dll.

Correct dll in the bin folder. Show me the pdf thumbnails.

No?

Another error message was now being displayed by my application. This one said;

Unable to load DLL 'gsdll32.dll': The specified module could not be found.

This error message was caused by insufficient permissions on the bin folder. To overcome this I had to grant read & write permissions for the application pool identity on the /bin folder to enable it to access the dll.


Another error message bites the dust, surely now.... thumbnails? No. Though now my application was up and running, so no more asp.net error messages, just application error messages. This one said;

Ghostscript 100 error

As it so often does, Stackoverflow came to the rescue. As well as granting read and write permissions on the bin folder, I also had to grant read and write permissions on the /files folder (the folder my thumbnails were being written to). This allowed the GhostScript dll to write thumbnails to the specified location.

And with that final change, the application sprang in to life and several hundred thumbnails were written to disk. Ah, the taste of victory.


In summary, writing the GhostScriptSharp code was a really simple process. Getting it to run on a production server was a slightly more involved process, but I would definitely use it again on other projects with the configuration knowledge I now have in the bank.

3 comments:

Saurabh Tyagi said...

Thank You very much, I was searching for this problem for last 5 days but not find a correct solution but after reading your very clear description i did it. Thank You so much.

Kiran malgi said...

hi.. i have used gsdll32 dll..but after hosting in windows server 2008(iis 7)..its is giving error Object reference not set to an instance of an object

Unknown said...

Thanks a lot for sharing the info....

Kentico Development