2010-03-24 70 views
2

我爲c#和ghostscript使用ghostscriptsharp包裝。我想從pdf文件中生成縮略圖。有關示例代碼的更多信息請參見here在Web應用程序中使用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在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服務器的新實例生成)。當我創建WindowsForm應用程序時,它也可以工作。生成縮略圖。

當我直接使用web瀏覽器訪問應用程序(http://localhoast/mywebappliation/.)時,它不起作用。沒有生成縮略圖。但也沒有拋出異常。

我將gsdll32.dll放在windows xp的system32文件夾中。 Ghostscript Runtime也被安裝。我在IIS-Webproject(.Net 2.0)中提供了完全訪問權限。

有誰知道我爲什麼不能從我的web應用程序訪問Ghostscript?是否有訪問IIS服務器上的DLL文件的安全問題?

問候 克勞斯

回答

1

我現在有一個解決方法。我沒有使用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>")); 
    } 
0

它可能是你正在運行的網站下沒有寫權限c中的身份:\

+0

我已經給所有可能的用戶(iis用戶,aspnetuser等)寫入權限爲c:\,gsdll32.dll,gs.exe,但它沒有工作。 – 2010-03-31 18:00:17

+0

您需要將gsdll32.dll放入webs bin文件夾。 – Adrian 2010-04-01 09:41:05

2

嘗試改變當前目錄

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