這篇文章主要介紹了c#免費組件html轉pdf怎么實現的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇c#免費組件html轉pdf怎么實現文章都會有所收獲,下面我們一起來看看吧。
如上有Select.HtmlToPdf和 Select.HtmlToPdf.netcore,兩種的使用差不多,只是Select.HtmlToPdf.netcore支持css效果更好,不過Select.HtmlToPdf.netcore只支持win,不支持linux,這個有點坑,其他還好,接下來我們使用Select.HtmlToPdf.netcore進行演示
static void Main(string[] args) { try { string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "文件夾", "文件夾下的html文件"); string line = ""; var testStr = new StringBuilder(); using (StreamReader sr = new StreamReader(fullPath)) { while ((line = sr.ReadLine()) != null) { testStr.Append(line); } } SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf(); PdfDocument doc = new PdfDocument(); for (int i = 0; i < 10; i++) { testStr.Replace("#ImageUrl#", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "文件夾", "文件夾下的圖片"));//由于html中圖片,使用相對地址解析不出來,所以使用替換方式去解決 var docStr = converter.ConvertHtmlString(testStr.ToString()); doc.Append(docStr); } doc.Save("xxxx");保存到xxx路徑下 doc.Close(); } catch (Exception e) { //dosomething } Console.ReadLine(); } }
如上一次性打印多張pdf,思路:
1.在本地找到要轉換的html文件,當然你也可以配置在程序里面,通過流的形式讀出來,也可用file的方法去讀,拿到html字符串
2.創建一個html轉pdf的對象,創建一個新的pdf文件對象
3.通過html轉pdf對象的converthtmlstring去獲取html字符串,另外還提供converurl的方法去把一個網頁轉換換成pdf,是不是很方便切功能強大。
4.save用來保存pdf的路徑,關閉pdf對象,操作文成,即可看到
這樣就是實現了html 轉pdf,另外,這個組件還提供了很多api可用
public string HtmlToPdf(string url) { bool success = true; // string dwbh = url.Split('?')[1].Split('=')[1]; //CommonBllHelper.CreateUserDir(dwbh); //url = Request.Url.Host + "/html/" + url; string guid = DateTime.Now.ToString("yyyyMMddhhmmss"); string pdfName = "1.pdf"; //string path = Server.MapPath("~/kehu/" + dwbh + "/pdf/") + pdfName; string path = "D:\\" + pdfName; try { if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path)) success = false; string str = Server.MapPath("~\\bin\\wkhtmltopdf.exe"); Process p = System.Diagnostics.Process.Start(str, url+" "+path); p.WaitForExit(); if (!System.IO.File.Exists(str)) success = false; if (System.IO.File.Exists(path)) { FileStream fs = new FileStream(path, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); if (Request.UserAgent != null) { string userAgent = Request.UserAgent.ToUpper(); if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0) { Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(pdfName, Encoding.UTF8)); } else { Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName); } } Response.ContentEncoding = Encoding.UTF8; Response.ContentType = "application/octet-stream"; //通知瀏覽器下載文件而不是打開 Response.BinaryWrite(bytes); Response.Flush(); Response.End(); fs.Close(); System.IO.File.Delete(path); } else { Response.Write("文件未找到,可能已經被刪除"); Response.Flush(); Response.End(); } } catch (Exception ex) { success = false; } return ""; }
protected void Page_Load(object sender, EventArgs e) { HtmlToPdf("http://www.deriva.cn"); }
關于“c#免費組件html轉pdf怎么實現”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“c#免費組件html轉pdf怎么實現”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。