dotnet Core 中的 PDF 实现 - DinkTOPdf
PDF Implementation in dotnet Core - DinkTOPdf
dinktopdf 块的实施
掘金link:
https://www.nuget.org/packages/Haukcode.DinkToPdf/1.1.2?_src=template
谁能分享在.netCore中实现pdf文件生成的步骤?
如何在将文件作为流发送以生成 pdf 之前生成模板?
如何在Startup.cs中注入依赖?
实施步骤:
使用来自 https://www.nuget.org/packages/Haukcode.DinkToPdf/1.1.2?_src=template
的金块
使用把手(javascript 库)从 html 生成模板
public class PdfExportTemplateBuilder : BaseTemplate
{
public static string BuildTemplate(EmployeeDetails employessDetails, string id, string name)
{
var html = File.ReadAllText(Path.Combine(GetCurrentFolder(), @"EMPLOYEE\Template.Pdf.Employee.html")); //create the folder in current Assembly
html = html.Replace(ID_PLACEHOLDER, string.IsNullOrWhiteSpace(id) ? "---" : id);
html = html.Replace(NAME_PLACEHOLDER, string.IsNullOrWhiteSpace(name) ? "---" : name);
html = html.Replace(REPORT_DATE, DateTime.UtcNow.ToString());
html = html.Replace(REPORT_YEAR, DateTime.UtcNow.Year.ToString());
var template = Handlebars.Compile(html);
return template(employessDetails);
}
}
public class BaseTemplate
{
protected const string ID_PLACEHOLDER= "{{ID}}";
protected const string NAME_PLACEHOLDER= "{{NAME}}";
}
- 通过html模板转换为PDF
public async Task<Stream> DownloadPdfAsync(User loggedInUser, string name)
{
var html = PdfExportTemplateBuilder .BuildTemplate(employeeDetails,id,name);
var conversionOptions = new ConversionOptions
{
Margins = new MarginSettings(10, 5, 10, 5),
};
var pdfStream = new MemoryStream(_htmlToPdfConverter.ConvertHtml(html, conversionOptions));
pdfStream.Position = 0;
return pdfStream; //returns file as stream
//Access as return File(result, "application/pdf", "EmployeeDetails.pdf") from calling function.
}
}
- _htmlToPdfConverter 实现
public class HtmlToPdfConverter : IHtmlToPdfConverter
{
private readonly IConverter _converter;
private const string FontName = "Arimo, Arial, Helvetica, sans-serif";
private const string CenterFooterText = "Page [page] of [toPage]";
private const string RightFooterTextFormat = "Report Date: {0}";
private const int FontSize = 6;
private const double Spacing = 7;
public HtmlToPdfConverter(IConverter converter) => _converter = converter;
public byte[] ConvertHtml(string html, ConversionOptions conversionOptions)
{
var doc = new HtmlToPdfDocument()
{
GlobalSettings = {
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = conversionOptions.Margins,
},
Objects = {
new ObjectSettings() {
PagesCount = true,
HtmlContent = html,
WebSettings = { EnableIntelligentShrinking = false },
FooterSettings = {
FontSize = FontSize,
FontName = FontName,
Right = string.Format(RightFooterTextFormat, DateTime.UtcNow.ToString()),
Center = CenterFooterText,
Spacing = Spacing
}
}
}
};
return _converter.Convert(doc);
}
}
- 在 Startup.cs 中注入依赖项(对于 .Net 核心)
services.AddSingleton<IHtmlToPdfConverter, HtmlToPdfConverter>();
services.AddSingleton(typeof(IConverter), new
SynchronizedConverter(new PdfTools()));
dinktopdf 块的实施 掘金link: https://www.nuget.org/packages/Haukcode.DinkToPdf/1.1.2?_src=template
谁能分享在.netCore中实现pdf文件生成的步骤? 如何在将文件作为流发送以生成 pdf 之前生成模板?
如何在Startup.cs中注入依赖?
实施步骤:
使用来自 https://www.nuget.org/packages/Haukcode.DinkToPdf/1.1.2?_src=template
的金块使用把手(javascript 库)从 html 生成模板
public class PdfExportTemplateBuilder : BaseTemplate
{
public static string BuildTemplate(EmployeeDetails employessDetails, string id, string name)
{
var html = File.ReadAllText(Path.Combine(GetCurrentFolder(), @"EMPLOYEE\Template.Pdf.Employee.html")); //create the folder in current Assembly
html = html.Replace(ID_PLACEHOLDER, string.IsNullOrWhiteSpace(id) ? "---" : id);
html = html.Replace(NAME_PLACEHOLDER, string.IsNullOrWhiteSpace(name) ? "---" : name);
html = html.Replace(REPORT_DATE, DateTime.UtcNow.ToString());
html = html.Replace(REPORT_YEAR, DateTime.UtcNow.Year.ToString());
var template = Handlebars.Compile(html);
return template(employessDetails);
}
}
public class BaseTemplate
{
protected const string ID_PLACEHOLDER= "{{ID}}";
protected const string NAME_PLACEHOLDER= "{{NAME}}";
}
- 通过html模板转换为PDF
public async Task<Stream> DownloadPdfAsync(User loggedInUser, string name)
{
var html = PdfExportTemplateBuilder .BuildTemplate(employeeDetails,id,name);
var conversionOptions = new ConversionOptions
{
Margins = new MarginSettings(10, 5, 10, 5),
};
var pdfStream = new MemoryStream(_htmlToPdfConverter.ConvertHtml(html, conversionOptions));
pdfStream.Position = 0;
return pdfStream; //returns file as stream
//Access as return File(result, "application/pdf", "EmployeeDetails.pdf") from calling function.
}
}
- _htmlToPdfConverter 实现
public class HtmlToPdfConverter : IHtmlToPdfConverter
{
private readonly IConverter _converter;
private const string FontName = "Arimo, Arial, Helvetica, sans-serif";
private const string CenterFooterText = "Page [page] of [toPage]";
private const string RightFooterTextFormat = "Report Date: {0}";
private const int FontSize = 6;
private const double Spacing = 7;
public HtmlToPdfConverter(IConverter converter) => _converter = converter;
public byte[] ConvertHtml(string html, ConversionOptions conversionOptions)
{
var doc = new HtmlToPdfDocument()
{
GlobalSettings = {
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = conversionOptions.Margins,
},
Objects = {
new ObjectSettings() {
PagesCount = true,
HtmlContent = html,
WebSettings = { EnableIntelligentShrinking = false },
FooterSettings = {
FontSize = FontSize,
FontName = FontName,
Right = string.Format(RightFooterTextFormat, DateTime.UtcNow.ToString()),
Center = CenterFooterText,
Spacing = Spacing
}
}
}
};
return _converter.Convert(doc);
}
}
- 在 Startup.cs 中注入依赖项(对于 .Net 核心)
services.AddSingleton<IHtmlToPdfConverter, HtmlToPdfConverter>();
services.AddSingleton(typeof(IConverter), new
SynchronizedConverter(new PdfTools()));