2011-12-14 13 views
3

は現在、私はpdf.Theページ内の1つの.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行に、「HTMLPARSERは現在のコンテキストに存在しません」というエラーがコードを実行する前でも表示されます。その行にコメントしてコードを実行すると、エラーのある1つのpdfファイルが作成されます。オープンされています。破損しています。バージョンなどが正しくありません。 "誰かが何が間違っているか教えてください。タスクを実行するためのオープンソースはありますか? ..

答えて

5

WkHtmltoPdfをpdfのページに使用できます。 PDFファイルへのURLを直接変換するために、このコードを使用し

this post編集

詳細を参照してください。あなたはまた、使用されているWebKitのレンダリングエンジンを使用します(無料のシェル・ユーティリティーwkhtmltopdfを使用することができます.pdfファイルに任意のWebページを変換するには、プロジェクト

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

は、私はあなただけのサーバー上のサービスを作成し、それがmethodes – Devjosh

+0

@Sukanyaだ呼び出すことができます@prasanthsソリューションをお勧めしますスカニア@ uが私が持っているリンク内のコードを試してみました与えられた?これを参照してくださいhttp://stackoverflow.com/a/2834549/915125 – Prasanth

+0

dllファイルを使用するか、Windows用に使用できるwkhtmltopdf.exeを使用できます。 exeファイルは使いやすいです。あなたはプロジェクトのフォルダにexeファイルをplacし、指定された投稿にコードを使用することができます。 – Prasanth

3

もしあなたがmi外部の図書館に支払う場合は、ABCPDFでこれを簡単に行うことができます。それはあなたに頭痛を救うかもしれません。

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

のbinフォルダにwkhtmltopdf.exeを配置する必要がありますGoogle ChromeやSafariブラウザの場合)。

このpostには、実装可能な命令とコードがあります。

ご不明な点がございましたら、お気軽にお問い合わせください。

+0

@Sukanyaあなたの問題を解決する答えが見つかった場合は、答えの左側にある目盛りを使って答えとしてマークすることができます。 – Prasanth

+0

@Sukanya WkHtmToPdfを使ってページをPDFに変換しようとしたコードを教えてもらえますか?私の知識では、それはpdfに直接ウェブページをカバーするための良い解決策です。 – Prasanth

+0

@Sukanya:代わりにソースコードをダウンロードして、ここからインストールをダウンロードすると思います。[wkhtmltopdf windows installer](http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltox-0.11 .0_rc1-installer.exe&can = 1&q =)。その後、インストールディレクトリC:\ Program Files(x86)\ wkhtmltopdfをビジュアルスタジオソリューションにコピーします。 –

0

まず、http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltox-0.11.0_rc1-installer.exe&can=1&q.Thenのリンクからwkhtmltox-0.11.0_rc1-installerをダウンロードしてから、プロジェクトのbinフォルダにwkhtmltopdfファイルをコピーしてください。 私の目的は、System.Diagnosticsを使用してpdf.Importに全体の.aspxページを変換することです。 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ドライブに保存されます。 友だち、それだけです。皆さんからいただいた全てのご支援に本当に感謝しております。

+0

wkhtmltopdfのすべてのファイルをbinフォルダに 'uninstall'を除いてインストールします。 – Sukanya

+0

柊、なぜあなたは私の答えをマークしていませんか?私はあなたにすべてのヒントを提供しました。何が間違っていたのですか?なぜなら、私はポイントを失ってしまったからです。 – Prasanth

+0

私はurの答えを解除しませんでした。完了したら、私は再びそれをマークしています。 – Sukanya

関連する問題