QUESTION
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!
ANSWER
\documentclass{article}
\IfFileExists{\jobname.pdf}
{\edef\Size{\pdffilesize{\jobname.pdf}}}
{\def\Size{0}}
\begin{document}
\Size
xyz
\end{document}
You test the existence of the PDF file produced in the previous run before pdftex 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:
\IfFileExists{\jobname.pdf}
{\edef\Size{\number\numexpr\pdffilesize{\jobname.pdf}/1024\relax\,KiB}}
{\def\Size{0\,KiB}}
Tweet