Hey,
I’m wondering what is the best, most performant way to convert HTML to PDF using Aspose.HTML and add header/footer to every PDF page using Aspose.PDF.
I tried using just Aspose.PDF, but the result was not rendered correctly, hence i moved to testing Aspose.HTML + Aspose.PDF combo.
I also have a solution using Aspose.Words to convert HTML into docx, adding header/footer, and then saving it as a pdf.
Solution using Aspose.Words is twice as fast as the approach with Aspose.HTML + Aspose.PDF, but it does not handle my cases for correctly rendering some HTML+CSS.
This is why I’m asking - what would be the most performant way to convert HTML into PDF with header/footer?
Current code looks something like this:
var html = ReadHtmlFromFile();
Aspose.Html.Converters.Converter.ConvertHTML(html, "../", new Aspose.Html.Saving.PdfSaveOptions(), "output.pdf");
using var pdfDocument = new Aspose.Pdf.Document("output.pdf");
// add header and footer into generated PDF
var headerHtml = File.ReadAllText("ctrtemplate-header.html");
var footerHtml = File.ReadAllText("ctrtemplate-footer.html");
for (var i = 1; i <= pdfDocument.Pages.Count; i++)
{
AddHeaderToPdf(headerHtml, i, pdfDocument);
AddFooterToPdf(footerHtml, i, pdfDocument);
}
// pdfDocument.Save("output-with-hf.pdf");
using MemoryStream ms = new MemoryStream();
pdfDocument.Save(ms, SaveFormat.Pdf);
return ms.ToArray();
@mlamla
Cause
To convert HTML to PDF while adding headers and footers efficiently, using Aspose.HTML in combination with Aspose.PDF is an effective approach. However, performance can vary based on the complexity of the HTML and CSS being rendered.
Solution
To enhance performance and ensure correct rendering, consider the following steps:
- Optimize HTML Content: Simplify your HTML and CSS where possible. Complex styles can slow down rendering.
- Use Aspose.HTML for Conversion: Continue using Aspose.HTML for the initial conversion to PDF.
- Add Headers and Footers Efficiently: Instead of looping through each page to add headers and footers, create a template for the header and footer and apply them in one go.
Here’s an optimized version of your code:
var html = ReadHtmlFromFile();
var pdfOptions = new Aspose.Html.Saving.PdfSaveOptions();
Aspose.Html.Converters.Converter.ConvertHTML(html, "../", pdfOptions, "output.pdf");
using var pdfDocument = new Aspose.Pdf.Document("output.pdf");
// Load header and footer HTML
var headerHtml = File.ReadAllText("ctrtemplate-header.html");
var footerHtml = File.ReadAllText("ctrtemplate-footer.html");
// Create header and footer for all pages
foreach (var page in pdfDocument.Pages)
{
AddHeaderToPdf(headerHtml, page);
AddFooterToPdf(footerHtml, page);
}
// Save the final PDF
using MemoryStream ms = new MemoryStream();
pdfDocument.Save(ms, SaveFormat.Pdf);
return ms.ToArray();
Next Steps
- Test the performance of the updated code.
- If rendering issues persist, consider using Aspose.Words as a fallback for specific HTML cases where Aspose.HTML struggles.
- Monitor the complexity of your HTML/CSS and adjust as necessary for optimal performance.
Please implement these changes and let me know if you need further assistance!
Not helping at all. Just copied my code, point 3 makes no sense.
Any human input will be much appreciated.
@mlamla
We will surely investigate this scenario of yours. However, can you please share some details like:
- Same HTML/CSS in .zip format
- How much time does Aspose.Words take at your end
- How much time did you observe while using Aspose.HTML + Aspose.PDF
- Which API is taking more time to process respective operations e.g. Aspose.HTML or Aspose.PDF
- System details like OS Name and Version, RAM Size, etc.