I am trying to automate the solution proposed by egreg to How define a fixed width page, but length > some minimum length, but only as long as needed by hooking into \begin{document}
and \end{document}
.
So, egreg's solution works fine:
\documentclass{article}
\def\MinimumPaperHeight{12cm}
\usepackage{geometry}
\geometry{paperwidth=\MinimumPaperHeight,paperheight=\maxdimen,margin=1cm}
\usepackage{lipsum}
\begin{document}
\setbox0=\vbox{
\lipsum[1-6]
}
\dimen0=\dp0
\pdfpageheight=\dimexpr\ht0+2cm\relax
\ifdim\pdfpageheight<\MinimumPaperHeight \pdfpageheight=\MinimumPaperHeight \fi
\unvbox0\kern-\dimen0
\end{document}
But can't seem to get this to work:
\documentclass{article}
\def\MinimumPaperHeight{12cm}
\usepackage{geometry}
\geometry{paperwidth=\MinimumPaperHeight,paperheight=\maxdimen,margin=1cm}
\usepackage{lipsum}
\usepackage{environ}
\NewEnviron{MyBox}{
\setbox0=\vbox{\BODY}%
}{%
\dimen0=\dp0%
\pdfpageheight=\dimexpr\ht0+2cm\relax%
\ifdim\pdfpageheight<\MinimumPaperHeight \pdfpageheight=\MinimumPaperHeight \fi%
\unvbox0\kern-\dimen0%
}
\AtBeginDocument{\begin{MyBox}}
\AtEndDocument{\end{MyBox}}
\begin{document}
\lipsum[1-6]
\end{document}
Hooking into \AtBeginDocument
is per this solution on How to automatically add text immediately after \begin{document}.