pdfHTML: Processing of extremely long elements
When processing paragraphs with a lot of nested subelements, iText would act suboptimally by not cleaning up certain parent links after laying out intermediate renderers. As a result, iText would consume more memory than expected during the conversion.
We've fixed this issue in our 7.1.15 release by making optimizations to the Core layout module in order to prevent the memory leak from happening. You can test out the issue by using the example code and resources below.
When running the sample on an older release you will notice that around 600-700MB of memory will be utilized. Compare this to only 100MB of memory when you run it on our newest version, which we're sure you'll agree is a significant improvement.
Java:
JAVA
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Example {
public static final String DEST = "results/result.pdf";
static {
new File(DEST).getParentFile().mkdirs();
}
public static void main(String[] args) throws Exception {
createPdf();
}
public static void createPdf() throws Exception {
Path p = Paths.get("src/main/resources/example.html");
PdfWriter writer = new PdfWriter(new FileOutputStream(DEST));
HtmlConverter.convertToPdf(Files.newInputStream(p), writer);
}
}
C#:
C#
using System.IO;
using iText.Html2pdf;
using iText.Kernel.Pdf;
namespace ConsoleApp1
{
class Program
{
private static string DEST = "result.pdf";
static void Main(string[] args)
{
PdfWriter writer = new PdfWriter(new FileStream(DEST, FileMode.Create));
HtmlConverter.ConvertToPdf(new FileStream("example.html", FileMode.Open), writer);
}
}
}