Question

These are the steps I am trying to achieve:

  1. Upload a PDF document on the server.
  2. Convert the PDF document to a set of images using GhostScript (every page is converted to an image).
  3. Send the collection of images back to the client.

So far, I am interested in #2.

First, I downloaded both gswin32c.exe and gsdll32.dll and managed to manually convert a PDF to a collection of images(I opened cmd and run the command bellow):

gswin32c.exe -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -r150 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dMaxStripSize=8192 -sOutputFile=image_%d.jpg somepdf.pdf

Then I thought, I'll put gswin32c.exe and gsdll32.dll into ClientBin of my web project, and run the .exe via a Process.

System.Diagnostics.Process process1 = new System.Diagnostics.Process();
process1.StartInfo.WorkingDirectory = Request.MapPath("~/");
process1.StartInfo.FileName = Request.MapPath("ClientBin/gswin32c.exe"); 
process1.StartInfo.Arguments = "-dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -r150 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dMaxStripSize=8192 -sOutputFile=image_%d.jpg somepdf.pdf"
process1.Start(); 

Unfortunately, nothing was output in ClientBin. Anyone got an idea why? Any recommendation will be highly appreciated.

Was it helpful?

Solution

I've tried your code and it seem to be working fine. I would recommend checking following things:

  1. verify if your somepdf.pdf is in the working folder of the gs process or specify the full path to the file in the command line. It would also be useful to see ghostscript's output by doing something like this:

    .... process1.StartInfo.RedirectStandardOutput = true; process1.StartInfo.UseShellExecute = false; process1.Start(); // read output string output = process1.StandardOutput.ReadToEnd(); ... process1.WaitForExit(); ...

    if gs can't find your file you would get an "Error: /undefinedfilename in (somepdf.pdf)" in the output stream.

  2. another suggestion is that you proceed with your script without waiting for the gs process to finish and generate resulting image_N.jpg files. I guess adding process1.WaitForExit should solve the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top