Free Support Forum - aspose.com

Convert Email Message (HTML) to PDF using Aspose.PDF for .NET - Social media buttons cause timeouts

We’re using the Aspose.Pdf.dll (version 9.9.0.0) in a webservice that converts the body data of a email message entity in MS Dynamics CRM to a pdf file somewhere on a sharepoint drive.

In earlier forum topics i already mentioned about having troubles when converting the body of the crm field, when containing images, to a pdf.
Our workaround for that was to extract the embedded images out of the body, using the src tag , storing the images on a temporary place and construct the email body outself, and place the images in that referencing to the temporary place.

But, we have one situation that always results in a timeout on the aspose dll action :
Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(EmailBody)), objLoadOptions);

And that’s when the images are gifs containing scripting, for example social media buttons containing hyperlinks : a twitter gif containing a link to twitter, a facebook gif containing a link to facebook etc.

When a user logs a timeout in the webservice in crm on that aspose action , we try to reproduce it, and than we remove those social buttons from the body, save the email and then the action in aspose succeeds.

Anyone aware of this problem in the aspose dll?

These are the buttons :

signature.png (8.7 KB)

This is the code :

			//**1 extract SRC from description
            Email mail = (Email)base.OrganizationService.Retrieve("email", Guid.Parse(emailId), new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

            //** Build EMAIL object **//
            StringBuilder emailbody = new StringBuilder();
            //** From/To/CC/BCC
            emailbody = CreateHeaderTable(emailbody, mail);
            //** Footer
            //emailbody = CreateFooterTable(emailbody, String.Format("Sent on {0}", mail.CreatedOn.Value));
            //** Body
            emailbody = CreateBodyTable(emailbody, mail.Description);
            string EmailBody = emailbody.ToString();
            
            List<string> Images = new List<string>();
            bool bImgAvailable = false;
            
            string[] temporaryFileToDeleteArray = null;
            int startIndex = EmailBody.IndexOf("<img ");
            if (startIndex >= 0)
            {
                bImgAvailable = true;
                int endIndex = EmailBody.IndexOf(">", startIndex);
                while (startIndex >= 0 && endIndex >= 0)
                {
                    string ImageTag = EmailBody.Substring(startIndex, endIndex - startIndex + 1);
                    Images.Add(ImageTag);
                    startIndex = EmailBody.IndexOf("<img ", endIndex);
                    if (startIndex >= 0)
                        endIndex = EmailBody.IndexOf(">", startIndex);
                }

                string srcUpdated = "";

                temporaryFileToDeleteArray = new string[Images.Count];
                int arrIndex = 0;

                foreach (string Image in Images)
                {
                    int start = 0;
                    int end = 0;
                    if (Image.ToLower().Contains("src="))
                    {
                        if (Image.ToLower().Contains("src='"))
                        {
                            start = Image.IndexOf("src='");
                            end = Image.IndexOf("'", start + 5);
                        }
                        else
                        {
                            start = Image.IndexOf("src=\"");
                            end = Image.IndexOf("\"", start + 5);
                        }
                        //**Extract SRC from IMG
                        string src = Image.Substring(start, end - start + 1);
                        if (src.Contains("AttachmentId="))
                        {
                            var query = HttpUtility.ParseQueryString(src);
                            if (query != null)
                            {
                                //**Get Attchment from IMG
                                var attachmentid = query.Get("AttachmentId");
                                if (attachmentid != null)
                                {
                                    attachmentid = attachmentid.ToLower().Replace("AttachmentId=", "").Replace("'", "").Replace("\"", "");
                                    Guid AttachmentId = new Guid(attachmentid);
                                    ActivityMimeAttachment attach = (ActivityMimeAttachment)
                                        base.OrganizationService.Retrieve("activitymimeattachment", AttachmentId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
                                    if (attach != null)
                                    {
                                        string FileName = attach.FileName.ToString();
                                        //**Generate unique id
                                        string newattachId = String.Format("{0}D{1}T{2}", uniqueName,
                                        DateTime.Now.Date.ToString("yyMMdd"),
                                        DateTime.Now.TimeOfDay.ToString().Replace(":", "").Replace(".", "").Substring(0, 8));
                                        //**Create temporary fysical file for attachment/embedded img
                                        srcUpdated = Path.Combine(GEDpath, newattachId.ToString() + FileName);
                                        temporaryFileToDeleteArray[arrIndex] = Path.Combine(GEDpath, newattachId.ToString() + FileName);
                                        using (FileStream fs = new FileStream(srcUpdated, FileMode.OpenOrCreate))
                                        {
                                            //**Write file to temporary path
                                            byte[] fileContent = DownloadAttachment(Guid.Parse(attachmentid));
                                            fs.Write(fileContent, 0, fileContent.Length);
                                        }
                                        srcUpdated = @"src='" + srcUpdated + "'";
                                        //**replace IMG SCR in BODY by a link to a temporary fysical file (srcUpdated)
                                        EmailBody = EmailBody.Replace(src, srcUpdated);
                                    }
                                }
                            }
                        }
                    }
                    arrIndex += 1;
                }
            }
            
            // Create a PDF license object
            Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
            // Instantiate license file
            pdfLicense.SetLicense("Aspose.Total.lic");
            // Set the value to indicate that license will be embedded in the application
            pdfLicense.Embedded = true;

            if (EmailBody != "")
            {
                //Set the properties for PDF file page format         
                HtmlLoadOptions objLoadOptions = new HtmlLoadOptions();
                objLoadOptions.PageInfo.Margin.Bottom = 10;
                objLoadOptions.PageInfo.Margin.Top = 20;

                //Load HTML string into MemoryStream using Aspose document class
                Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(EmailBody)), objLoadOptions);
                string FileName = Path.Combine(GEDpath, newmailId + ".pdf");
                //Save PDF file on local hard drive or database or as you wish          
                doc.Save(@FileName);
            }

            if (bImgAvailable)
            {
                //**Delete temporary fysical file
                foreach (string temporaryFileToDelete in temporaryFileToDeleteArray)
                {
                    if (temporaryFileToDelete != null)
                        File.Delete(temporaryFileToDelete);
                }
            }

@vanbocpe

Thank you for contacting support.

You are using an outdated version of Aspose.PDF for .NET API so please upgrade to Aspose.PDF for .NET 18.8 because it includes more features and bug fixes. Also, the support is provided based on latest available version. In case the issue persists, please create a narrowed down sample application reproducing this issue because currently there are some undefined methods included in your code snippet. We will then investigate it further to help you out.