Javascript iText在转换为pdf时未正确呈现html标题标记

Javascript iText在转换为pdf时未正确呈现html标题标记,javascript,html,css,pdf,itext,Javascript,Html,Css,Pdf,Itext,这个新问题与此有关。基本上,我能够将具有图像和CSS样式的html文件转换为PDF。然而,我只是注意到标题标签(h1-h6)没有正确呈现。我意识到在iText网页上有一个这样的例子,但是代码与我目前拥有的不兼容。有人能帮我解决这个问题吗。谢谢 public class HtmlToPdfConverter { public static final String HTML = "C:\\Users\\APPS\\Desktop\\helppages_COPY\\Help_SiteMap

这个新问题与此有关。基本上,我能够将具有图像和CSS样式的html文件转换为PDF。然而,我只是注意到标题标签(h1-h6)没有正确呈现。我意识到在iText网页上有一个这样的例子,但是代码与我目前拥有的不兼容。有人能帮我解决这个问题吗。谢谢

public class HtmlToPdfConverter {

    public static final String HTML = "C:\\Users\\APPS\\Desktop\\helppages_COPY\\Help_SiteMap.htm";
    public static final String DEST = "C:\\Users\\APPS\\Desktop\\output.pdf";

    //public static final String HTML = "C:\\Users\\APPS\\Desktop\\helppages_COPY\\general\\Training1.htm";
    //public static final String DEST = "C:\\Users\\APPS\\Desktop\\Training1.pdf";

    public static final String IMG_PATH = "C:\\Users\\APPS\\Desktop\\helppages_COPY\\images\\";
    public static final String RELATIVE_PATH = "C:\\Users\\APPS\\Desktop\\helppages_COPY\\images\\";
    public static final String CSS_DIR = "C:\\Users\\APPS\\Desktop\\helppages_COPY\\css\\";

    public void createPdf(String file) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();

        // CSS
        CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(false);
        FileRetrieve fileRetrieve = new FileRetrieveImpl(CSS_DIR);
        cssResolver.setFileRetrieve(fileRetrieve);

        // HTML
        HtmlPipelineContext htmlPipelineContext = new HtmlPipelineContext(null);
        htmlPipelineContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        htmlPipelineContext.setImageProvider(new AbstractImageProvider() {

            @Override
            public String getImageRootPath() {
                return IMG_PATH;
            }
        });

        htmlPipelineContext.setLinkProvider(new LinkProvider() {

            @Override
            public String getLinkRoot() {
                return RELATIVE_PATH;
            }
        });

        // Pipelines
        ElementList elements = new ElementList();
        ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
        //PdfWriterPipeline pdfWriterPipeline = new PdfWriterPipeline(document, pdfWriter);
        //HtmlPipeline hhHtmlPipeline = new HtmlPipeline(htmlPipelineContext, pdfWriterPipeline);
        HtmlPipeline hhHtmlPipeline = new HtmlPipeline(htmlPipelineContext, end);
        CssResolverPipeline cssResolverPipeline = new CssResolverPipeline(cssResolver, hhHtmlPipeline);

        // XML Worker
        XMLWorker xmlWorker = new XMLWorker(cssResolverPipeline, true);
        XMLParser xmlParser = new XMLParser(xmlWorker);
        xmlParser.parse(new FileInputStream(HTML));

        document.open();
        for (Element e : elements) {
            document.add(e);
        }
        document.add(Chunk.NEWLINE);


        document.close();

        System.out.println("**** Conversion Complete ****");
    }

    public static void main(String[] args) throws IOException, DocumentException {
        new HtmlToPdfConverter().createPdf(DEST);
    }

}
下面是我的一个html页面的示例。请不要以为这些页面只是静态html页面。它们与在用户桌面上运行的基于JavaFX的帮助页面应用程序一起使用。它允许他们查看页面,然后根据需要将其打印为pdf格式。这些页面由Tomcat服务器提供。页面使用外部css样式表和图像。html文件是用HTML5编写的。html文件中的所有内容都在pdf文件中正确呈现,但标题标记除外。例如,在我包含的页面中,“Tutorial”一词应该是a,但正如您所看到的,它不是。我已经在浏览器中包含了我的页面外观以及我的pdf外观,以便您可以查看我的问题。我希望这解释得足够清楚。我不知道如何使这一点更清楚

HTML文件:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>Alert Notification</title>
    <link rel="stylesheet" type="text/css" href="../css/helppages.css" />
</head>

