11

I see there are lot many questions related to the same scenario this one is little different couldn't figure out the solution. I have a in a tabl in a cell. When I give the continous text like "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" Its overflowing the text when I download the print file and its normal when I give normal text with breaks.Here is my code

.generaltable {
 background-color : #5C7FBF;
 border:thin;
 width : 100%;
 font-family: Arial, Helvetica, sans-serif;
 font-size: 14px;
 }
  
  
 .column {
 background-color : #DEEDFF;
 font-weight : bold;
 padding-bottom : 1px;
 padding-left : 1px;
 padding-right : 1px;
 padding-top : 1px;
 text-align : center;
 font-family: Arial, Helvetica, sans-serif;
 font-size: 14px;
 border: none;
 }
  
 .edit {
 background-color : #DEEDFF;
 border-width: 1px;
 border-style:solid;
 border-color:#DEEDFF;
 border: 1px solid #DEEDFF;
 color: black;
 text-align : left;
 font-weight : bold;
 font-family: Arial, Helvetica, sans-serif;
 font-size: 14px;
 word-break: break-all;
 }
  
  .iedit2 {
 background-color : white;
 text-align: left;
 color: black;
 font-family: Arial, Helvetica, sans-serif;
 font-weight: bold;
 border-top: 1px solid #999999;
 border-right: 1px solid #333333;
 border-bottom: 1px solid #333333;
 border-left: 1px solid #999999;
 word-break: break-all;
 }
  
  
  
  
  
<table border="1" width="100%" align="center" cellpadding="2" cellspacing="1"  class="generaltable">  <tbody><tr id="Row35494#0">
    <th id="04" class="column" width="39%"><a href="javascript:alert('Self Explanatory')">Brief                 description of the issue *</a></th>
        <td id="1 04" width="39%" class="edit">
            <textarea id="269494_0" class="iedit2" cols="35" rows="5" wrap="virtual" maxlength="4000"                   name="fiy">ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
             </textarea>
        </td>
      </tr>
  </tbody>
 </table>

please find the pictures it for clear overview This is the entry

I have a click like Go once I do it it downloads the entire document here using JAVA code and the code part is working Good but the issue is with itext 7.1.7. this is happening and I have changed it to flying saucer its working good but causing other issues. I want to stay with Itext 7.1.7 and solve this continuous fix.

This is the pdf

Juke
  • 1,306
  • 2
  • 11
  • 27

4 Answers4

4

pdfHTML allows you to either convert the form-related elements (inputs, text areas) directly into the plain PDF content, or create a PDF with AcroForm (so that those elements are editable, as they are supposed to be in HTML).

To enable that behavior, you should use setCreateAcroForm(true) in ConverterProperties that you pass to HtmlConverter.

If you don't want to have those fields editable, you can flatten those fields as a second step after you have converted the HTML into PDF.

Having that said, the behavior you describe looks like a bug in iText. But the mode of creating the AcroForm and flattening is implemented in a slightly different way and it looks like the textarea would be converted as expected in your case. You haven't attached the whole example so it's hard to verify for sure, but for the small snippet you attached everything is fine. Here is the code you can use:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
HtmlConverter.convertToPdf(new FileInputStream("C:\\file.html"), baos,
        new ConverterProperties().setCreateAcroForm(true));

PdfDocument document = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())), 
        new PdfWriter(new File("C:\\out.pdf")));
PdfAcroForm acroForm = PdfAcroForm.getAcroForm(document, false);
acroForm.flattenFields();
document.close();
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
  • I am using xhtmlrenderer and itext7-core as dependencies not pdfHTML do I need to change the dependency in this case? – Juke Sep 09 '19 at 13:58
  • @Juke You haven't posted your code but you say you want to use iText. The code I provided is for converting HTML to PDF with pdfHTML add-on to iText Core. In order to use this add-on, you need to add `html2pdf` dependency to your code. – Alexey Subach Sep 09 '19 at 16:46
  • Yes I have changes it to html2pdf and I have seen yu were using pdfDocument and pdfWriter is it needed because I am using dataHandler , Can I use dataHandler? – Juke Sep 09 '19 at 18:10
  • @Juke don't know about DataHandler, this is not an iText class, I'm suggesting an iText7 solution as you mention in the question you want to use iText7 – Alexey Subach Sep 10 '19 at 06:15
  • Datahandler is still working, but I have a separate CSS class which I am referring in my html. But this dependency is not affecting any css reflection in my html do we need to set it in properties? – Juke Sep 10 '19 at 14:23
  • @Juke you need to pass normal HTML file to `HtmlConverter` that opens up in a browser correctly. Your HTML has to contain either inline CSS in ` – Alexey Subach Sep 10 '19 at 18:26
  • Yes I have a valid HTML in webpage css istaking effect but not in pdf when I add in style tag its good. but I want it to refer as external css is tehre any way? WHy pdf is not reading – Juke Sep 10 '19 at 20:25
  • @Juke I don't know the answer to that question without your code. I've tried to convert the example you provided from HTML to PDF and that worked fine with the tweak I provided – Alexey Subach Sep 10 '19 at 20:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199260/discussion-between-juke-and-alexey-subach). – Juke Sep 10 '19 at 20:31
2

You can detect whenever the text reaches a new line in the input box, and insert '\n' to force line breaks so that when you download an image, it should have the line break hard-coded. Hope this helps!

Beckett O'Brien
  • 176
  • 2
  • 16
  • I have tried it using codefor(int limit = 149; limit < fm.cellValue.length() ; limit = limit+149) { if(fm.cellValue.length() > limit) { buffer.append(fm.cellValue.substring(0, 149)); buffer.append(" "); } } – Juke Jun 03 '19 at 18:53
  • but its l;eaving blank at the end – Juke Jun 03 '19 at 18:53
  • What do you mean? – Beckett O'Brien Jun 03 '19 at 19:01
  • the scenario here some of the text areas doesn't come to new line and I have used 149 because that text area fits 149 and than comes to the next line and I am using 13and10 fopr white spacing – Juke Jun 03 '19 at 19:05
  • Try using a for loop to iterate over the characters in the string, and every 149 characters, insert a '\n' into the string. – Beckett O'Brien Jun 03 '19 at 19:13
  • @Juke This may help: [https://stackoverflow.com/questions/537174/putting-char-into-a-java-string-for-each-n-characters] (https://stackoverflow.com/questions/537174/putting-char-into-a-java-string-for-each-n-characters) – Beckett O'Brien Jun 03 '19 at 19:24
0

I guess adding the following css to your content element (i.e .edit, .iedit2) should sort the issue.

overflow-wrap: break-word;
word-wrap: break-word;
word-break: break-word;

Let me know if that works :)

Also for a bit better looking code, you can add the following ;)

-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;

These will add "-" to break word :)

Prajyot Tote
  • 670
  • 5
  • 12
0

Adding white-space: pre-wrap; fixes this issue for me.

K.Mat
  • 1,341
  • 11
  • 17
daynok
  • 1
  • 3