0

environment is : flyingsaucer r8 and itext 2.0.8

I'm going to create pdf file with flyingsaucer and itext, I added

table-layout:fixed;word-wrap:break-word;

to wrap the cell content. However, the generated pdf file looks like next : enter image description here

from the above diagram, we can see that over long English sentence of column 'Description' can be wrapped correctly, but the over long number in 'Account Code' and 'Description' cannot be wrapped.

I also tried "word-break: break-all;", but it still doesn't work.

my xhtml file is :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
<style type="text/css">
@page {-fs-flow-top: 'header';-fs-flow-bottom: 'footer';size:210mm 297mm;margin-top:40pt;@top-center {content: element(header)}
@bottom-center {content: element(footer)}page-break-before:always}#pagenumber:before { content: counter(page); }
#pagecount:before { content: counter(pages); }.pageNext{page-break-before: always;}
#header {position: running(header);font-style: italic;font-family: Arial Unicode MS;
-fs-move-to-flow: 'header';width: 100px; padding-top:10pt;}#footer {position: running(footer);font-style: italic;
font-family: Arial Unicode MS;-fs-move-to-flow: 'footer';color: #6c6ce8;}
body{font-size:13px;font-family:Arial Unicode MS;white-space:inherit;}b{font-size:13px;font-weight:bold;font-family:Arial;}
#dt th{text-align: center;font-family:Arial;font-weight:bold;}#title {font-size:15px;font-weight:bold;}#lab {font-size:15px;font-weight:bold;}
table.data{ border-top: 1px solid #333;border-bottom: 1px solid #333;width:100%;border:1px solid #333;}table{ border-collapse:collapse;}
table td{ padding:0 0 0 0; vertical-align:top;white-space:inherit;}
table.uReportStandard > thead > tr > th{ border:0.5pt #333 solid; background:#d5d0c3;color:#000;text-align:center;font-size:15px;
font-family:Arial,sans-serif;font-weight:bold;}
table.uReportStandard > tbody > tr > td{ padding:1px 1px; font-size:13px;}.data td.left_text{ font-size:13px;
font-family:Arial Unicode MS,sans-serif;width:300px;}.data td.right_text{ text-align:right;font-size:13px;
font-family:Arial Unicode MS,sans-serif;width:120px;}table#uPageCols td#uRightCol,table#uPageCols td#uRightCol aside{width:0;}
table.uReportStandard{border:0.5px #333 solid;}
</style>
    <meta http-equiv='content-type' content='text/html; charset=UTF-8' />

    <title></title>
  </head>

  <body>
    <div id='footer' style='text-align:center;margin-top:0;'>
      Page <span id='pagenumber' /> of <span id='pagecount' /><span style='margin-left:150px;'>2016-05-23 16:03:07</span>
    </div>

    <div>

      <table border='0' id='dt' style='width:100%;table-layout:fixed;word-wrap:break-word;'>
        <thead>
          <tr style='background-color: gainsboro;border:solid 1px #333;'>
            <th style='border:solid 1px #333;width:10%;'>Account Code</th>

            <th style='border:solid 1px #333;width:29%;'>Bank Name</th>

            <th style='border:solid 1px #333;width:35%;'>Description</th>

            <th style='border:solid 1px #333;width:13%;'>Load(CNY)</th>

            <th style='border:solid 1px #333;width:13%;'>Borrow(CNY)</th>
          </tr>
        </thead>        

        <tr>
          <td style='border:solid 1px #333;'>66020901039</td>

          <td style='border:solid 1px #333;'>ABC DEF<br />
          -Global Logistics LTD</td>

          <td style='border:solid 1px #333;white-space:inherit;'>break-all Behaves the same as normal for Asian text, yet allows the line to break arbitrarily for non-Asian text. This value is suited to Asian text that contains some excerpts of non-Asian text. Debit: EUR 50.00</td>

          <td style='text-align: right;border:solid 1px #333;'>367.47</td>

          <td style='text-align: right;border:solid 1px #333;'>
          </td>
        </tr>        

        <tr>
          <td style='border:solid 1px #333;'>220201</td>

          <td style='border:solid 1px #333;'>ACCOUNT PAYABLE<br />
          -Global Logistics LTD</td>

          <td style='border:solid 1px #333;white-space:inherit;'>88888888888888888889999999999999999997777777777777 Credit: EUR 284.36</td>

          <td style='text-align: right;border:solid 1px #333;'>
          </td>

          <td style='text-align: right;border:solid 1px #333;'>2,089.85</td>
        </tr>

      </table>
    </div>
  </body>
</html>

my code is :

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(new File("test.xhtml"));;
renderer.layout();
FileOutputStream fos = new FileOutputStream("a.pdf");
renderer.createPDF(fos);
fos.close();

my questions are :

  1. how table-layout:fixed;word-wrap:break-word; wrap content? it base on what to wrap?
  2. why English/Chinese centense can be wrapped correctly, while number cannot?
  3. how to wrap the number in my case?

Thanks in advance!

after using itext 5.5.9, number can be wrapped correctly, but now css : -fs-table-paginate: paginate; #pagenumber:before { content: counter(page); }#pagecount:before { content: counter(pages); } doesn't work in itext 5.5.9

frank
  • 1,169
  • 18
  • 43
  • iText `2.0.8` is from January 2008. More than 8 years ago. iText `5.5.9` was released 2 months ago, iText `7.0.0` was released earlier this month. Could you please try again with a more recent version of iText and see if that solves your problem? – Amedee Van Gasse May 23 '16 at 14:10
  • This seems to be a flying saucer problem, so updating iText will probably not help (you can maybe try itext 2.1.7 at maximum anyway). Wrapping probably won't work because when wrapping text there are blanks which can be wrapped. A number doesn't have blanks so it won't be wrapped. Try out a long text without any spaces and see whether this will be wrapped. For numbers try something like 1.000.000 if you can – Lonzak May 24 '16 at 07:54
  • @AmedeeVanGasse, thanks for your tip, I tried iText 5.5.9, it can wrap the number. However, css : -fs-table-paginate: paginate and #pagenumber:before { content: counter(page); } #pagecount:before { content: counter(pages); } doesn't work in 5.5.9, while they works in 2.0.7 – frank May 24 '16 at 14:06
  • @Lonzak as you said, a number doesn't have blank so it won't be wrapped. – frank May 24 '16 at 14:39

2 Answers2

1

as @Lonzak said, I just replaced core-renderer-r8.jar with flying-saucer-core.9.0.9.jar and flying-saucer-pdf.9.0.9.jar, use itext 2.1.7, then it works with css :

table-layout:fixed;word-wrap:break-word

, the long non-blank string or number can be wrapped correctly.

frank
  • 1,169
  • 18
  • 43
0

I was breaking my mind with that and i would recommend you to follow this solution. If you are using thymeleaf try to use the .abbreviate method :

${#strings.abbreviate( ${exampleOfText}, 10)}

source

Skatt
  • 372
  • 3
  • 10