<body>

    <div class="page-padding">

        <table class="header">
            <tr class="header-footer-tr">
                <td class="header-td-left">
                    <a href="#LINKS">Topic References</a>
                </td>
                <td class="header-td-center">
                    <a id="TOP">Create Alert</a>
                </td>
                <td class="header-td-right">
                    <a href="../Help_SiteMap.htm">Topics Map</a>
                </td>
            </tr>
        </table>

        <p>The Create Alert function provides an Administrator a means to send a &#8220;System&#8221; Alert that immediately displays
            a message window to all users who are currently logged on and to the rest when they do log on until a specified time.
            A system Alert might be used to notify users of changes that effect system access or availability such as scheduled maintenance.
        </p>

        <p>This function is executed as an Admin menu option.</p>

        <div class="image-padding center-content">
            <!-- <img src="../images/Create_Alert.png" alt="Create Alert" /> -->
            <img src="http://via.placeholder.com/300x100" alt="dummy image" />
        </div>

        <p>This window has two text fields, two selection lists, and two buttons.</p>

        <ul>
            <li>Subject - text input to accept a title of the message to all users</li>
            <li>Expire time hour - selection list to specify the hour of the day until which users will see this message at login</li>
            <li>Expire time minute - selection list to specify the minute of the hour until which users will see this message at login
            </li>
            <li>Multi-line text input field to accept message body</li>
            <li>OK - button to accept and broadcast the Alert then close the window</li>
            <li>Close - button to close the window discarding the message</li>
        </ul>

        <br />

        <div class="thin-horizontal-line"></div>

        <h2 id="TUTORIAL">Tutorial</h2>

        <p>
            <b>Send an Alert</b>
        </p>

        <ul>
            <li>Enter an Alert title in the Subject text input field and information for the Alert body (both required fields)</li>
            <li>Optionally, change the expiration time (must be at least 5 minutes in future, 10 is safer)</li>
            <li>Click the OK button</li>
        </ul>

        <br />

        <table class="footer">
            <tr class="header-footer-tr">
                <td style="width: 50%; text-align: center">
                    <a id="LINKS"></a>Local Topics</td>
                <td style="width: 50%; text-align: center">Related Topics</td>
            </tr>
            <tr class="verticle-align-top">
                <td class="local-topics-padding">
                    <p>
                        <a href="#TOP">Top of this Page</a>
                    </p>
                    <p>
                        <a href="#TUTORIAL">Tutorial</a>
                    </p>
                </td>
                <td class="text-align-right related-topics-padding">
                    <p>
                        <a href="../admin/Admin_Main.htm">Admin Functions</a>
                    </p>
                </td>
            </tr>
        </table>
    </div>
</body>

</html>

警报通知
创建警报功能为管理员提供了发送“;系统&8221;立即显示的警报
向当前登录的所有用户以及在指定时间之前登录的其他用户发送消息窗口。
系统警报可用于通知用户影响系统访问或可用性的更改,如计划维护。

此功能作为管理菜单选项执行

此窗口有两个文本字段、两个选择列表和两个按钮

  • 主题-文本输入,用于向所有用户接受消息标题
  • Expire time hour-选择列表,指定用户在登录时看到此消息的时间
  • Expire time minute(过期时间分钟)-选择列表,指定用户在登录时看到此消息之前的每小时分钟数
  • 接受消息正文的多行文本输入字段
  • 确定-按钮接受并广播警报,然后关闭窗口
  • 关闭-按钮关闭窗口,丢弃消息

辅导的 发出警报

  • 在主题文本输入字段中输入警报标题和警报正文信息(两个必填字段)
  • (可选)更改过期时间(以后必须至少5分钟,10分钟更安全)
  • 单击OK按钮

页面应该是什么样子的:

这就是我转换后的pdf格式:


谢谢你,我也非常感谢迄今为止给我的所有帮助。

添加你的代码、css以及打印内容和未打印内容的屏幕截图会很有帮助……我已经添加了我的代码。HTML是什么样子的?您希望从输出中得到什么?你的输出是什么?你为什么不使用最新版本的iText?(见附录)问题不清楚。根据问题当前版本中的信息,无法给出答案。我已经在帖子中添加了更多信息,以及代码和示例。这应该是关于我的html看起来像什么,我的期望输出是什么,以及我的当前输出是什么的问题。至于我为什么不使用iText 7。我为一家小企业工作,该企业正在为政府进行一个小项目。不会支付iText 7许可证的费用,向公众发布整个应用程序代码不是一个选项。添加您的代码、css以及打印内容和未打印内容的屏幕截图会很有帮助……我已经添加了我的代码。HTML看起来像什么?您希望从输出中得到什么?你的输出是什么?你为什么不使用最新版本的iText?(见附录)问题不清楚。根据问题当前版本中的信息,无法给出答案。我已经在帖子中添加了更多信息,以及代码和示例。这应该是关于我的html看起来像什么,我的期望输出是什么,以及我的当前输出是什么的问题。至于我为什么不使用iText 7。我为一家小企业工作,该企业正在为政府进行一个小项目。用户不会支付iText 7许可证的费用,并且不会向公众发布整个应用程序代码。