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

4
我正在使用C#和Ghostscript的GhostscriptSharp包装器。我想从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在一个Web应用程序(.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应用程序时,它也可以正常工作。缩略图被生成。
但是,当我直接使用Web浏览器访问应用程序时(http://localhoast/mywebappliation/..),它就无法工作。没有生成任何缩略图,但也没有抛出任何异常。
我已将gsdll32.dll放置在Windows XP的system32文件夹中,并安装了Ghostscript Runtime。我已在IIS-Web项目(.Net 2.0)中授予完全访问权限。
有人知道为什么我无法从我的Web应用程序访问Ghostscript吗?在访问IIS服务器上的dll文件时是否存在任何安全问题?
问候 Klaus
3个回答

2
尝试更改当前目录。
string workingDirectory = @"C:\tmp";
Directory.SetCurrentDirectory(workingDirectory);
GhostscriptWrapper.GeneratePageThumb("c:\\sample.pdf", "c:\\sample.jpg", 1, 100, 100);

1
我现在有一种解决方法。我不使用GhostscriptWrapper类访问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 = "<font face=courier size=0>{0}</font>";
        return String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "<br>"));
    }

0

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


我已经为所有可能的用户(如iis用户、aspnetuser等)授予了c:\、gsdll32.dll、gs.exe等文件的写入权限,但仍然无法正常工作。 - cpt.oneeye
你需要将 gsdll32.dll 放入网站的 bin 文件夹中。 - Mesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接