I need to add line breaks in positions that the browser naturally adds a new line to the line of text.
For instance:
<p> This is a very long text \ n that spans several lines in a paragraph. </p>
This is the paragraph that the browser decided to break at \ n
I need to find this position and insert.
Does anyone know of any JS libraries or functions that can do this?
The only solution I have found so far is to remove the tokens from the paragraph and observe the clientHeight property to detect a change in element height. I don’t have time to finish this and would like to find something that has already been tested.
Edit: The reason I need to do this is because I need to accurately convert HTML to PDF. Acrobat makes text narrower than a browser. This leads to the fact that the text is torn in different positions. I need the same ragged edge and the same number of lines in the converted PDF.
Edit:
@dtsazza: Thanks for your reply. It is impossible to create a layout editor that almost accurately reflects HTML, I wrote 99% of them;)
The application I'm working on allows the user to create a product catalog by dragging and dropping tiles. Tiles are fixed widths, absolutely positioned divs containing images and text. All elements are designed so that the font size is fixed. My solution for finding \ n in a paragraph is in the order of 80% of the time, and when it works with this paragraph, the resulting PDF is so close to the on-screen version that the differences don't matter. Items have the same height (pixel), images are replaced with high-resolution versions, and all bitmaps are replaced on the server side generated by SVG.
The only slight difference between my HTML and PDF is that Acrobat makes the text a little narrower, which results in a line slightly shorter than the length of the line.
The diode solution for adding spans and finding their coordinates is very good and should give me the location of the BR. Remember that the user will never see HTML with BR inserted - they are added so that the PDF conversion creates a paragraph that is exactly the same size.
There are many people who seem to think that this is impossible. I already have a working application that created an extremely accurate HTML-> PDF conversion of our documents. I just need a better solution to add BR, because my solution sometimes skips BR. BTW, when it works, my paragraphs are the same height as the HTML equivalents that we produce.
If anyone is interested in the doc conversion type, I can check this screen:
http://www.localsa.com.au/brochure/brochure.html
Edit: Thank you very much Diodeus - your offer was in place.
Solution: for my situation it was more appropriate to wrap the words in spaces instead of spaces.
var text = paragraphElement.innerHTML.replace (// g, '</span> <span>');
text = "<span>" + text + "</span>"; // wrap the first and last words.
It wraps every word in the gap. Now I can query the document to get all the words, iterate over and compare the position of y. When y pos changes adds br.
This works flawlessly and gives me the results I need - Thank you!