標籤:c#產生pdf html產生pdf
使用iTextSharp 控制項
using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using iTextSharp.text;using iTextSharp.text.pdf;using System.IO;using iTextSharp.tool.xml;using System.Text;using System.Net;namespace PDFTools{ public partial class CreatePdf : System.Web.UI.Page { string INPATH = System.Web.HttpContext.Current.Server.MapPath("~/bin/temp.html"); protected void Page_Load(object sender, EventArgs e) { string path = System.Web.HttpContext.Current.Server.MapPath("~/UploadPdf"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); WebClient wc = new WebClient(); //從網址下載Html字串 wc.Encoding = System.Text.Encoding.UTF8; string htmlText = getWebContent(); byte[] pdfFile = this.ConvertHtmlTextToPDF(htmlText); string fileId = "/file_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; System.IO.File.WriteAllBytes(path + fileId, pdfFile); } /// <summary> /// 判斷是否有亂碼 /// </summary> /// <param name="txt"></param> /// <returns></returns> public bool isMessyCode(string txt) { var bytes = Encoding.UTF8.GetBytes(txt); //239 191 189 for (var i = 0; i < bytes.Length; i++) { if (i < bytes.Length - 3) if (bytes[i] == 239 && bytes[i + 1] == 191 && bytes[i + 2] == 189) { return true; } } return false; } /// <summary> /// 擷取網站內容,包含了 HTML+CSS+JS /// </summary> /// <returns>String返回網頁資訊</returns> public string getWebContent() { try { WebClient MyWebClient = new WebClient(); MyWebClient.Credentials = CredentialCache.DefaultCredentials; //擷取或設定用於向Internet資源的請求進行身分識別驗證的網路憑據 Byte[] pageData = MyWebClient.DownloadData(INPATH); //從指定網站下載資料 string pageHtml = Encoding.UTF8.GetString(pageData); //如果擷取網站頁面採用的是GB2312,則使用這句 bool isBool = isMessyCode(pageHtml);//判斷使用哪種編碼 讀取網頁資訊 if (!isBool) { string pageHtml1 = Encoding.UTF8.GetString(pageData); pageHtml = pageHtml1; } else { string pageHtml2 = Encoding.Default.GetString(pageData); pageHtml = pageHtml2; } return pageHtml; } catch (WebException webEx) { Console.WriteLine(webEx.Message.ToString()); return webEx.Message; } } /// <summary> /// 將Html文字 輸出到PDF檔裡 /// </summary> /// <param name="htmlText"></param> /// <returns></returns> public byte[] ConvertHtmlTextToPDF(string htmlText) { if (string.IsNullOrEmpty(htmlText)) { return null; } //避免當htmlText無任何html tag標籤的純文字時,轉PDF時會掛掉,所以一律加上<p>標籤 htmlText = "<p>" + htmlText + "</p>"; MemoryStream outputStream = new MemoryStream();//要把PDF寫到哪個串流 byte[] data = Encoding.UTF8.GetBytes(htmlText);//字串轉成byte[] MemoryStream msInput = new MemoryStream(data); Document doc = new Document();//要寫PDF的檔案,建構子沒填的話預設直式A4 PdfWriter writer = PdfWriter.GetInstance(doc, outputStream); //指定檔案預設開檔時的縮放為100% PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f); //開啟Document檔案 doc.Open(); //使用XMLWorkerHelper把Html parse到PDF檔裡 // XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory()); XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8); //將pdfDest設定的資料寫到PDF檔 PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer); writer.SetOpenAction(action); doc.Close(); msInput.Close(); outputStream.Close(); //回傳PDF檔案 return outputStream.ToArray(); } //設定字型類 public class UnicodeFontFactory : FontFactoryImp { private static readonly string arialFontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arialuni.ttf");//arial unicode MS是完整的unicode字型。 private static readonly string 標楷體Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "KAIU.TTF");//標楷體 public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached) { BaseFont bfChiness = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //可用Arial或標楷體,自己選一個 BaseFont baseFont = BaseFont.CreateFont(標楷體Path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); return new Font(bfChiness, size, style, color); } } }}
C#根據html產生PDF