Hang when content cannot fit constraint after 2+ RowSpan cell
Describe the bug
Adding an image that exceeds its constraint will, in some scenarios, cause the application to "hang" until memory exhaustion. Rather than throwing a DocumentLayoutException exception, the execution of GeneratePdf will not complete, the CPU will remain busy, and the process' memory will steadily climb. Based on the minimal code needed to reproduce the issue, it appears to be related to the use of table cell with a RowSpan greater than 1 followed by a cell that cannot fit within its available constraints.
To Reproduce
A minimal .NET standalone example is provided below that can be executed with dotnet run and some arguments to illustrate a few different cases. It generates a 768x1024 page containing a table with two cells: the first containing text, and the second containing an image. The arguments specify the RowSpan value, rendered text, and the image height respectively.
#: package [email protected]
#: package [email protected]
using System.IO;
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
uint rowSpan = uint.Parse(args[0]);
string text = args[1];
int imageHeight = int.Parse(args[2]);
QuestPDF.Settings.License = LicenseType.Professional;
var result = Document
.Create(container =>
{
container.Page(page =>
{
page.Size(768, 1024);
page.Content().Table(table =>
{
table.ColumnsDefinition(column => column.RelativeColumn());
table.Cell().RowSpan(rowSpan).Text(text);
table.Cell().Image(CreateImage(width: 768, height: imageHeight));
});
});
})
.GeneratePdf();
static QuestPDF.Infrastructure.Image CreateImage(int width, int height)
{
using var image = new Image<Rgba32>(width, height);
using var stream = new MemoryStream();
image.SaveAsGif(stream);
return QuestPDF.Infrastructure.Image.FromBinaryData(stream.ToArray());
}
The program will successfully create the PDF when generated with an image height of 1009px. However, making the image 1px taller will cause a hang if the RowSpan is 2+ despite having enough space on a second page.
dotnet run pdf.cs 1 " " 1009 # Ok
dotnet run pdf.cs 2 " " 1009 # Ok
dotnet run pdf.cs 1 " " 1010 # Ok
dotnet run pdf.cs 2 " " 1010 # Hang; Expect: 2 Pages
Changing the text of the first cell from whitespace to "Text" will work though.
dotnet run pdf.cs 2 "Text" 1010 # Ok
Lastly, an image that is taller than the page should consistently throw a DocumentLayoutException exception but hangs if 2+.
dotnet run pdf.cs 1 "Text" 1025 # Ok; Result: DocumentLayoutException
dotnet run pdf.cs 2 "Text" 1025 # Hang; Expect: DocumentLayoutException
Expected behavior
-
GeneratePdfshould always return from execution with a either result or exception. -
DocumentLayoutExceptionshould be thrown when wrapping onto a new page still doesn't satisfy constraints. - Render the image on the next page, if it can fit, regardless of the prior cell's
RowSpanvalue.
Environment QuestPDF Version: 2025.7.4, 2025.12.0-alpha4 .NET Version: 10.0.100 Operating Systems: Windows Enterprise 11, macOS Sequioa 15.6, Microsoft Azure Web App (Linux)
Additional context The original QuestPDF use case where we discovered the issue was a 12 column layout utilizing both column and row spans.