使用 GhostScript 将 PDF 转换为服务器上的图像集合
这些是我试图实现的步骤:
- 在服务器上上传 PDF 文档。
- 使用 GhostScript 将 PDF 文档转换为一组图像(每个页面都转换为图像)。
- 将图像集合发送回客户端。
到目前为止,我对#2感兴趣。
首先,我下载了 gswin32c.exe 和 gsdll32.dll 并设法手动将 PDF 转换为图像集合(我打开 cmd 并运行下面的命令):
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 中,并通过 Process 运行 .exe 。
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 中没有输出任何内容。有人知道为什么吗?任何建议都将受到高度赞赏。
These are the steps I am trying to achieve:
- Upload a PDF document on the server.
- Convert the PDF document to a set of images using GhostScript (every page is converted to an image).
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经尝试过你的代码,它似乎工作正常。我建议检查以下内容:
验证您的 somepdf.pdf 是否位于 gs 进程的工作文件夹中,或在命令行中指定文件的完整路径。通过执行以下操作来查看 Ghostscript 的输出也很有用:
....
process1.StartInfo.RedirectStandardOutput = true;
process1.StartInfo.UseShellExecute = false;
进程1.Start();
// 读取输出
字符串输出 = process1.StandardOutput.ReadToEnd();
...
process1.WaitForExit();
...
如果 gs 找不到您的文件,您将在输出流中收到“错误:/undefinedfilename in (somepdf.pdf)”。
另一个建议是您继续执行脚本,而无需等待 gs 进程完成并生成结果 image_N.jpg 文件。我想添加 process1.WaitForExit 应该可以解决问题。
I've tried your code and it seem to be working fine. I would recommend checking following things:
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.
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.