Hi team,
We’ve an Aspose total license. We’re trying to generate PDF from HTML template string but Aspose document pdf library is taking lot of time to generate PDF out of it. We’re getting a lot of negative feedbacks from our clients because of pdf generation slowness.
We’re using Aspose pdf version 24.1.0
For converting sample html string to pdf, the library is taking almost 40 to 50 seconds for 20 pages pdf with and without images as well.
We’re using Aspose.Pdf.Document(Stream input, LoadOptions options) method for converting html string to pdf.
We’re using below logic for Html to pdf conversion -
string htmlStr = “Sample html string”;
HtmlLoadOptions options1 = new()
{
// set Print or Screen mode
HtmlMediaType = HtmlMediaType.Print,
IsEmbedFonts = true,
//PageInfo = { Width = 794, Height = 1191, IsLandscape = true }
PageInfo = {
Width = Aspose.Pdf.PageSize.A4.Width,
Height = Aspose.Pdf.PageSize.A4.Height,
Margin = { Left = 10, Right = 10 }
}
};
byte[] byteArray = Encoding.UTF8.GetBytes(htmlStr);
MemoryStream htmlStrStream = new(byteArray);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(htmlStrStream, options1);
await htmlStrStream.DisposeAsync();
pdfDocument.SetTitle(obj.CedingCompany);
string headerText = obj.Company + " - " + obj.Title + " - " + obj.No;
AddHeaderToDocument(pdfDocument, headerText);
AddPageNumberToDocument(pdfDocument);
pdfDocument.FreeMemory();
OptimizationOptions optimizeOptions = new();
////Set CompressImages option
optimizeOptions.ImageCompressionOptions.CompressImages = true;
optimizeOptions.ImageCompressionOptions.ImageQuality = 80;
optimizeOptions.ImageCompressionOptions.Version = ImageCompressionVersion.Fast;
////Keeping below Encoding option commented as it can reduce pdf size to great extent but increases processing time. So keeping it commented for now.
////optimizeOptions.ImageCompressionOptions.Encoding = ImageEncoding.Flate
optimizeOptions.UnembedFonts = true;
optimizeOptions.CompressObjects = true;
optimizeOptions.RemoveUnusedObjects = true;
optimizeOptions.RemoveUnusedStreams = true;
optimizeOptions.LinkDuplcateStreams = true;
optimizeOptions.AllowReusePageContent = true;
////Optimize PDF document using OptimizationOptions
pdfDocument.OptimizeResources(optimizeOptions);
MemoryStream ms = new();
pdfDocument.Save(ms);
pdfDocument.Dispose(); //Disposing Aspose PDF object instance to free memory
byte[]? pdfByteArr = ms.ToArray();
await ms.DisposeAsync();
DateTime endTime2 = DateTime.Now;
TimeSpan elapsedTime2 = endTime2 - startTime2;
_logger.LogInformation("ReferralIntegrationController:UpdateAssociatedReferrals:GeneratePdfDocument:Function - actualPdfCreation ended at : {endTime} Elapsed Time : {elapsedTime} seconds for summaryId: {id}", endTime2, elapsedTime2.Seconds, id);
We’re providing the sample HTML template we’ve used to generate PDF with and without images.
Aspose_Support_Ticket_Sample_Data.zip (3.7 MB)
We are getting a lot of performance issues due to pdf generation slowness. This consistent slowness has caused negative impact on our client business. So we are very worried about it.
Team can you please help us to get it fixed as soon as possible. Please provide an ETA as earliest.