Data not printing correctly on PDF using iText

2019-06-11 08:45发布

问题:

Here's my 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:

    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:

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.

回答1:

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.



回答2:

You're using Flying Saucer (org.xhtmlrenderer.pdf.ITextRenderer), a library that converts XHTML+CSS to PDF. It uses iText for the PDF generation.

If your problem is in the CSS support you should be looking at Flying Saucer. The XHTML and CSS rendering is done before iText is used to convert the rendered content to PDF.