<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Am 11.12.2017 um 21:27 schrieb Hans
Hagen:<br>
</div>
<blockquote type="cite"
cite="mid:c13c2c82-be65-5b13-f143-387afe51636f@wxs.nl"><br>
<blockquote type="cite">A "stat lyInLatex.pdf" after luatex
finished shows that 40 bytes are not written at the time of
callback execution.
<br>
</blockquote>
<br>
Which doesn't mean that there cannot be something written via lua
scripts. Also, there is be more to be wrapped up: the log file,
file recording, closing of the synctex file, etc.
<br>
<br>
</blockquote>
<br>
Yes. In currently stop_run is documented as cited below: <br>
<blockquote><tt>8.5.3 stop_run</tt><br>
<tt>function()</tt><br>
<tt>end</tt><br>
<tt>This callback replaces the code that prints LuaTEX’s
statistics and ‘output written to’ messages.</tt><br>
</blockquote>
In <a moz-do-not-send="true"
href="http://tug.org/pipermail/luatex/2012-May/003647.html">2012</a>
Patrick Gundlach discussed on this list if the sentence "The output
file is not complete at this point and thus you cannot use this
callback to post process the output file." should be added to the
documentation. The subject that stop_run is not executed in
draftmode, the subject of a postprocessing hook and that stop_run
cannot be used for that purpose also came up on stackexchange. It
might be a good idea to extend the documentation, I propose:<br>
<blockquote><tt>8.5.3 stop_run</tt><br>
<tt>function()</tt><br>
<tt>end</tt><br>
<tt>This callback replaces the code that prints LuaTEX’s
statistics and ‘output written to’ messages.<br>
It is not executed </tt><tt>in draftmode, and t</tt><tt>he
output file is not complete at the point of execution.<br>
Thus you cannot use this callback to </tt><tt>post process the
output file.</tt><br>
</blockquote>
<blockquote type="cite"
cite="mid:c13c2c82-be65-5b13-f143-387afe51636f@wxs.nl">Anyway,
managing your workflow can best be done with a wrapper (make was
already suggested but many other solutions are possible).
<br>
</blockquote>
<br>
Anything might be done using wrappers, but it's a pitty that some
tasks currently need a wrapper. A few examples where a pdf_closed
callback would be usefull:<br>
<ul>
<li>I do not consider it to be good programming practice to remove
temporary files that are still opened by the lualatex process,
but I would like to remove those files from within the .sty
file. [Ok, it is possible to remove files used by
\includegraphics in the stop_run callback. They are still open,
but they will not be used anymore and the library will probably
silently handle the situation. But is this good programming
practice?]<br>
</li>
<li>I take the pdf produced by *TeX, split it into pages and
combine those pages and an audio recording to produce a final
x.264 video. That could be done from within the .sty file, but
with current luatex the best I can do is to write a script from
within the style and typeout a message that tells the user to
execute that script. [There is a hackish solution at the end of
the mail]<br>
</li>
<li>If \includegraphics is used to include a lot of pdfs with
embedded fonts the pdfs produced by luatex and other tex engines
often produce big files that waste a lot of disk space because
of duplicated font data. A solution is to include pdfs without
embedded fonts and to post process the output with ghostscript.
[There is a hackish solution at the end of the mail]<br>
</li>
</ul>
<p>So there really would be reasonable use for a pdf_closed callback<br>
</p>
<blockquote><tt>8.5.x pdf_closed</tt><br>
<tt>function()</tt><br>
<tt>end</tt><br>
<tt>This callback is executed immediately after the output pdf has
been finished and closed.<br>
</tt><tt>It might be used to</tt><tt> post process the output file.<br>
</tt></blockquote>
In 2012 <a moz-do-not-send="true"
href="http://tug.org/pipermail/luatex/2012-May/003648.html">Taco
Hoekwater wrote</a>:<br>
<blockquote>
<pre>At this point I am not sure any more but there definitely is a use case
for a callback that runs after the pdf has been closed already. I am not
sure whether we should change stop_run or create a new callback.
</pre>
<blockquote>
</blockquote>
</blockquote>
<blockquote type="cite"
cite="mid:c13c2c82-be65-5b13-f143-387afe51636f@wxs.nl">Pre- or
postprocessing as part of the run is not part of the concept. What
works for you can fail for someone else as timing is very
application specific.
<br>
</blockquote>
<br>
I don't understand your last sentence about timing, but I hope that
you rethink about the implementation of an additional pdf_closed()
callback.<br>
<br>
Now here is the hackish solution to the problem that works on my
system. I really don't like it, but it works here. Obviously it
depends on the availability of a unix environment and the tools
used, no error handling is implemented. The unfinished pdf is copied
and fixed, mutool is used to obtain the correct xref offset. After
that tmp.pdf should be identical to the final pdf and might be used
as desired.<br>
<blockquote><tt>\def\tmpScriptName{tmpfixpdf.sh}</tt><br>
<tt>\newwrite\tmpScript</tt><br>
<tt>\immediate\openout\tmpScript=\tmpScriptName</tt><br>
<tt>\immediate\write\tmpScript{cp \jobname.pdf tmp.pdf}</tt><br>
<tt>\immediate\write\tmpScript{echo endstream>> tmp.pdf}</tt><br>
<tt>\immediate\write\tmpScript{echo endobj>> tmp.pdf}</tt><br>
<tt>\immediate\write\tmpScript{echo startxref>> tmp.pdf}</tt><br>
<tt>\immediate\write\tmpScript{\unexpanded{echo $((`mutool show
tmp.pdf xref 2>/dev/null</tt><br>
<tt> | grep : </tt><br>
<tt> | tail -1 </tt><br>
<tt> | sed -e
's/[[:digit:]]*: 0*\([[:digit:]]*\) </tt><br>
<tt>
[[:print:]]*/\1/'` + 1))</tt><br>
<tt> >>
tmp.pdf}}</tt><br>
<tt>\immediate\write\tmpScript{echo \%\%EOF>> tmp.pdf}</tt><br>
<tt>\immediate\write\tmpScript{\unexpanded{echo -e "\nHave a look
at tmp.pdf"}}</tt><br>
<tt>\immediate\closeout\tmpScript</tt><br>
<br>
<tt>\directlua {</tt><br>
<tt> os.execute("chmod 700 \tmpScriptName")</tt><br>
<br>
<tt> function my_stop_run()</tt><br>
<tt> os.execute("./\tmpScriptName")</tt><br>
<tt> end</tt><br>
<tt> </tt><br>
<tt> luatexbase.add_to_callback('stop_run', my_stop_run, 'TEST')</tt><br>
<tt>}</tt><br>
</blockquote>
I think is time to quote Hans Hagen: "so far my experience with tex
is that there's always a solution" ;-)<br>
<br>
Knut<br>
</body>
</html>