I would like to produce a pdf document whose pages have various lengths and fixed widths.
An easier situation
The easiest case would be for instance: the first page has a length of 10cm all the next pages have a length of 15cm. But, what I am looking is more complicated. I wonder if it is possible.
The real situation
Here is what I would like to achieve:
- the first page has a variable length, depending of the content (cf. this question), which is at most
10cm - the next pages have a length equal to
15cm - the last page has a variable length, depending of the content, which is at most
15cm
Bounty-rewarded
For those interested, there will be bounties for answers.
"Bug" in Heiko's solution
The solution propped by Heiko, which is great by the way, seems to have problem handling the list. Here is a MWE that shows this problem. The problem is that when there is a list (an itemize environment), the page change is done too early, and, as a result, the first page, whose height should be 8cm in the following example, is too short.
\documentclass{article}
\usepackage%
[paperwidth=10.000000cm,
paperheight=8cm,
hmargin=1.000000mm,
top=1.000000mm,
bottom=1.000000mm]
{geometry}
\pagestyle{empty}
\flushbottom
\setlength{\maxdepth}{0pt}% to address the "third bug"
\setlength{\topskip}{0pt}% no space above the first line
% assuming the page number is the absolute page number
\usepackage{zref-totpages,zref-savepos}
\usepackage{atbegshi}
\makeatletter
\providecommand{\zsaveposy}{\zsavepos}% for older zref-savepos
\def@oddhead{\PosFirstHead\PosLastHead\hss}%
\def@evenhead{\PosLastHead\hss}%
\newcommand{\PosFirstHead}{%
\ifnum\value{page}=1 %
\zsaveposy{PosFirstHead}%
\global\let\PosFirstHead@empty
\fi
}
\newcommand*{\PosLastHead}{%
\ifnum\value{page}=\ztotpages
\zsaveposy{PosLastHead}%
\global\let\PosLastHead@empty
\fi
}
\AtBeginShipout{%
\ifnum\value{page}=1 %
\dimen@=\dimexpr
\zposy{PosFirstHead}sp-\headsep
-\zposy{PosFirst}sp%
\relax
\setbox\AtBeginShipoutBox=\vbox{%
\kern-\dimen@ %
\copy\AtBeginShipoutBox
}%
\advance\pdfpageheight by -\dimen@
\else
\ifnum\value{page}=\ztotpages
\advance\pdfpageheight by%
-\dimexpr
\textheight
-\zposy{PosLastHead}sp+\headsep
+\zposy{PosLast}sp%
\relax
\fi
\fi
}
\AtEndDocument{%
\par
\nobreak
\dimen@=\prevdepth
\ifdim\dimen@>\maxdepth
\kern-\maxdepth
\else
\ifdim\dimen@>0pt %
\kern-\dimen@
\fi
\fi
\zsaveposy{PosLast}%
}
\makeatother
\usepackage{lipsum}% provides dummy text
\interlinepenalty=-100
\begin{document}
\vspace*{\dimexpr0.000cm-\topskip plus 1fill}
\zsaveposy{PosFirst}
\nointerlineskip
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test:
\begin{itemize}
\item Hello
\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.
\item Good Bye
\item Hello
\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.
\end{itemize}
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
\end{document}

"Bug" in wipet's solution
I tried wipet's solution with the package mdframed and it clashes. Here is a MWE. It produces a document with only one page.
\documentclass{article}
\usepackage[
paperwidth=15cm,
paperheight=15cm,
hmargin=1cm,
vmargin=0cm,
]{geometry}
\usepackage[framemethod=tikz, needspace=1.5cm]{mdframed}
\usepackage{lipsum}
\newmdenv[%
innerleftmargin = 2mm,
innerrightmargin = 2mm,
innertopmargin = 2mm,
innerbottommargin = 2mm,
leftmargin = 0mm,
rightmargin = 0mm,
splitbottomskip = 2mm,
splittopskip = 4mm,
middlelinewidth = 0mm,
linecolor = red,
backgroundcolor = red,
roundcorner = 0pt,
skipbelow = 0mm,
skipabove = 0mm,
]
{mybox}
\pagestyle{empty}
\topmargin=0pt % vertical shift of the text in the page
\pdfvorigin=0in % vertical margins above and below the text
\let\textheight=\vsize
\expandafter\let\csname @colht\endcsname=\vsize
\newdimen\topbotmargin
\topbotmargin=2\pdfvorigin \advance\topbotmargin by2\voffset
\headheight=0pt \headsep=0pt
\long\def\firstpage#1{\setbox0=\vbox{#1}\pdfpageheight=\ht0
\advance\pdfpageheight by\topbotmargin \vsize=\ht0 \box0 \vfil\break
\pdfpageheight=15cm \vsize=\pdfpageheight \advance\vsize by-\topbotmargin
}
\AtEndDocument{\pdfpageheight=\pagetotal \vsize=\pagetotal
\advance\pdfpageheight by\topbotmargin
}
\begin{document}
\begin{mybox}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque.
\end{mybox}
\lipsum[1-9]
\end{document}



geometrypackage in conjunction with\geometry{<options>}will suit to your needs. You can declare this on a page-by-page basis, or (more advanced!) automate it between two values with a boolean. – 1010011010 May 22 '14 at 11:57geometrycan also handle the issue of indeterminate length? – Colas May 22 '14 at 12:0510cmbut the length according to the content. If the content is "longer than10cm", then the length of the page is10cm. – Colas May 22 '14 at 12:22\newgeometry? – 1010011010 May 22 '14 at 12:36pdflatex. Thanks. – Colas May 22 '14 at 18:50mdframed. You require a shorter last page (10cm). If the last page is 11cm, for example, you have a problem: 11cm does not fit on 10cm and 11cm does not fill 15cm and leaves something for the final 10cm page. You have to shorten your text or make it longer. – Heiko Oberdiek May 27 '14 at 12:26\flushbottomand add some glue to\parskip/\baselineskip, e.g.\addtolength{\parskip}{0pt plus 2ex}to stretch the white spaces. – Heiko Oberdiek May 27 '14 at 12:3610cm + n*15cm + x, withxbetween 10 and 15. I will drop this condition. Thanks! Do you know how to fix the issue about the margins? (the same issue happens on the last page) – Colas May 27 '14 at 12:44\setlength{\maxdepth}{0pt}\flushbottomshould move the last line of the text page right to the bottom border of the text area without descenders outside the text area. – Heiko Oberdiek May 27 '14 at 12:47\flushbottomis better because the same margin to the baseline is applied everywhere. Thank you very much for this help!!! – Colas May 27 '14 at 12:5010cm) – Colas May 27 '14 at 12:53\maxdepth. But you have to leave some space in the margins. Otherwise the descenders (ofg,p, ...) are outside the page. – Heiko Oberdiek May 27 '14 at 21:14\vboxwarnings. The example can be fixed, e.g. by\addtolength{\parskip}{0pt plus 1ex}and\makeatletter\g@addto@macro\normalsize{\addtolength{\baselineskip}{0pt plus 1pt}}\makeatother. – Heiko Oberdiek May 27 '14 at 22:02\setlength{\topskip}{0pt}, but then the first baseline might vary depending on the text. An alternative:\settoheight{\topskip}{ABC}. – Heiko Oberdiek May 28 '14 at 11:26