简体   繁体   中英

Creating a PDF from HTML string is duplicating Links in Parenthesis and inline CSS style is not applying to all text Aspose.PDF

I am currently creating a PDF document from an HTML string. During creation my HTML string has inline CSS styles as well as a tag that are typically converted and reflect correctly on the PDF. For this current instance I'm running into a problem where the CSS style is not being applied to all of the text. In the following block of code you will notice I have style attributes with "color:#e74c3c" and on the output document it is skipping a large portion of the text that is covered in the html string.

<td colspan='2'>
         <span style=\"font-family:Arial,Helvetica,sans-serif;\">
              <span style=\"font-size:12pt;\">
                   <span style=\"color:#e74c3c;\">
                        <b>
                             <u style=\"text-underline:#e74c3c\">
                                  <span lang=\"EN\">
                                       <span style=\"line-height:107%\">
                                            NOTICE REGARDING PUBLIC OBSERVATION AND PARTICIPATION IN THE CITY COUNCIL MEETING
                                       </span>
                                  </span>
                             </u>
                        </b>
                   </span>
              </span>
         </span>
         \n
        <div>
             <span style=\"font-family:Arial,Helvetica,sans-serif;\">
                  <span style=\"font-size:12pt;\">
                       <span style=\"color:#e74c3c;\">
                            <span style=\"line-height:107%\">
                                 To comply with Governor Newsom&rsquo;s Executive Order N-29-20 and the Amended Order and Guidance of the Orange County Health Officer issued March 18, 2020 the City of Seal Beach hereby gives notice of the &ldquo;means by which members of the public may observe the meeting and offer public comment&rdquo; for the City Council meeting on&nbsp; July 27, 2020.&nbsp; &nbsp;Due to the need for social distancing and the prohibition on public gatherings set forth in the County Health Officer&rsquo;s Order, all participation in the above-referenced&nbsp;Meeting will be by teleconference for the Members of the City Council and staff.&nbsp; Because of the unique nature of the emergency there will NOT be a physical meeting location and all public participation will be&nbsp;electronic.&nbsp;<br />\n<br />\nPUBLIC COMMENT AND PUBLIC HEARING:&nbsp; Members of the public may submit comments on any item ON this City Council meeting agenda&nbsp;via email&nbsp;to the City Clerk at gharper@sealbeachca.gov
                            </span>&nbsp;
                       </span>
                       
                       <span style="line-height:107%">
                            <span style="color:#e74c3c;">
                                 or via the comment icon through the online portal at&nbsp;
                            </span>
                                                
                            <a href=\"https://www.sealbeachca.gov/Government/Agendas-Notices-Meeting-Videos/Council-Commission-Meetings\" style="color:blue; text-decoration:underline">
                                 <span style="color:#e74c3c;">
                                      https://www.sealbeachca.gov/Government/Agendas-Notices-Meeting-Videos/Council-Commission-Meetings
                                 </span>
                            </a>
                            <span style=\"color:#e74c3c;\">
                                 &nbsp;.&nbsp; For those who would like to call in to make a comment regarding the Public Hearing item please sign up through the City website at&nbsp;
                            </span>
                            
                            <a href=\"https://www.sealbeachca.gov/Government/Agendas-Notices-Meeting-Videos/Council-Commission-Meetings\" style=\"color:blue; text-decoration:underline\">
                                 <span style=\"color:#e74c3c;\">
                                      https://www.sealbeachca.gov/Government/Agendas-Notices-Meeting-Videos/Council-Commission-Meetings
                                 </span>
                            </a>
                            <span style=\"color:#e74c3c;\">
                                 &nbsp;using the microphone icon.&nbsp;&nbsp;All email comments the City Clerk receives before the start of the meeting will be posted on the City website and distributed to City Council.&nbsp;Email comments received after that time will be posted on the City&rsquo;s website and forwarded to the City Council after the meeting.&nbsp;
                            </span>
                       </span>
                  </span>
             </span>
        </div>
        \n
        <br />
        \n
        <br />
        \n
        <span style=\"font-family:Arial,Helvetica,sans-serif;\">
             <span style=\"font-size:12pt;\">
                  <span style=\"line-height:107%\">
                       <span style=\"color:#e74c3c;\">
                            THIS NOTICE AND ELECTRONIC PARTICIPATION PROVISIONS SET FORTH IN THIS NOTICE ARE PROVIDED PURSUANT TO SECTION 3 OF EXECUTIVE ORDER N-29-20.
                       </span>
                  </span>
             </span>
        </span>
        <br />\n&nbsp;
    </td>

