欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 免費(fèi)組件html轉(zhuǎn)pdf的實(shí)現(xiàn)過程

 更新時(shí)間:2022年06月09日 11:30:07   作者:looper.zhuo  
這篇文章主要介紹了c# 免費(fèi)組件html轉(zhuǎn)pdf的實(shí)現(xiàn)過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

免費(fèi)組件html轉(zhuǎn)pdf

背景

我們?cè)诠究赡苡龅揭恍┪募D(zhuǎn)pdf的場景,這里主要講述html轉(zhuǎn)pdf。

通常在c#里面有很多html轉(zhuǎn)pdf的組件,我們采用第三方的組件,比如 iTextSharp, aspose等,但是有些組件用起來復(fù)雜,需要很多配置,而且在轉(zhuǎn)換出來之后可能出現(xiàn)排版不正確的場景

下面主要介紹Select.HtmlToPdf的使用,很簡單且方面,可以一次性生成幾百頁不是問題,關(guān)鍵是免費(fèi)哦。

1.在guget下載組件

如上有Select.HtmlToPdf和 Select.HtmlToPdf.netcore,兩種的使用差不多,只是Select.HtmlToPdf.netcore支持css效果更好,不過Select.HtmlToPdf.netcore只支持win,不支持linux,這個(gè)有點(diǎn)坑,其他還好,接下來我們使用Select.HtmlToPdf.netcore進(jìn)行演示

2.使用:直接上代碼

 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中圖片,使用相對(duì)地址解析不出來,所以使用替換方式去解決
                    var docStr = converter.ConvertHtmlString(testStr.ToString());
                    doc.Append(docStr);
                }
                doc.Save("xxxx");保存到xxx路徑下
                doc.Close();
 
            }
            catch (Exception e)
            {
                //dosomething 
            }
            Console.ReadLine();
        }
    }

如上一次性打印多張pdf,思路:

1.在本地找到要轉(zhuǎn)換的html文件,當(dāng)然你也可以配置在程序里面,通過流的形式讀出來,也可用file的方法去讀,拿到html字符串

2.創(chuàng)建一個(gè)html轉(zhuǎn)pdf的對(duì)象,創(chuàng)建一個(gè)新的pdf文件對(duì)象

3.通過html轉(zhuǎn)pdf對(duì)象的converthtmlstring去獲取html字符串,另外還提供converurl的方法去把一個(gè)網(wǎng)頁轉(zhuǎn)換換成pdf,是不是很方便切功能強(qiáng)大。

4.save用來保存pdf的路徑,關(guān)閉pdf對(duì)象,操作文成,即可看到

這樣就是實(shí)現(xiàn)了html 轉(zhuǎn)pdf,另外,這個(gè)組件還提供了很多api可用

附上鏈接:https://selectpdf.com/docs/Index.htm

C#如何將html轉(zhuǎn)pdf

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("文件未找到,可能已經(jīng)被刪除");
                    Response.Flush();
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                success = false;
            }
            return "";
        }
protected void Page_Load(object sender, EventArgs e)
{
HtmlToPdf("http://www.deriva.cn");
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論