使用iText在PDF上无法正确打印数据 [英] Data not printing correctly on PDF using iText

查看:102
本文介绍了使用iText在PDF上无法正确打印数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的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>

当我尝试使用此代码将其转换为PDF时:

When I try to convert it into a PDF with this code:

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

它给了我这个输出:

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

But when I open the XHTML in a browser, it's like this:

我没有遇到问题。我的主要目标是打破那个特定空间的话语。我使用iText 2.0.8,我不能使用其他API,因为我的公司不允许这样做。任何建议都会有很多帮助。

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. Any suggestion would be a lot of help.

推荐答案

你正在使用已有几年历史的iText版本。为什么不使用当前版本并查看问题是否得到了解决?在此期间,他们已经完成了大量的工作,将HTML和XML转换为PDF(反之亦然)。事实上,我会说,这是他们所做的主要事情。

You're using a version of iText that's several years old. 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. In fact, I'd say, it's been the principal thing they've worked on.

这篇关于使用iText在PDF上无法正确打印数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