【问题标题】:Using Ghostscript in a Webapplication (PDF Thumbnails)在 Web 应用程序中使用 Ghostscript(PDF 缩略图)
【发布时间】:2011-01-31 04:29:45
【问题描述】:

我正在为 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);
 }

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

当我使用网络浏览器直接访问应用程序 (http://localhoast/mywebappliation/..) 时,它不起作用。不生成缩略图。但是也没有抛出异常。

我将 gsdll32.dll 放在 windows xp 的 system32 文件夹中。 Ghostscript 运行时也已安装。我已授予 IIS-Webproject (.Net 2.0) 的完全访问权限。

有人知道为什么我不能从我的网络应用程序访问 Ghostscript 吗?访问 IIS 服务器上的 dll 文件是否存在任何安全问题?

问候 克劳斯

【问题讨论】:

    标签: c# asp.net pdf ghostscript


    【解决方案1】:

    尝试改变当前目录

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        您运行网站的身份可能没有 c:\

        的写入权限

        【讨论】:

        • 我已授予所有可能的用户(iis 用户、aspnetuser 等)对 c:\、gsdll32.dll、gs.exe 的写入权限...但它不起作用。
        • 您需要将 gsdll32.dll 放入 webs bin 文件夹中。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-10
        • 1970-01-01
        • 1970-01-01
        • 2011-01-23
        相关资源
        最近更新 更多