一、添加OpenHtmlToPdf,O2S.Components.PDFRender4NET引用
OpenHtmlToPdf:是一個.NET庫(開源的),用於將HTML文檔呈現爲PDF格式。
github地址:https://github.com/vilppu/OpenHtmlToPdf
O2S.Components.PDFRender4NET:是一個.NET庫,用於將PDF文檔呈現爲圖片。
想要在C#中把html轉成PDF,PDF轉成圖片,首先得要有個C#版的驅動。
通過網絡下載或nuget安裝,得到OpenHtmlToPdf、O2S.Components.PDFRender4NET相關的dll,添加到項目中引用。
第一步:在項目中右鍵,選擇管理NuGet管理包
第二步:搜索OpenHtmlToPdf,選中安裝
第三步:下載O2S.Components.PDFRender4NET.dll,在Nuget中沒找到,只能自己找資源下載了
下載地址:https://download.csdn.net/download/u011301348/12442663
下載完成後添加dll引用到項目中
通過這三步,在項目中有OpenHtmlToPdf、O2S.Components.PDFRender4NET的引用,表示添加成功
二、創建PDFTools工具類
/// <summary>
/// PDF工具
/// </summary>
public class PDFTools
{
/// <summary>
/// html轉pdf
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string HtmlToPdf(string html)
{
var document = Pdf.From(html)
.OfSize(PaperSize.A4) //大小,這裏選A4紙大小
.WithGlobalSetting("margin.top", "0.4cm"); //設置全局樣式
//此屬性的值在32位進程中爲4,在64位進程中爲8。
if (IntPtr.Size == 4)
{
document = document.WithObjectSetting("load.zoomFactor", "1.5");
}
var result = document.Content();
string filePath = "pdf/" + DateTime.Now.ToString("yyyyMMddHH") + "/";//路徑
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + filePath);//保存在當前項目跟路徑下
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".pdf";
//通過stream把html寫入到pdf文件中
FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + filePath + fileName, FileMode.Create, FileAccess.Write);
fs.Write(result, 0, result.Length);
fs.Close();
return filePath + fileName;//返回pdf路徑
}
/// <summary>
/// PDF轉化爲圖片
/// </summary>
/// <param name="filePath">文件路徑</param>
/// <param name="resolution">分辨率</param>
/// <returns></returns>
public static List<string> PDFToIMG(string filePath, float resolution)
{
string imgPath = "pdf/IMG/" + DateTime.Now.ToString("yyyyMMddHH") + "/";
//如果文件夾不存在,則創建
if (!Directory.Exists(imgPath))
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + imgPath);
}
PDFFile file = PDFFile.Open(filePath);
int pageCount = file.PageCount;
List<string> imgPathList = new List<string>();
for (int i = 0; i < pageCount; i++)
{
string imgName = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".jpg";
Bitmap img = file.GetBWPageImage(i, resolution);
img.Save(AppDomain.CurrentDomain.BaseDirectory + imgPath + imgName, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPathList.Add(imgPath + imgName);
}
return imgPathList;
}
}
三、調用
在HomeController中添加一下方法。
1.定義組裝html生成pdf方法
public string CreatePdf()
{
StringBuilder detail = new StringBuilder();
detail.Append(@"<p><span style='FONT-FAMILY:宋體;BACKGROUND:white;COLOR:#333333'></span></p><p><span style='FONT-FAMILY:宋體;BACKGROUND:white;COLOR:#333333'>pdf商品</span></p>");
detail.Append(@"<table style='WIDTH:280px' height='159' cellspacing='0' cellpadding='0' width='807' align='center' uetable='null' data-sort='sortDisabled'><tbody>");
detail.Append("<tr class='firstRow'>");
detail.Append("<td valign='top' width='47'><p><span style='FONT-SIZE:13px;FONT-FAMILY:宋體;BACKGROUND:white;COLOR:#333333'>序號</span></p></td>");
detail.Append("<td valign ='top' width='83'><p style='TEXT-ALIGN:center'><span style='FONT-SIZE:13px;FONT-FAMILY:宋體;BACKGROUND:white;COLOR:#333333'>商品名稱</span></p></td>");
detail.Append("<td valign='top' width='132'><p style='TEXT-ALIGN:center'><span style='FONT-SIZE:13px;FONT-FAMILY:宋體;BACKGROUND:white;COLOR:#333333'>品牌</span></p ></td>");
detail.Append("<td valign='top' width='75'><p style='TEXT-ALIGN:center'><span style='FONT-SIZE:13px;FONT-FAMILY:宋體;BACKGROUND:white;COLOR:#333333'>單位</span></span></p></td>");
detail.Append("<td valign='top' width='50'><p style='TEXT-ALIGN:center'><span style='FONT-SIZE:13px;FONT-FAMILY:宋體;BACKGROUND:white;COLOR:#333333'>規格</span></p></td>");
detail.Append("<td valign='top' width='66'><p style='TEXT-ALIGN:center'><span style='FONT-SIZE:13px;FONT-FAMILY:宋體;BACKGROUND:white;COLOR:#333333'>價格</span></p></td>");
detail.Append("</tr>");
for (int i = 1; i < 100; i++)
{
detail.Append("<tr>");
detail.Append($"<td valign='top' width='47'>{i}</td>");
detail.Append($"<td valign='top' width='83'>商品{i}</td>");
detail.Append($"<td valign='top' width='83'>品牌{i}</td>");
detail.Append($"<td valign='top' width='83'>單位{i}</td>");
detail.Append($"<td valign='top' width='83'>規格{i}</td>");
detail.Append($"<td valign='top' width='83'>價格{i}</td>");
detail.Append("</tr>");
i++;
}
detail.Append("</tbody></table>");
//生成PDF
string html = $"<!DOCTYPE html><html><head><meta charset='UTF-8'><title>pdf</title></head><body>{detail.ToString()}</body></html>
2.在Index中調用。
public ActionResult Index()
{
ViewBag.pdfpath = CreatePdf();
return View();
}
3.查看結果
4.瀏覽器中打開文件效果
5.轉換成圖片
調用方法:PDFTools.PDFToIMG("文件.pdf", 200);
生成圖片文件
瀏覽查看
四、代碼下載地址