简体   繁体   English

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

[英]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.我目前正在从 HTML 字符串创建 PDF 文档。 During creation my HTML string has inline CSS styles as well as a tag that are typically converted and reflect correctly on the PDF.在创建期间,我的 HTML 字符串具有内联 CSS styles 以及通常在 ZBCD1D10B6861775BA 上正确转换和反映的标签。 For this current instance I'm running into a problem where the CSS style is not being applied to all of the text.对于这个当前实例,我遇到了 CSS 样式未应用于所有文本的问题。 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.在下面的代码块中,您会注意到我有带有“颜色:#e74c3c”的样式属性,并且在 output 文档中,它跳过了 html 字符串中包含的大部分文本。

<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这是 output 的部分屏幕截图:部分文档失败的屏幕截图

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.它们是典型的带有 url 的标签“<a href=”来引导用户。 The display text is the same url with a span to change it's color to red.显示文本与 url 相同,但有一个跨度将其颜色更改为红色。 In the output all of the links are being duplicated in parenthesis.在 output 中,所有链接都在括号中重复。 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.在上面的 HTML 字符串中,括号中没有这样的链接,但 Aspose.PDF 似乎每次都自己添加它。

In order to build the document I am using the following method and in particular document.Save(filePath)为了构建文档,我使用以下方法,特别是 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. 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.您会在 HTML 上注意到我创建的它没有重复的链接,并且所有文本都显示为红色,这是正确的。 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.这是因为我们最近从不支持 @media css 样式规则的 Aspose v.17 升级到现在支持 @media css 样式规则的 Aspose v22.2。 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.在我的 css 中,它有一个 @media 打印样式规则,在使用 document.Save() 时会导致样式规则生效并导致上述所有问题。

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.如果 Aspose 中的任何人读到此内容,则很难找到此问题,因为我必须获取传递到 AsposePdfDocument 的字符串并删除代码块并尝试再次保存文档。 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.如果我们能够深入到 AsposePdfDocument 并具体到每个页面并查看创建该特定页面的内容,包括正在应用并将在 Save 方法期间应用的 CSS 规则,这将非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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