Get the automatically produced line and page breaks from LaTeX

My aim is to produce an html-file with the same text as the pdf produced by LaTeX- The html shall represent the pagination and line-break structure of the pdf: When there is a linebreak in the pdf I want to produce a <br> in html, when there is a paragraph I want to produce a <p> in html, when there is a newpage in the pdf I want to produce a horizontal line in html.

Handling of the paragraphs is easy since they are defined in the input file. But line-breaking and pagination depends on the font and on the width and height of the document (and maybe on some other things I cannot even imagine yet).

Is there a way of getting LaTex to tell me where it broke the lines and where it started a new page?

• Not easily, you may find it easier to simply convert the pdf to html. But even if you preserve the line breaks justified text will not look justified unless you are using exactly the same fonts and same white space calculations. Mar 2 '15 at 12:29
• The html does not need to appear as a kind of faksimile of the pdf, so the justified-issue is no problem. I just need the line-break and pagination structure for citation reasons. Mar 2 '15 at 12:52
• oh OK, as I say it's not that easy, you could add \showoutput to the document then essentially all the information about where each character went is in the log file, but constructing any usable html from that format might be an interesting exercise. Mar 2 '15 at 13:00
• a solution for the line breaking problem : create a vbox, use vsplit to create mini vbox of one line and export them with LuaLaTeX (see tex.stackexchange.com/q/228312/7712) Mar 13 '15 at 20:11

This latex:

\documentclass{article}
\showoutput
\usepackage{lipsum}
\begin{document}

\lipsum

\end{document}


Produces a log file showing the position of all the output:

.....
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.

Completed box being shipped out [1]
\vbox(633.0+0.0)x407.0
.\glue 16.0
.\vbox(617.0+0.0)x345.0, shifted 62.0
..\vbox(12.0+0.0)x345.0, glue set 12.0fil
...\glue 0.0 plus 1.0fil
...\hbox(0.0+0.0)x345.0
..\glue 25.0
..\glue(\lineskip) 0.0
......
...\hbox(6.94444+1.94444)x345.0, glue set 0.85849
....\hbox(0.0+0.0)x15.0
....\OT1/cmr/m/n/10 L
....\OT1/cmr/m/n/10 o
....\OT1/cmr/m/n/10 r
....\OT1/cmr/m/n/10 e
....\OT1/cmr/m/n/10 m
....\glue 3.33333 plus 1.66666 minus 1.11111
....\OT1/cmr/m/n/10 i
....\OT1/cmr/m/n/10 p
.......


So with a bit of perl (which might need to be made smarter in a real example) You can re-constitute the text adding the requested line and paragraph markup:

#!/usr/bin/perl
while(<>){
chomp();
if(m@^\.[^ ]* (.)\s*$@){ print "$1";
}
if (m@ligature ([^ ]*)\)\s*$@){ print "$1";
}
if(m@^\.*\\glue ([0-9.]*)@){
print " " if ($1 > 2); } print"\n<br>" if (m@\\baselineskip@); print"\n<p>" if (m@\\parskip@); print "\n\n<hr>\n\n" if (m@Completed box being shipped@); }  then perl zz.pl zz.log > zz.html produces: ..... <br>fau-cibus. Morbi do-lor nulla, male-suada eu, pul-v-inar at, mol-lis ac, nulla. Cur- <br>abitur auc-tor sem-per nulla. Donec var-ius orci eget risus. Duis nibh mi, congue <br>eu, ac-cum-san eleifend, sagit-tis quis, diam. Duis eget orci sit amet orci dig-nis-sim <br>rutrum. <p> <br>Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi <br>auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultric ...  which looks like • actually this does need to be smarter, as shown it doesn't distinguish between discretionary hyphens that are taken and those that aren't but the latter should be easy to spot at the ends of lines... Mar 13 '15 at 20:17 • That is cool. I wanted to do something similar (with python) but did not find the time to actually make it yet. But I have another problem: my original text has some special characters ("ä","ü","ö" for instance), which get lost by the log file. Do you have an idea how to solve it? In some way I have to find the letters in the .log-file back in the original .tex-file Mar 13 '15 at 21:17 • @jonathan.scholbach two ways you can use a tcx file so the T1 encoded characters get writen as bytes rather than ^^ notation in the log, or change the perl where it picks up a character (.) and make it pick up (^^..) and in that case write out the html hex reference &#x$1; works so long as the T1 codepoint = Unicode, which it does in the Western European languages except for German ess-zed, so you'd have to fix that. Make sure you are not using OT1 as I do above. Or you could use xelatex or lualatex then the log will have unicode characters with the write codepoints Mar 13 '15 at 21:24