Я сталкиваюсь с проблемой, преобразующей HTML в PDF с использованием пакета htmlrenderer.pdfsharp в .net. < /p>
При преобразовании в PDF некоторые строки таблицы разделены вертикально на страницах. Иногда сам контент также разделен вертикально. Страница CSS Patch-Break-indide: избегать; , похоже, не работает, как и ожидалось. Попробовал взлома: избежать; Свойство с таблицей , tr , th и td элементы.
, а также попробовал с приведенным ниже методом
using System;
using System.IO;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace HtmlToPdfIssue
{
class Program
{
static void Main(string[] args)
{
// Define the output PDF file path
string outputPath = "OutputWithSplitTable.pdf";
// Get the HTML content
string htmlContent = GetHtmlContent();
try
{
// Convert HTML to PDF
// Note: The 'size' and 'margins' are adjusted to make it more likely for content to split
PdfDocument pdf = PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4, margin: 10);
// Save the PDF
pdf.Save(outputPath);
Console.WriteLine($"PDF generated successfully at: {Path.GetFullPath(outputPath)}");
Console.WriteLine("Please open the PDF and observe the table row splitting issue.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
Console.ReadKey(); // Keep console open
}
private static string GetHtmlContent()
{
// This HTML is designed to create a long table that will likely span multiple pages.
// We're also applying page-break-inside: avoid; to the table rows, as you've tried.
return @"
HTML to PDF Split Table Issue
body {
font-family: Arial, sans-serif;
font-size: 12px;
margin: 0;
padding: 0;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
page-break-inside: avoid !important;
}
tr {
page-break-inside: avoid !important;
break-inside: avoid !important; /* Also trying break-inside */
}
Table Row Splitting Issue Demonstration
This document demonstrates an issue where table rows and content are split vertically across pages when converting HTML to PDF using HtmlRenderer.PdfSharp, despite `page-break-inside: avoid;`.
Below is a table with many rows, designed to span multiple pages. Observe how rows might be cut off or content within cells is split.
Row #
Description
Details
" +
// Generate many rows to ensure the table spans multiple pages
GenerateTableRows(20) +
@"
End of document.
";
}
private static string GenerateTableRows(int count)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 1; i
Подробнее здесь: https://stackoverflow.com/questions/796 ... -splitting
Почему не «разрыв страниц». Избегайте; Предотвращение строки таблицы и разделения контента при преобразовании HTML в PDF ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Ошибка FPDI: «Шаблон не существует!» при импорте страниц PDF для разделения QR-кода
Anonymous » » в форуме Php - 0 Ответы
- 11 Просмотры
-
Последнее сообщение Anonymous
-