Hi team,
We’re trying to generate PDF from HTML template but after conversion tables are not rendered in pdf as showing in html.
We’re 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.
// 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);
// 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();
}
Attached html and pdf file for reference. Please compare all the tables on both the files.
Issue 1 - For one table, Column width is not maintained in converted pdf.
Issue 2 - Other tables are getting cut off in converted pdf.
Please suggest what to do to resolve this issue.
Aspose_Generated_Pdf.zip (402.0 KB)
Aspose_HTML.zip (43.1 KB)
Thank you,
Tushar Sahu