views:

205

answers:

3

Hello everybody,

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.

Best Regards,

K

+1  A: 

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 smth 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.

Hope this helps, regards

serge_gubenko
A: 

Hi Serge,

Thanks for the response. Unfortunately I still can't manage to do it.

I have an asp server button:

<asp:Button Text="Click Me" runat="server" onclick="Button1_Click"/>

The Button1_Click event handler looks like this:

protected void Button1_Click(object sender, EventArgs e)
{
    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 somedocument.pdf";
    process1.StartInfo.RedirectStandardOutput = true;
    process1.StartInfo.UseShellExecute = false;
    process1.Start();
    string output = process1.StandardOutput.ReadToEnd();
    process1.WaitForExit();
    Response.Write(output);
}

On the image bellow, you can see that all the necessary files are in place.

alt text

Unfortunately, once I click the button, I get the following result.

alt text

Am I doing anything wrong, or maybe the whole approach is wrong.

Thanks a lot for the help.

Best Regards,

K

Kiril
I see there is "Error: /undefinedfilename.." in the logs; you usually get it when gs can't find the file. Are you sure "somedocument.pdf" file is in the working drectory of the process? Coz it looks like it's not.
serge_gubenko
just noticed you have "somedocument.pdf" in the ClientBin; it should be in the working direcorty of gs process; which is what ever "Request.MapPath("~/")" returns to you; not the ClientBin. You might want to set it to the ClientBin; in this case it should work. Smth like this:process1.StartInfo.WorkingDirectory = Path.Combine(Request.MapPath("~/"), "ClientBin");
serge_gubenko
process1.StartInfo.WorkingDirectory = Path.Combine(Request.MapPath("~/"), "ClientBin"); saved my life. Thanks a lot :)
Kiril
A: 

oh, thanks god, thank you both :) you both saved my eyes and my life. I was googling from 2:00 PM since now (6:00 AM). I think I've read all the related pages, posts, forums, ... but your sample code was the only code that worked without ANY problem. Thank you again :) now I can go to sleep :D