我的 Program.cs 中有这个 C#:
var page = "plain";
var slnpath = $@"{Directory.GetCurrentDirectory()}\..\..\..\..";
var htmlpath = $@"{slnpath}\HtmlTemplates\{page}.html";
var pdfpath = $@"{slnpath}\PdfFiles\{page}.pdf";
var dllpath = $@"{slnpath}\DinkNative64bit\libwkhtmltox.dll";
var html = new StringBuilder(File.ReadAllText(htmlpath));
var _converter = new SynchronizedConverter(new PdfTools());
var context = new CustomAssemblyLoadContext().LoadUnmanagedLibrary(dllpath);
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = new MarginSettings { Top = 10 },
DocumentTitle = "PDF Report",
//Out = @"D:\PDFCreator\Employee_Report.pdf" USE THIS PROPERTY TO SAVE PDF TO A PROVIDED LOCATION
};
var objectSettings = new ObjectSettings
{
PagesCount = true,
HtmlContent = html.ToString(),
//Page = "https://code-maze.com/", USE THIS PROPERTY TO GENERATE PDF CONTENT FROM AN HTML PAGE
WebSettings = { DefaultEncoding = "utf-8" }, //, UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },
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 }
};
//_converter.Convert(pdf); IF WE USE Out PROPERTY IN THE GlobalSettings CLASS, THIS IS ENOUGH FOR CONVERSION
var file = _converter.Convert(pdf);
File.WriteAllBytes(pdfpath, file);
我有这个 HTML 文件(太大,无法粘贴到这里)。
生成的 PDF 大部分都很好,但在第 3 页上的分页符不正确。较大的内容与之前的内容相抵触——我认为是因为它不适合下一页。
如何使每个带有page
类的 DIV 都从新页面开始?