-1

I have a variable long string. I want to put the text in a pdf. In this pdf One line can fit 72 characters. Sometimes a line is not quite full because a line break was made. How do I find out how many lines the complete text has? But i have to take care, when the word at the end of the line is too long it silps in the next line. So i cannot just split the text by \n or split through 72 chars.

I load the text from a database and then have it as a string.

if i make a console.log() from the String it looks so (that you see what my starting material is)

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam orem ipsum dolor sit amet

orgfdem ipdsum doasdlor sdft amet

orem ipsum doewlor sit aaaadfmet

oradfem iwwpsum doasdlor siaet amet

ordfdfem ipsdsum dosdlor sddit amet

  • Did you try `myString.split('\n');`? – damian Oct 11 '19 at 15:02
  • yes but then I have the individual lines only in an array. If I then divide each by 72 I get for example. 1.9 out. But now if a word is so long that it does not fit in the first line and slips it in the second line are actually required 3 lines but rounded up so I've calculated only 2 lines. – Firma Buchfink Oct 11 '19 at 15:12
  • 1
    @ suspectus @Kaiido this is not duplicate! – Firma Buchfink Oct 11 '19 at 15:14
  • 1
    (https://stackoverflow.com/questions/783899/how-can-i-count-text-lines-inside-an-dom-element-can-i) – Omer Oct 11 '19 at 15:16
  • How isn't it? Your question is asking how to get the number of lines right? The other question is asking for the content of these lines, so for your case you just have to get the length of the returned array: https://jsfiddle.net/ex39ow5t/ – Kaiido Oct 11 '19 at 15:18
  • i have just a String-variable i have the text not in a div – Firma Buchfink Oct 11 '19 at 15:18
  • What is this "paragraph" which has lines that "can fit 72 characters"? Certainly a DOM element no? Simply put your string-variable in such an element, run the function there and if you wish, you can even remove the element afterward. – Kaiido Oct 11 '19 at 15:21
  • @Kaiido your example shows that i have 7 lines. If you count the lines of my example from my question if i seperate the lines by myself in 72 er pieces and take care that i cut no word i have 11 lines – Firma Buchfink Oct 11 '19 at 15:24
  • You mean the jsfiddle? Of course, there I only did set the width of the container to `40vw` so it's dependent on the size of your screen, on mine it outputs 13 lines. You just have to set the css to what your own renderer will be. – Kaiido Oct 11 '19 at 15:26
  • I want to figure it out with linebreaks because it is then handed over to a pdf. and on the pdf page, a maximum of 72 characters can be used per line. – Firma Buchfink Oct 11 '19 at 15:32
  • so please remove your duplicate marked! – Firma Buchfink Oct 11 '19 at 15:43
  • Then please [edit] your question and explain your real case. Where are you executing this code? On a browser? Do you have access to the DOM? Where does this 72chars limit come from? Are you sure it's really 72chars? Is it a monospace font? – Kaiido Oct 11 '19 at 15:47
  • I'm guessing you want this q&a https://stackoverflow.com/questions/14484787/wrap-text-in-javascript – gman Oct 11 '19 at 15:51
  • @gman unfortunately not, The text with the \n I get so passed from the database. – Firma Buchfink Oct 11 '19 at 16:03

1 Answers1

0

If I understand your question correctly, you want to know how many lines a piece of text will occupy. It isn't enough to count the line breaks because some text may wrap due to its length.

In that case, you should have a look at the link that @Omar included: How can I count text lines inside an DOM element? Can I?

You'll need two bits of information to count the number of lines which includes the font size and the container width that the PDF will ultimately use.

Note that this will only give you a rough approximation of how many lines will be used because text layout can vary from application to application. i.e. text may render differently based on how the PDF is generated. It should hopefully work well enough.

Also note that if the text comes with line breaks (\n) already included, you may want to add the following style to the container element: white-space: pre-wrap;

Soc
  • 7,425
  • 4
  • 13
  • 30
  • @Omar and Soc - Thank you, i got it. I add white-space: pre-wrap; and i change the width from vw to px so it seperatet on all devices in the same way. THANK YOU – Firma Buchfink Oct 11 '19 at 16:56