2012-11-15 58 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默认网页浏览器)做渲染和印刷本身。这种技术在服务器端环境中不能可靠地工作。