Hi team,
I’m trying to generate PDF from HTML template but this conversion goes into infinite loop and pdf is not generated. I’m using Aspose html 23.5.0 and Aspose pdf 23.6.0 in our application.
We are also tried upgrading both packages to latest versions but still facing the same issue.
I’m using Aspose.Html.Converters.Converter.ConvertHTML(HTMLDocument document, PdfSaveOptions options, ICreateStreamProvider provider) method for converting html to pdf.
We are fetching data from external source and replacing values in template. Now we are converting html template to aspose template and then converting to pdf.
below is the small portion of our code -
//Fetching data
var summary = await _summaryRepository.GetItemAsync(id.ToString());
string serializedJson = JsonConvert.SerializeObject(summary);
// Setup default template with placeholders(only a portion of code)
string documentPath = Path.Combine(templatePath, “summarytemplate.html”);
HTMLDocument templateDocument = new HTMLDocument(documentPath);
templateDocument.Title = summary?.Company;
// Overlay data from datastore in the default template
templateDocument.GetElementById(“uwRationaleNotes”).InnerHTML = summary?.UWRationale?.Notes ?? “”;
if (summary.UWRationale.Notes == “” || summary.UWRationale.Notes == null)
{
templateDocument.GetElementById(“uwRationale”).SetAttribute(“style”, “display:none”);
}
//After preparing template, below is the code for pdf generation. This portion is taking more than 10 seconds for conversion.
// Setup Template Options with Serialized json from repository
TemplateContentOptions templateOptions = new TemplateContentOptions(serializedJson, TemplateContent.JSON);
using (var streamProvider = new MemoryStreamProvider())
{
// Initialize HTML Template Merge
using (var mergedTemplate = Converter.ConvertTemplate(templateDocument, new TemplateData(templateOptions), new TemplateLoadOptions()))
{
// Convert HTML to PDF and load output to MemoryStreamProvider
Aspose.Html.Converters.Converter.ConvertHTML(mergedTemplate, new Aspose.Html.Saving.PdfSaveOptions(), streamProvider); //This conversion is taking more than 10 sec.Also more then 30 sec in some cases.
// Get access to the memory stream that contains the result data
var pdfStream = streamProvider.Streams.First();
pdfStream.Seek(0, System.IO.SeekOrigin.Begin);
Document pdfDocument = new Document(pdfStream);
PdfFileInfo pfi = new PdfFileInfo(pdfDocument);
pfi.Title = summary?.Company;
string headerText = summary?.Company + " - " + summary?.Title + " - " + summary?.SubmissionNo;
AddHeaderToDocument(pdfDocument, headerText);
AddPageNumberToDocument(pdfDocument);
pdfDocument.Save(pdfStream);
return pdfStream.ToArray();
}
Please suggest what to do to resolve this issue.
Thank you,
Tushar Sahu