How define a fixed width page, but length > some minimum length, but only as long as needed

This recent question on how to increase the width of the output, and this earlier question on how to increase the length of the output reminded me of something I have been wondering: I would like to fix the width and have the length to be longer than some minimum length, but only as long as necessary.

So far I have been using the geometry package to fix the width, and set the page length to be very long as per the above links, and this has been working. However, I would like to not have the extra blank space at the bottom for very short pages.

This is intended to generate individual pages for on screen viewing, and not a long document. Hence the desire for a single "page".

-
@Peter: \newgeometry and \restoregeometry from geometry could be what you are looking for? – egreg Jun 2 '11 at 9:25
But how do I determine how long to set the page? The page length is not fixed. If I add more text I want the page length to grow, and if I delete text I want the page length to shrink (but not shrink below a certain minimum) – Peter Grill Jun 2 '11 at 9:46
@Peter paperheight=200cm? – egreg Jun 2 '11 at 9:54
See my answer to Automatically increase PDF page height for a solution based on preview. The minimal length would need to be implemented in addition. – Martin Scharrer Jun 2 '11 at 11:12

\documentclass{article}
\usepackage{geometry}
\geometry{paperwidth=12cm,paperheight=\maxdimen,margin=1cm}

\usepackage{lipsum}

\begin{document}
\setbox0=\vbox{
\lipsum
}
\dimen0=\dp0
\pdfpageheight=\dimexpr\ht0+2cm\relax
\ifdim\pdfpageheight<12cm \pdfpageheight=12cm \fi
\unvbox0\kern-\dimen0

\end{document}


If you try \lipsum[1] instead of \lipsum you get 12cm as height. This can be made automatic, I believe, by hooking into \begin{document} and \end{document}.

-
This seems to do exactly what I need. I'll see if I can figure out how to wrap it around \begin{document} and end{document}. Thanks. – Peter Grill Jun 2 '11 at 18:19
I am accepting this as it does what I want. I have not been able to automate this yet, but will keep trying to figure that out as it should not be that hard. – Peter Grill Jun 20 '11 at 17:40
The automation is described here: tex.stackexchange.com/a/27057/7323 – Patrick Häcker Apr 14 '14 at 14:27