有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中使用Jtidy和Itext将html转换为pdf时未应用样式

我使用Jtidy和java将文档从html转换为pdf 问题是,当我转换pdf时,样式未应用于文档。当我尝试其他解决方案(Jsoup、HTMLworker、xmlWorker)时,文档的格式也不正确

    private static ByteArrayOutputStream html2Xhtml(String html) {
    Tidy tidy = new Tidy();
    tidy.setShowWarnings(false);
    tidy.setXmlTags(false);
    tidy.setInputEncoding("UTF-8");
    tidy.setOutputEncoding("UTF-8");
    tidy.setXHTML(true);
    tidy.setMakeClean(true);
    tidy.setForceOutput(true);
    ByteArrayOutputStream fileFit = new ByteArrayOutputStream();
    tidy.parseDOM(new ByteArrayInputStream(html.getBytes()), fileFit);
    return fileFit;
}

public @ResponseBody byte[] SavePdf(
        @RequestParam("documentContent") final Object documentContent)
        throws  IOException {
    ByteArrayOutputStream file = new ByteArrayOutputStream();
    ITextRenderer renderer = new ITextRenderer();
    String xhtml = html2Xhtml(documentContent.toString()).toString();
    renderer.setDocumentFromString(xhtml);
    renderer.layout();
    try {
        renderer.createPDF(file);
    } catch (com.lowagie.text.DocumentException e) {
        log.info(e.getMessage(), e);
        e.printStackTrace();
    }
    return file.toByteArray();
}

这是html代码的一部分:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org">
<style type="text/css">
/*<![CDATA[*/
@media screen , print {
        html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p,
                blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn,
                em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt,
                var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label,
                legend, table, caption, tbody, tfoot, thead, tr, th, td, article,
                aside, canvas, details, embed, figure, figcaption, footer, header,
                hgroup, menu, nav, output, ruby, section, summary, time, mark, audio,
                video {
                margin: 0;
                padding: 0;
                border: 0;
                font-size: 100%;
                font: inherit;
                vertical-align: baseline;
                position: static;
        }
        article, aside, details, figcaption, figure, footer, header, hgroup, img,
                menu, nav, section {
                display: block;
        }
        body {
                line-height: 1;
        }
        ol, ul {
                list-style-type: circle;
                list-style-position: inside;
        }
        a {
                color: #009ddc;
                text-decoration: none;
        }
        table {
                border-collapse: collapse;
                border-spacing: 0px;
                border-width: 0px;
                width: 100%;
        }
        td[colspan="4"], .allergiesSection td:first-child, .problemsSection td:first-child,
                .activeProblemsSection td:first-child, .medicationsSection td:first-child,
                .immunizationsSection td:first-child, .vitalsSection td:first-child,
                .vitalsSection th:first-child, .proceduresSection td:first-child,
                .careTeamSection td:first-child {
                font-weight: bold;
        }
        .divider {
                border-top: 1px solid #ddd;
                color: #444;
        }
} /*]]>*/
</style>
<title></title>
</head>
<body>
</body>
</html>

共 (0) 个答案