How can I include the (approximate) file size of the generated pdf within a pdflatex document? I envision having to compile it three times: once to create the pdf, a second time to write the size to an .aux file and the third pass will include it in the document. A minimal working example would be pedagogic and useful!

3 Answers
3

You test the existence of the PDF file produced in the previous run beforepdftex starts to write on a new one. The primitive \pdffilesize outputs the file size and with \edef you freeze this at the moment of executing \pdffilesize.

It won't be accurate to the byte, because of compression. I get

10401
9427
10258
10599
10466

in successive runs over the document. Dividing by 1024 and rounding could be better:

Here is an implementation in ConTeXt MkIV. I use io.fileseize(...) (part of ConTeXt's l-io.lua library) to find the size of the pdf file and add it to a two pass data list. By default, ConTeXt automatically recompiles the file if the two pass data list has changed, until a maximum of 8 runs after which it stops even if the two pass data is changing.

The file size does not stabilise because of how pdf compresses (the digits). As soon as you enter a new file size (with different digits), it compresses differently when converted to pdf.

For example, this code:

\documentclass{article}
\begin{document}
1234
\end{document}

and this code:

\documentclass{article}
\begin{document}
4321
\end{document}

will produce files of equal size but this code:

\documentclass{article}
\begin{document}
1235
\end{document}

will produce a pdf file of a different size. So when you insert this new size into the document, just because the digits are different from the last time, it will produce a pdf file of a different size. However, you might find that after a number of iterations, the pdf file size appears to cycle through the same numbers.