使用GhostScript的服务器上的PDF转换为图像的集合

问题描述:

这是我想要实现的步骤:

These are the steps I am trying to achieve:


  1. 上传PDF文档的服务器上。

  2. 转换PDF文档到一组使用GhostScript的(每个页面转换为图像)的图像

  3. 发送图像集合返回给客户端。

到目前为止,我感兴趣的 2

So far, I am interested in #2.

首先,我下载了这两个 gswin32c.exe gsdll32.dll 并设法为PDF手动转换为图像的集合(我开的 CMD ,并运行命令波纹管):

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

后来我想,我就把 gswin32c.exe gsdll32.dll 到我的web项目中的ClientBin,并运行通过过程中的.exe。

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(); 

不幸的是,没有什么是的ClientBin输出。任何人有一个想法,为什么?任何建议将高​​度AP preciated。

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

我试过你的code,它似乎是工作的罚款。我会建议您检查以下几点:

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


  1. 验证您的somepdf.pdf是在GS进程的工作文件夹或指定命令行文件的完整路径。这也将是有益的做这样的事情,看看ghostscript的输出:

  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 =真;
process1.StartInfo.UseShellExecute = FALSE;
process1.Start();
//读取输出
字符串输出= process1.StandardOutput.ReadToEnd();
...
process1.WaitForExit();
...

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

如果GS无法找到你的文件,你会得到一个错误:/ undefinedfilename在(somepdf.pdf)。在输出流中

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

另一个建议是,你与你的剧本进行,不会等待GS进程完成,并产生导致image_N.jpg文件。我猜增加process1.WaitForExit应该解决的问题。

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.