@amitmalhotra
Summary
Starting with Aspose.PDF v25.7 the HTML‑to‑PDF conversion uses a newer rendering engine that, by default, parses CSS, executes JavaScript and tries to load external resources. Those extra steps can add noticeable latency, especially when the source HTML is simple and those features are not required.
Why it happens
- New default rendering engine – From v25.7 the engine is based on Aspose.HTML (WebKit). It performs full layout, script execution and resource fetching unless the corresponding options are turned off.
- External‑resource scanning – Even when a custom loader returns
null, the engine still scans the markup for <link>, <script> and <img> tags, which adds processing time.
- Stream position – If the same
MemoryStream is reused, its Position may be at the end after a previous write, causing the Document constructor to read an empty stream and then re‑process, which further slows the operation.
What to do next
- Reset the input stream before loading
using (var htmlStream = new MemoryStream(htmlBytes))
{
htmlStream.Position = 0; // ensure reading starts at the beginning
// …
}
- Disable unneeded features via
HtmlLoadOptions
var htmlOptions = new Aspose.Pdf.HtmlLoadOptions
{
// Prevent the engine from trying to download external files
EnableExternalResources = false, // introduced in v25.7
// Skip JavaScript execution if it is not required
EnableJavaScript = false,
// Omit CSS parsing when the layout is simple
EnableCss = false,
// Keep your existing page settings
PageInfo = new PageInfo
{
Margin = new MarginInfo(0, 20, 0, 20)
}
};
- Choose the rendering engine that best fits your performance needs
// Use the legacy engine (the one that was the default before v25.7)
htmlOptions.HtmlRenderingEngine = HtmlRenderingEngine.Browser;
// Or keep the newer engine but with the reduced‑work settings above
// htmlOptions.HtmlRenderingEngine = HtmlRenderingEngine.WebKit;
- Load the document and save the PDF
using (var pdfStream = new MemoryStream())
{
using (var pdfDoc = new Document(htmlStream, htmlOptions))
{
pdfDoc.Save(pdfStream);
}
}
- If the conversion is still slower than expected, consider using Aspose.HTML directly – Aspose.HTML provides a dedicated
HtmlToPdfConverter that is optimized for high‑speed conversions. Example:
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Pdf;
var converter = new HtmlToPdfConverter();
var pdfSaveOptions = new PdfSaveOptions(); // configure page size, margins, etc. if needed
converter.Convert(htmlStream, pdfSaveOptions, pdfStream);
- Measure the impact
- Wrap the conversion code with a
Stopwatch to record elapsed time before and after applying the changes.
- Compare the results with the same HTML on v25.6 (if available) to confirm the performance gain.
If, after applying the steps above, the latency remains excessive, please provide the size of the HTML payload and any external assets it references. We will investigate further internally and determine whether additional tuning or a hot‑fix is required.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.