5

I would like to integrate a sanity test in my Makefile that builds multiple PDFs. I would like to count and compare the number of pages generated based on what xelatex outputs during/after the run, compare it an expected value and stop make to see if layout changes accidentally created too many / too few pages. What is the best/easiest way to do that?

I could grep for the usual page counter in the output, e.g. [1][2][3], but that is prone to errors and often this counter is 1 page higher than the resulting PDF - in above example it counts to [3], but the PDF contains only 2 pages.

I would like to avoid to check the PDF itself in a separate step so I don't have to "guess" its filename (which is generated in my case). In case that makes a difference, I need a solution for xelatex.

TeXter
  • 1,902
  • As a bonus question: WHY does xelatex sometimes count an additional page that is not in the PDF? – TeXter Dec 27 '15 at 07:38
  • 4
    I don't see the TeX related content in this question yet. You could write the number of pages at the end of the document to a normal ASCII file and analyze this. –  Dec 27 '15 at 07:47
  • I am trying to analyze the TeX output (console / log) and/or return codes ... I tried to make that clearer in the OP. – TeXter Dec 27 '15 at 07:52
  • 3
    what about the Output written on filename.pdf (x pages). in filename.log? – touhami Dec 27 '15 at 08:42
  • Thanks, I have missed that line - it seems more reliable than the [] values. I would gladly accept "grep for that" as an answer. – TeXter Dec 27 '15 at 08:56
  • @touhami: Your turn ;-) –  Dec 27 '15 at 09:02
  • @ChristianHupfer I am not sure, if you can do it for me many thanks, otherwise I'll ask the OP. – touhami Dec 27 '15 at 09:04
  • @touhami: Do it -- you provided the easiest way –  Dec 27 '15 at 09:09
  • What about pdfinfo filename.pdf | grep Pages:? – egreg Dec 27 '15 at 10:02
  • 1
    With a rather straightforward addition to texloganalyser I can get texloganalyser -q test.log to output Output written on test.pdf (9 pages, 48492 bytes). It could be a nice feature request. – egreg Dec 27 '15 at 12:13
  • @egreg That's a separate step, though, isn't it? pdfinfo, I mean. – cfr Dec 27 '15 at 12:22
  • @cfr Yes, it's part of Poppler – egreg Dec 27 '15 at 12:24
  • 1
    Related: http://tex.stackexchange.com/questions/198091/get-number-of-pages-of-external-pdf – egreg Dec 27 '15 at 12:25

2 Answers2

5

There are several ways for knowing the number of pages in a PDF file.

  1. Grep the log file for Output written that will return something like

    Output written on test.pdf (18 pages, 74251 bytes).
    

    (independently of the page numbering)

  2. Use pdfinfo (that comes with Poppler), so pdfinfo test.pdf|grep Pages will output something like

    Pages: 18

  3. Use qpdf --show-npages test.pdf that just outputs 18.

  4. Check this question and the answer by Heiko Oberdiek Get number of pages of external PDF

egreg
  • 1,121,712
2

You can write it from within xelatex to an external file, e.g. \jobname.pp:

\documentclass{article}
\usepackage{totpages}
\usepackage{blindtext}
\AtEndDocument{%
    \newwrite\tempfile\newpage
    \immediate\openout\tempfile=\jobname.pp
    \immediate\write\tempfile{\theTotPages}%
    \immediate\closeout\tempfile}
\begin{document}

\blinddocument \newpage \blinddocument

\end{document}