使用 DinkToPdf 分隔分页符
Separating Page Breaks with DinkToPdf
我的 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);
我有 this HTML file I have(太大而无法粘贴到这里)。
The generated PDF is mostly fine,但第 3 页的分页符不正确。较大的内容但与以前的内容相反 - 我认为是因为它不适合下一页。
如何让 page
class 的每个 DIV 在新页面开始?
如果您想在每页之后添加分页符,请将其添加到您的
第 class 页:
page-break-after: always;
我用过“page-break-after:总是;”在 div 内,使用 DinkToPdf
对我来说非常完美
var sb = new StringBuilder();
sb.AppendFormat("<div style='page-break-after: always;'></div>");
对于那些想要防止 table 中间分页的人,如下例所示:
只需使用 css page-break-inside with avoid value:
table {
page-break-inside: avoid;
}
找到:https://github.com/rdvojmoc/DinkToPdf/issues/7
CSS:
table {
page-break-inside: auto;
}
tr {
page-break-inside: avoid;
page-break-after: auto;
}
thead {
display: table-header-group;
}
tfoot {
display: table-footer-group;
}
我的 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);
我有 this HTML file I have(太大而无法粘贴到这里)。
The generated PDF is mostly fine,但第 3 页的分页符不正确。较大的内容但与以前的内容相反 - 我认为是因为它不适合下一页。
如何让 page
class 的每个 DIV 在新页面开始?
如果您想在每页之后添加分页符,请将其添加到您的 第 class 页:
page-break-after: always;
我用过“page-break-after:总是;”在 div 内,使用 DinkToPdf
对我来说非常完美 var sb = new StringBuilder();
sb.AppendFormat("<div style='page-break-after: always;'></div>");
对于那些想要防止 table 中间分页的人,如下例所示:
只需使用 css page-break-inside with avoid value:
table {
page-break-inside: avoid;
}
找到:https://github.com/rdvojmoc/DinkToPdf/issues/7
CSS:
table {
page-break-inside: auto;
}
tr {
page-break-inside: avoid;
page-break-after: auto;
}
thead {
display: table-header-group;
}
tfoot {
display: table-footer-group;
}