简体   繁体   English

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

[英]Data not printing correctly on PDF using iText

Here's my XHTML: 这是我的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>

When I try to convert it into a PDF with this code: 当我尝试使用以下代码将其转换为PDF时:

    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();
        }
    }

It's giving me this output: 它给了我这个输出:

在此处输入图片说明

But when I open the XHTML in a browser, it's like this: 但是,当我在浏览器中打开XHTML时,就像这样:

在此处输入图片说明

I am not getting the problem. 我没有问题。 My main aim is to break the words in that given space. 我的主要目的是在给定的空间内打破常规。 I use iText 2.0.8 and I can't use other APIs as my company does not allow that. 我使用的是iText 2.0.8,我不能使用其他API,因为我的公司不允许这样做。 Any suggestion would be a lot of help. 任何建议都会有很大帮助。

You're using Flying Saucer (org.xhtmlrenderer.pdf.ITextRenderer), a library that converts XHTML+CSS to PDF. 您正在使用Flying Saucer(org.xhtmlrenderer.pdf.ITextRenderer),该库将XHTML + CSS转换为PDF。 It uses iText for the PDF generation. 它使用iText生成PDF。

If your problem is in the CSS support you should be looking at Flying Saucer. 如果您的问题出在CSS支持上,那么您应该考虑使用飞碟。 The XHTML and CSS rendering is done before iText is used to convert the rendered content to PDF. 在使用iText将呈现的内容转换为PDF之前,已完成XHTML和CSS呈现。

You're using a version of iText that's several years old. 您使用的iText版本已有几年历史了。 Why don't you use a current version and see whether the problem has been taken care of? 为什么不使用当前版本,看看问题是否得到解决? They've done a huge amount of work converting HTML and XML to PDF (and vice versa) during this time. 在这段时间内,他们做了很多工作,将HTML和XML转换为PDF(反之亦然)。 In fact, I'd say, it's been the principal thing they've worked on. 实际上,我会说,这是他们所做的主要工作。

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

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