Hi,
We recently purchased Aspose.Total.
I am trying to convert HTML to PDF (sample html attached, in real scenario it will contain two more images.
To create such html I using HTML agility Pack and based on certain rules I apply different images. That piece of code takes only 30 secs for creating 1000 html like the one attached. My requirement is to have five such divs on one page of the pdf.
I am using following code to convert and ultimately spit the file out to the user:
public async Task<byte[]> CreatePdfFileFromHtmlString(List<string> validHtmlStrings, PageInfo pageInfo = null,int? numberOfStringsPerPage = null, HorizontalAlignment stringAlignment =HorizontalAlignment.Left, bool applyBreakAfterString = false)
{
Aspose.Pdf.License license = new License();
license.SetLicense("Aspose.Total.lic");
var memory = new MemoryStream();
var doc = new Document();
doc.Pages.Add();
if (pageInfo != null)
{
doc.PageInfo = pageInfo;
}
var stringPerPageCount = 1;
var pageIndex = 1; // pageindex in Aspose is 1-n based.
for (int i = 0; i < validHtmlStrings.Count; i++)
{
//Start counting how many strings went on the page:
HtmlFragment htmlFragment = new HtmlFragment(validHtmlStrings[i]);
if (applyBreakAfterString)
{
htmlFragment.Margin.Bottom = 2;
}
doc.Pages[pageIndex].Paragraphs.Add(htmlFragment);
doc.Pages[pageIndex].Paragraphs[stringPerPageCount -1].HorizontalAlignment = stringAlignment;
//Reset the counter, increase the page index and start the new page if count reached:
if (stringPerPageCount == numberOfStringsPerPage)
{
pageIndex++;
stringPerPageCount = 1;
doc.Pages.Add();
continue;
}
stringPerPageCount++;
}
doc.Save(memory,SaveFormat.Pdf);
return memory.ToArray();
}
In this function when it comes to doc.Save part, it takes a hell lot of time there.
I read some of the similar issues here on the forum and the suggestions like using word which might be faster. I don’t have that option so I am looking for some guidance as to how I can improve performance of this function.
- Will it help if I keep a blank pdf handy and then merge the pages into that? will that be faster?
- Is there a faster way to do this with Aspose.Html may be or any other since we have Aspose.Total?
I am using 19.3 version.
Please guideBlackDigits.zip (32.2 KB)