2012-11-15 60 views
0

我有問題,當我打印的html文件,我已經試過DOC,XLS和TXT文件,它們完美地工作,但是當我給HTML文件它爲我打印對話框和我有選擇ghostscript打印機才能工作。將HTML轉換成PS使用Ghostscript C#

我的代碼是:

[DllImport("Winspool.drv")] 
    private static extern bool SetDefaultPrinter(string printerName); 

    [ValidateInput(false)] 
    public ActionResult CreatePdf(string file , string html) 
    { 
     SetDefaultPrinter("Ghostscript"); 
     Process process1 = new Process(); 
     if (html != null && html != "") 
     { process1.StartInfo.FileName = "example.html"; } 
     else 
     { process1.StartInfo.FileName = file; } 
     process1.EnableRaisingEvents = true; 
     process1.StartInfo.Verb = "print"; 
     process1.StartInfo.Arguments = "\"Ghostscript PDF\""; 
     process1.StartInfo.WorkingDirectory = Server.MapPath("~" + "/Export"); 
     process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     process1.StartInfo.CreateNoWindow = true; 
     process1.Start(); 
     try 
     { 
      process1.WaitForExit(); 
     } 
     catch (InvalidOperationException) { } 
     process1.Dispose(); 
    } 

這應該改變我的output.ps文件,然後我用它來製作PDF文件,完美的作品,我只是需要做這項工作的html文件。

我跟着這兩個例子:

Example 1 Example 2

編輯: 我需要爲了得到從HTML PDF文件這個converstion,發現wkhtmltopdf最適合我的。

回答

0

Ghostscript不會將HTML文檔轉換(佈局和渲染)爲PDF或PostScript,它只是一個用於處理PostScript和PDF文件的庫,例如從頭創建它們並將PostScript文件轉換爲柵格格式。

如果你想將HTML轉換爲PDF,最好的辦法是使用商業圖書館,如PrinceXML或主機WebKit。

當你的代碼的工作,它的工作原理是讓的Internet Explorer(或任何你的shell默認網頁瀏覽器)做渲染和印刷本身。這種技術在服務器端環境中不能可靠地工作。