Java 无法使用iText在PDF上正确打印数据

Java 无法使用iText在PDF上正确打印数据,java,pdf,xhtml,itext,Java,Pdf,Xhtml,Itext,这是我的XHTML: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <META content="text/html; charset=UTF-8" http-equiv="Content-Type" /> <head> <meta http-e

这是我的XHTML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<META content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table cellspacing="0" cellpadding="0" align="Center"
    style="table-layout: fixed; word-wrap: break-word; width: 97%"
    bordercolor="4f81BD" border="1">
    <tbody>
        <tr style="color: white;">
            <td height="31" bgcolor="#4f81BD"
                style="border-left: none; border-bottom: none; border-Right: none; border-top: none; border-width: 1px"
                align="Center"><span style="font-weight: Bold">Type</span></td>
            <td bgcolor="#4f81BD"
                style="border-left: none; border-bottom: none; border-Right: none; border-top: none; border-width: 1px"
                align="Center"><span style="font-weight: Bold">Custodian
            Name</span></td>
            <td bgcolor="#4f81BD"
                style="border-left: none; border-bottom: none; border-Right: none; border-top: none; border-width: 1px"
                align="Center"><span style="font-weight: Bold">Relationship
            to Owner</span></td>
            <td bgcolor="#4f81BD"
                style="border-left: none; border-bottom: none; border-Right: none; border-top: none; border-width: 1px"
                align="Center"><span style="font-weight: Bold">Percent</span></td>
            <td bgcolor="#4f81BD"
                style="border-left: none; border-bottom: none; border-Right: none; border-top: none; border-width: 1px"
                align="Center"><span style="font-weight: Bold">Minor
            Name</span></td>
            <td bgcolor="#4f81BD"
                style="border-left: none; border-bottom: none; border-Right: none; border-top: none; border-width: 1px"
                align="Center"><span style="font-weight: Bold">For the
            State of</span></td>
        </tr>
        <tr style="font-family: SansSerif;" align="center">
            <td
                style="border-left: none; border-bottom: solid; border-top: none; border-Right: none; border-width: 1px;"
                align="Center"><span style="font-weight: Bold">
            UTMA/UGMA</span></td>
            <td
                style="border-left: none; border-bottom: solid; border-top: none; border-Right: none; border-width: 1px;"
                align="Center"><span>QQQQQQQQQQQQQQQQQQQQQQQQQ
            MMMMMMMMMMMMMMMMMMMMMMMMMM</span></td>
            <td
                style="border-left: none; border-bottom: solid; border-top: none; border-Right: none; border-width: 1px;"
                align="Center"><span>Common Law Husband</span></td>
            <td
                style="border-left: none; border-bottom: solid; border-top: none; border-Right: none; border-width: 1px;"
                align="Center"><span>15%</span></td>
            <td
                style="border-left: solid; border-bottom: solid; border-top: none; border-Right: solid; border-width: 1px;"
                align="Center">RRRRRRRRRRRRRRRRRRRRRRRR BBBBBBBBBBBBBBBBBBBBBB</td>
            <td
                style="border-left: none; border-bottom: solid; border-top: none; border-Right: none; border-width: 1px;"
                align="Center"><span>DC</span></td>
        </tr>
    </tbody>
</table>
</body>
</html>
它给了我这个输出:

但当我在浏览器中打开XHTML时,它是这样的:


我不明白这个问题。我的主要目的是在给定的空间中打断单词。我使用iText2.0.8,不能使用其他API,因为我的公司不允许这样做。任何建议都会有很大帮助。

您使用的是一个已有几年历史的iText版本。为什么不使用当前版本,看看问题是否得到了解决?在这段时间里,他们做了大量的工作,将HTML和XML转换为PDF(反之亦然)。事实上,我得说,这是他们一直在做的主要事情

您使用的是飞碟(org.xhtmlrenderer.pdf.ITextRenderer),一个将XHTML+CSS转换为pdf的库。它使用iText生成PDF


如果你的问题是CSS支持,你应该看看飞碟。XHTML和CSS呈现是在使用iText将呈现内容转换为PDF之前完成的。

这里猜测:可能iText不理解CSS属性
表格布局
word wrap
?我会试着先把它们去掉。此外,请尝试删除您的
样式并将其移动到样式表中。此外,如果您想要快速解决方案,您可以使用
wkhtmltopdf
启动并运行某些内容,然后在启用后返回到iText解决方案。它可以识别表格布局,而不是文字换行。如果我不使用word wrap,我怎么能打断这些单词?我不能使用iText以外的任何东西。如果没有CSS属性,这些单词不会自动换行吗?试试看!此外,如果您解释为什么不能使用iText以外的任何东西,那么该上下文可能对您的读者有用。服务器限制?公司指令?等等,我同意了。但是,如果我更改版本,会有什么潜在影响?我的整个项目都是基于2.0的。8@sans481:新版本解决了您的问题吗?我正在使用2.1.7,并且面临着换行符问题。任何指针都会非常有用helpful@HardikMishra:没有,我没有修好。但通过论坛,我知道这是由于旧版本的iText。
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import org.xhtmlrenderer.pdf.ITextRenderer;
    import com.lowagie.text.DocumentException;

    public class Practice {
        public static void main(String[] args) throws 
IOException, DocumentException {
            String inputFile = "sample.xhtml";
            String url = new File(inputFile).toURI().toURL().toString();
            String outputFile = "firstdoc.pdf";
            OutputStream os = new FileOutputStream(outputFile);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(url);
            renderer.layout();
            renderer.createPDF(os);

            os.close();
        }
    }