本文,主要記錄在處理.NET Core Web API項目時使用DinkToPDF庫輕松生成PDF。
本文源碼 https://github.com/forestGzh/HtmlToPdf
DinkToPdf吁峻,是一個跨平臺的庫用含,是Webkit HTML to PDF庫的包裝。它使用WebKit引擎將HTML轉(zhuǎn)換為PDF缸夹。它可以在.NET Core項目中將HTML字符串生成PDF文檔,或者從現(xiàn)有的HTML頁面創(chuàng)建PDF文檔芽唇。
由于官方的DinkToPdf https://github.com/rdvojmoc/DinkToPdf 最新的更新已經(jīng)是三年前的了,原來的DinkToPdf需要引入幾個dll,這些dll也沒有更新疚膊,導(dǎo)致目前在linux系統(tǒng)中,還有docker容器環(huán)境中無法使用傀蚌。(github上也有很多issue都是關(guān)于這些問題的库继,好像都沒有特別好的處理辦法)榨惰。
后來發(fā)現(xiàn)了一個Haukcode.DinkToPdf這個庫,是作者根據(jù)DinkToPdf改造的(https://www.nuget.org/packages/Haukcode.DinkToPdf/
),將一些dll整合進去了,也兼容容器環(huán)境了嫁佳。
安裝,在.csproj中加上下面這行湿弦,然后restore一下〖⒙或者直接去NuGet包管理器搜索安裝也是一樣的闯割。
<PackageReference Include="Haukcode.DinkToPdf" Version="1.1.2" />
或
Install-Package Haukcode.DinkToPdf -Version 1.1.2
在Startup.cs中添加:
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));//DinkToPdf注入
創(chuàng)建IPDFService
using System;
namespace HtmlToPdf.Services
{
/// <summary>
/// 與pdf相關(guān)
/// </summary>
public interface IPDFService
{
/// <summary>
/// 創(chuàng)建PDF
/// </summary>
/// <param name="htmlContent">傳入html字符串</param>
/// <returns></returns>
byte[] CreatePDF(string htmlContent);
}
}
創(chuàng)建PDFService
using System;
using DinkToPdf;
using DinkToPdf.Contracts;
namespace HtmlToPdf.Services
{
/// <summary>
/// 與pdf相關(guān)
/// </summary>
public class PDFService : IPDFService
{
private IConverter _converter;
public PDFService(IConverter converter)
{
_converter = converter;
}
/// <summary>
/// 創(chuàng)建PDF
/// </summary>
/// <param name="htmlContent">傳入html字符串</param>
/// <returns></returns>
public byte[] CreatePDF(string htmlContent)
{
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
//Margins = new MarginSettings
//{
// Top = 10,
// Left = 0,
// Right = 0,
//},
DocumentTitle = "PDF Report",
};
var objectSettings = new ObjectSettings
{
PagesCount = true,
HtmlContent = htmlContent,
// Page = "www.baidu.com", //USE THIS PROPERTY TO GENERATE PDF CONTENT FROM AN HTML PAGE 這里是用現(xiàn)有的網(wǎng)頁生成PDF
//WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },
WebSettings = { DefaultEncoding = "utf-8" },
//HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },
//FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" }
};
var pdf = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { objectSettings }
};
var file = _converter.Convert(pdf);
//return File(file, "application/pdf");
return file;
}
}
}
在Startup.cs中依賴注入:
services.AddTransient<IPDFService, PDFService>();
創(chuàng)建TemplateGenerator,生成html字符串
using System;
using System.Text;
namespace HtmlToPdf
{
public static class TemplateGenerator
{
/// <summary>
/// 獲取HTML字符串
/// </summary>
/// <returns></returns>
public static string GetPDFHTMLString()
{
StringBuilder sb = new StringBuilder();
sb.Append(@"
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<style>
</style>
</head>
<body>
<div>
這是一個網(wǎng)頁瑞你!
</div>
</body>
</html>
");
return sb.ToString();
}
}
}
修改ValuesController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HtmlToPdf.Services;
using Microsoft.AspNetCore.Mvc;
namespace HtmlToPdf.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private IPDFService _PDFService;
public ValuesController(IPDFService pDFService)
{
_PDFService = pDFService;
}
[HttpGet("pdf")]
public FileResult GetPDF()
{
//獲取html模板
var htmlContent = TemplateGenerator.GetPDFHTMLString();
//生成PDF
var pdfBytes = _PDFService.CreatePDF(htmlContent);
return File(pdfBytes, "application/pdf");
}
}
}
測試:
瀏覽器輸入 https://localhost:5001/api/values/pdf
可以看到html字符串已經(jīng)生成了pdf文檔
本文項目部署的環(huán)境是在Docker容器中嫩实,采用ASP.NET Core官方的鏡像。
這個鏡像是精簡的,所以沒有一些字體氧骤,那就這樣部署到docker容器中的話網(wǎng)頁會亂碼际歼。
所以,要手動添加字體颅筋。如下宙暇,下載字體文件simsun.ttc占贫。
在dockerfile文件中添加:
ADD ./simsun.ttc /usr/share/fonts/simsun.ttc
構(gòu)建鏡像部署即可