2011-12-14 136 views
3

目前我需要转换的pdf.The第一个.aspx页面中包含images.I've用下面的代码,但它给error.I've在我的项目加入iTextsharp.dll ..如何使用C#将.aspx转换为pdf?

protected void btnConvertToPDF_Click(object sender, EventArgs e) 
{ 
    Uri strurl = Request.Url;    
    string url = strurl.ToString();    
    string text = GetPageText(url);    
    string filepath = Server.MapPath("test.htm"); 
    StreamWriter writer = new StreamWriter(filepath);    
    writer.Write(text);    
    writer.Close();    
    htmltopdf(text); 
} 

public string GetPageText(string url)    
{ 
    string htmlText = string.Empty;    
    string FILE_NAME = Server.MapPath("test.xml"); //"c:\\test.xml";    
    try 
    { 
     HttpWebRequest requestIP = (HttpWebRequest)WebRequest.Create(url);    
     requestIP.Timeout = 10000; 
     using (HttpWebResponse responseIP = (HttpWebResponse)requestIP.GetResponse())    
     { 
      using (Stream streamIP = responseIP.GetResponseStream())    
      { 
       using (StreamReader readerText = new StreamReader(streamIP))    
       { 
        htmlText = readerText.ReadToEnd();    
        string text = htmlText;    
        StreamWriter writer = new StreamWriter(FILE_NAME);    
        writer.Write(text);    
        writer.Close();    
       } 
      } 
     } 
    } 
    finally 
    { 
    } 
    return htmlText;    
} 

public void htmltopdf(string strHtml) 
{ 
    Document doc = new Document(); 
    PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("test.pdf"), System.IO.FileMode.Create)); 
    HTMLParser.Parse(doc, Server.MapPath("test.htm")); 
    if (File.Exists(Server.MapPath("test.htm")))   
     File.Delete(Server.MapPath("test.htm"));    
    if (File.Exists(Server.MapPath("test.xml")))    
     File.Delete(Server.MapPath("test.xml")); 
} 

即使在运行代码之前,HTMLparser.Parse在HTMLPPARSER在当前上下文中不存在的错误显示在HTMLparser.Parse行上。如果我注释该行并运行代码,它将创建一个错误为“它不能是开放的,它已经被破坏,不是正确的版本等等。“请问任何人都可以告诉我什么是错误的吗?有没有开源的工具可以用来完成这个任务?我必须通过编写代码而不是购买任何组件..

回答

5

您可以使用WkHtmltoPdf将页面转换为pdf。见this post更多细节

编辑:

使用该代码的URL直接转换为PDF。你需要把wkhtmltopdf.exe在您的项目

string url= @"http://www.google.com"; 

try 
{ 
System.Diagnostics.Process process = new System.Diagnostics.Process(); 
process.StartInfo.UseShellExecute = false; 
process.StartInfo.CreateNoWindow = true; 
process.StartInfo.FileName = Server.MapPath("~/bin/") + "wkhtmltopdf.exe"; 
process.StartInfo.Arguments = "\""+ url+ " " + Server.MapPath("~/PDFFiles/") + "test.pdf\""; 

process.StartInfo.RedirectStandardOutput = true; 
process.StartInfo.RedirectStandardError = true; 
process.Start(); 
process.WaitForExit(); 

} 
catch (Exception ee) 
      { 
     //logging 
      } 
+0

@萨卡尼亚我会建议@prasanths解决方案,你可以只是使服务器上的服务,然后调用它的methodes – Devjosh 2011-12-14 12:41:41

+0

@Sukanya具有u试图在链接我的代码给定?请参阅http://stackoverflow.com/a/2834549/915125 – Prasanth 2011-12-15 07:20:36

+0

您可以使用dll文件,也可以使用可用于windows的wkhtmltopdf.exe。该exe文件很容易使用。你可以在你的项目的文件夹中放置exe文件,并在给定的文章中使用代码。 – Prasanth 2011-12-15 07:36:02

3

如果你不mi nd支付外部图书馆,您可以通过ABCPDF轻松完成此操作。这可能会让你头痛。

Doc theDoc = new Doc(); 
theDoc.AddImageUrl("http://www.google.com/"); 
theDoc.Save(Server.MapPath("htmlimport.pdf")); 
theDoc.Clear(); 
0

的bin文件夹中任何网页转换成您可以使用免费的shell实用程序wkhtmltopdf它采用了WebKit渲染引擎(也使用.pdf文件对于谷歌浏览器或Safari浏览器)。

我已经为post中的可能实现提供了说明和代码。

如果您有任何问题,请随时询问。

0

首先从链接中下载wkhtmltox-0.11.0_rc1-installer http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltox-0.11.0_rc1-installer.exe&can=1&q.Then运行安装程序,然后将wkhtmltopdf文件复制到项目的bin文件夹中。 我的目标是使用System.Diagnostics将整个.aspx页面转换为pdf.Import; using System.Configuration;在该按钮的你form.On Click事件添加一个按钮写下面的代码:

protected void btnConvertToPDF_Click(object sender, EventArgs e) 
{ 
    Uri strurl = Request.Url; 
    string url = strurl.ToString(); 

    string filename = "Test"; 

    HtmlToPdf(url, filename); 

} 
public static bool HtmlToPdf(string Url, string outputFilename) 
{ 
    string filename = ConfigurationManager.AppSettings["ExportFilePath"] + "\\" + outputFilename + ".pdf"; 

    Process p = new System.Diagnostics.Process(); 
    p.StartInfo.Arguments = Url + " " + filename; 
    p.StartInfo.UseShellExecute = false; 
    p.StartInfo.CreateNoWindow = true; 

    p.StartInfo.FileName = HttpContext.Current.Server.MapPath("~/bin/") + "wkhtmltopdf.exe"; 

    p.StartInfo.RedirectStandardOutput = true; 
    p.StartInfo.RedirectStandardError = true; 
    p.StartInfo.RedirectStandardInput = true; 
    p.Start(); 
    string output = p.StandardOutput.ReadToEnd(); 

    p.WaitForExit(60000); 

    // read the exit code, close process 
    int returnCode = p.ExitCode; 
    p.Close(); 

    // if 0 or 2, it works 
    return (returnCode == 0 || returnCode == 2); 
} 

此代码将创建一个名为Test的pdf文件,它会保存在C盘。 朋友们,这就是全部。我非常感谢所有来自你们的支持。