Here is a partial screen shot of the output: Screen Shot of portion of document failing

The second issue I'm having is that you will notice in the above code that I'm including a couple anchor tags. They are typical a tags "<a href=" with a url to direct the user to. The display text is the same url with a span to change it's color to red. In the output all of the links are being duplicated in parenthesis. In the HTML string above there is no such links in parenthesis and yet Aspose.PDF appears to be adding it each time on it's own.

In order to build the document I am using the following method and in particular document.Save(filePath)

private async Task SaveFile(CompiledMeetingDocumentFile file, string filename, int meetingId, bool isPdfConvert, IDocumentHelper document = null)
    {
        string filePath = string.Empty;
        try
        {
            // Creates a temp folder on the hosting environment if it doesn't already exist.
            var savePath = System.Web.Hosting.HostingEnvironment.MapPath($"~/temp/{meetingId}/");
            if (!Directory.Exists(savePath))
            {
                if (string.IsNullOrEmpty(savePath))
                {
                    throw new Exception("Could not create a valid file path in the current Hosting Environment.");
                }
                Directory.CreateDirectory(savePath);
            }
            filePath = Path.Combine(savePath, filename);

            string fileExt = file.CompileOutputType == CompileOutputTypes.Docx ? ".docx" : ".pdf";

            if (isPdfConvert)
            {
                Aspose.Words.Document PdfDocument = new Document(filePath + ".docx");
                ELSLogHelper.InsertInfoLog(_callContext, ELSLogHelper.AsposeLogMessage("Open"), MethodBase.GetCurrentMethod()?.Name, MethodBase.GetCurrentMethod().DeclaringType?.Name, Environment.StackTrace);
                filePath += fileExt;
                PdfDocument.Save(filePath, SaveFormat.Pdf);
                ELSLogHelper.InsertInfoLog(_callContext, ELSLogHelper.AsposeLogMessage("Save"), MethodBase.GetCurrentMethod()?.Name, MethodBase.GetCurrentMethod().DeclaringType?.Name, Environment.StackTrace);
            }
            else
            {
                filePath += fileExt;
                document.Save(filePath);
            }

            string permFilePath = string.Format("Meetings/{0}/{1}{2}", meetingId, Path.GetFileNameWithoutExtension(filePath), fileExt);
            await _azureProvider.SaveAzureFileAsync(permFilePath, File.ReadAllBytes(filePath));

            file.FilePath = permFilePath;
            file.CompileFinishedDate = DateTime.UtcNow;
        }
        catch (Exception exception)
        {
            LogManager.Error($"Failed to save compiled file: meetingId: {meetingId} fileName: {filename}", exception);
            throw;
        }
        finally
        {
            if (File.Exists(filePath))
                File.Delete(filePath);
        }
    }

Here is a link to get to my HTML string, the created pdf output, and an HTML file I create before it actually becomes a PDF. You will notice on the HTML that I create it does not have the duplicate links and all of the text comes out red which is correct. Documents

I found the issue to my problem above. It was being caused because we recently upgraded from Aspose v.17 which did not support @media css style rules to Aspose v22.2 which now does support @media css style rules. In my css it had an @media print style rule which when using document.Save() was causing the style rule to take effect and causing all of the issues above.

If anyone from Aspose reads this it was very difficult to find this issue because I had to grab the string that was being passed into the AsposePdfDocument and remove a block of code and attempt to save the document again. It would be incredibly helpful if we were able to drill down into the AsposePdfDocument and specifically into each page and see the content that was creating that specific page including the CSS rules being applied and will be applied during the Save method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM