在Web应用程序中使用Ghostscript(PDF缩略图)

我使用ghostscriptsharp包装为c#和ghostscript。 我想用pdf文件生成缩略图。 有关示例代码的更多信息,请参见此处 。

从ghostscript-c-dll“gsdll32.dll”导入了不同的方法。

[DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")] private static extern int CreateAPIInstance(out IntPtr pinstance, IntPtr caller_handle); [DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")] private static extern int InitAPI(IntPtr instance, int argc, IntPtr argv); //...and so on 

我正在使用GhostscriptWrapper在webapplication(.net 2.0)中生成缩略图。 该类使用上面导入的方法。

  protected void Page_Load(object sender, EventArgs e){ GhostscriptWrapper.GeneratePageThumb("c:\\sample.pdf", "c:\\sample.jpg", 1, 100, 100); } 

当我通过按“F5”键调试Visual Studio 2008中的Web应用程序时,它工作正常(生成一个新的Web服务器实例)。 当我创建一个WindowsForm应用程序时,它也可以工作。 生成缩略图。

当我直接使用webbrowser访问应用程序时( http:// localhoast / mywebappliation / 。),它不起作用。 没有生成缩略图。 但也没有例外。

我将gsdll32.dll放在windows xp的system32文件夹中。 Ghostscript Runtime也已安装。 我已经在IIS-Webproject(.Net 2.0)中提供了完全访问权限。

有谁知道为什么我无法从我的webapplication访问Ghostscript? 在IIS服务器上访问dll文件是否存在任何安全问题?

问候克劳斯

尝试更改当前目录

 string workingDirectory = @"C:\tmp"; Directory.SetCurrentDirectory(workingDirectory); GhostscriptWrapper.GeneratePageThumb("c:\\sample.pdf", "c:\\sample.jpg", 1, 100, 100); 

我现在有一个解决方法。 我没有使用GhostscriptWrapper-Class访问Ghostscript。 相反,我直接访问服务器上的cmd.exe。 以下方法接受命令(ghostscript语法)并在cmd.exe中运行它。 我使用以下方法执行此操作:

 public static string runCommand(string workingDirectory, string command) { // Create the ProcessInfo object System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe"); psi.UseShellExecute = false; psi.RedirectStandardOutput = true; psi.RedirectStandardInput = true; psi.RedirectStandardError = true; psi.WorkingDirectory = workingDirectory; // Start the process System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi); // Attach the output for reading System.IO.StreamReader sOut = proc.StandardOutput; // Attach the in for writing System.IO.StreamWriter sIn = proc.StandardInput; sIn.WriteLine(command); // strm.Close(); // Exit CMD.EXE string stEchoFmt = "# {0} run successfully. Exiting"; // sIn.WriteLine(String.Format(stEchoFmt, targetBat)); sIn.WriteLine("EXIT"); // Close the process proc.Close(); // Read the sOut to a string. string results = sOut.ReadToEnd().Trim(); // Close the io Streams; sIn.Close(); sOut.Close(); // Write out the results. string fmtStdOut = "{0}"; return String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "
")); }

您运行该网站的身份可能没有c:\的写权限