c# 免費(fèi)組件html轉(zhuǎn)pdf的實(shí)現(xiàn)過(guò)程
免費(fèi)組件html轉(zhuǎn)pdf
背景
我們?cè)诠究赡苡龅揭恍┪募D(zhuǎn)pdf的場(chǎng)景,這里主要講述html轉(zhuǎn)pdf。
通常在c#里面有很多html轉(zhuǎn)pdf的組件,我們采用第三方的組件,比如 iTextSharp, aspose等,但是有些組件用起來(lái)復(fù)雜,需要很多配置,而且在轉(zhuǎn)換出來(lái)之后可能出現(xiàn)排版不正確的場(chǎng)景
下面主要介紹Select.HtmlToPdf的使用,很簡(jiǎn)單且方面,可以一次性生成幾百頁(yè)不是問(wèn)題,關(guān)鍵是免費(fèi)哦。
1.在guget下載組件

如上有Select.HtmlToPdf和 Select.HtmlToPdf.netcore,兩種的使用差不多,只是Select.HtmlToPdf.netcore支持css效果更好,不過(guò)Select.HtmlToPdf.netcore只支持win,不支持linux,這個(gè)有點(diǎn)坑,其他還好,接下來(lái)我們使用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ì)地址解析不出來(lái),所以使用替換方式去解決
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)然你也可以配置在程序里面,通過(guò)流的形式讀出來(lái),也可用file的方法去讀,拿到html字符串
2.創(chuàng)建一個(gè)html轉(zhuǎn)pdf的對(duì)象,創(chuàng)建一個(gè)新的pdf文件對(duì)象
3.通過(guò)html轉(zhuǎn)pdf對(duì)象的converthtmlstring去獲取html字符串,另外還提供converurl的方法去把一個(gè)網(wǎng)頁(yè)轉(zhuǎn)換換成pdf,是不是很方便切功能強(qiáng)大。
4.save用來(lái)保存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";
//通知瀏覽器下載文件而不是打開(kāi)
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)文章
WinForm中變Enter鍵為T(mén)ab鍵實(shí)現(xiàn)焦點(diǎn)轉(zhuǎn)移的方法
這篇文章主要介紹了WinForm中變Enter鍵為T(mén)ab鍵實(shí)現(xiàn)焦點(diǎn)轉(zhuǎn)移的方法,主要通過(guò)一個(gè)ControlTools類來(lái)實(shí)現(xiàn)該功能,需要的朋友可以參考下2014-08-08
c# List find()方法返回值的問(wèn)題說(shuō)明(返回結(jié)果為對(duì)象的指針)
本篇文章主要介紹了c#中List find()方法返回值的問(wèn)題說(shuō)明(返回結(jié)果為對(duì)象的指針) 需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
C# List集合中獲取重復(fù)值及集合運(yùn)算詳解
這篇文章主要介紹了C# List集合中獲取重復(fù)值及集合運(yùn)算詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Unity實(shí)現(xiàn)高效的音效管理類的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Unity實(shí)現(xiàn)高效的音效管理類,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2023-03-03
C#保存圖片到數(shù)據(jù)庫(kù)并讀取顯示圖片的方法
將圖像保存到SQL server2000的Image字段中2013-04-04
Visual C#類的定義及實(shí)現(xiàn)方法實(shí)例解析
這篇文章主要介紹了Visual C#類的定義及實(shí)現(xiàn)方法實(shí)例解析,對(duì)于新手來(lái)說(shuō)有不錯(cuò)的借鑒學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-07-07
C#中緩存System.Web.Caching用法總結(jié)
本文詳細(xì)講解了C#中緩存System.Web.Caching的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#實(shí)現(xiàn)多個(gè)計(jì)時(shí)器記錄不同定時(shí)時(shí)間
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)多個(gè)計(jì)時(shí)器記錄不同定時(shí)時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12

