2

For example, let's say that I use the pdfpages package and import a PDF. Is there any way (without using an external scripting language like Python) to access the dimensions of the PDF? Can I pass those dimensions to the geometry package?

I would like a simple way to create a document output that

The grid overlay should be drawn efficiently using these new dimensions (variables grabbed from input should be passed to TikZ), because I don't want to just make the grid big enough to cover any likely paper size (not very efficient for inputs with smaller dimensions).

Purpose

This is useful for reverse-engineering PDF templates/layouts (e.g. from InDesign) or images. By reverse-engineering, I mean that I can easily design precise layouts with TikZ's page coordinates.

Code

In this example, I commented out a pdfinput in the body. I want to grab the pdf dimensions at that point and create \setpaperwidth and \setpaperheight macros that set the \inputwidth and \inputheight variables.

\documentclass{article}
\usepackage{fontspec}

\newcommand\inputwidth{5.5in}  % <- set from input
\newcommand\inputheight{8.5in} % <- set from input

\usepackage[%
paperwidth=\inputwidth,  
paperheight=\inputheight,  
verbose,             % show the values of the parameters in the log file
]{geometry}

\usepackage{pdfpages}
\usepackage{tikz} % Support for drawing grid
\usetikzlibrary{backgrounds,calc}
\usepackage{atbegshi} % Add support for the showgrid overlay
\newcommand{\showgrid}{%
  \AtBeginShipoutNext{\AtBeginShipoutAddToBoxForeground{%
      \begin{tikzpicture}
        [
          overlay,
          remember picture,
          inner sep=0pt,
          outer sep=0pt,
          minor line/.style={help lines, draw=black!50, on background layer},
          major line/.style={help lines, draw=black},
          major number/.style={font=\fontsize{3}{5}\selectfont\bfseries},
          minor number/.style={font=\fontsize{1}{2}\selectfont},
        ]
        \pgfmathtruncatemacro\xmaxstep{\paperwidth/1mm}% calculate needed steps in x direction
        \pgfmathtruncatemacro\ymaxstep{\paperheight/1mm}% calculate needed steps in y direction
        \foreach \step in {0,1,...,\xmaxstep} {
          \pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
          \draw [\gridlineconfig] ($(current page.north west) + (\step mm,0)$) -- ($(current page.south west) + (\step mm,0)$);
        }
        \foreach \step in {0,1,...,\ymaxstep} {
          \pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
          \pgfmathsetmacro\numberconfig{ifthenelse(equal(int(mod(\step,10)),0),"major number","minor number")}%
          \draw [\gridlineconfig] ($(current page.north west) - (0,\step mm)$) -- ($(current page.north east) - (0,\step mm)$);
          \node [anchor=north,\numberconfig] at ($ (current page.north west) + (\step mm,0) $) {\step};
          \node [anchor=west,\numberconfig] at ($ (current page.north west) - (0,\step mm) $) {\step};
        }
      \end{tikzpicture}
    }%
  }%
}
\pagenumbering{gobble} % Remove Page Numbers (without fancyhdr)
\begin{document}
\showgrid
\null
%\includepdf[noscale]{pdffile.pdf} % <- Grab dimensions here
\end{document}
  • 1
    pdfinfo can show you the dimension of a PDF file. – egreg Sep 22 '15 at 14:25
  • @egreg I assume that is not the same as this pdfinfo tex.stackexchange.com/a/26531/13552 – Jonathan Komar Sep 22 '15 at 14:52
  • No, I mean the command line utility named pdfinfo. – egreg Sep 22 '15 at 14:58
  • Well, you can \includegraphics the first page into a box and measure the box. – yo' Sep 22 '15 at 15:10
3

You can use graphicx's \includegraphics to store a page of the document and measure it:

\newsavebox{\measurebox}
\newlength{\measuredwidth}
\newlength{\measuredheight}
\newcommand\measureimage[2][1]{%
    \savebox{\measurebox}{\includegraphics[page=#1]{#2}}%
    \setlength{\measuredwidth}{\wd\measurebox}%
    \setlength{\measuredheight}{\ht\measurebox}%
    \savebox{\measurebox}{}%
    }

...

\measureimage[2]{myfigure} % measures the 2nd page of the file.

\showthe\measuredwidth % shows the width
\showthe\measuredheight % shows the height

After issuing \measureimage[pagenumber]{filename}, the width and height of the image are stored in \measurewidth and \measuredheight. The default page number is 1. Just remember that doing this is not quite fast so you should use it only a small number of files (it wouldn't be good if you needed to measure each page separately).

1

Fit Page Dimensions to Input and Draw Grid Overlay

After experimenting with yo's answer, I found a way to achieve this with any PDF or image file input.

Given image hike.png:

  • identify -format "%wx%h\n" hike.png -> 483x271
  • file hike.png- > hike.png: PNG image data, 483 x 271, 8-bit/color RGBA, non-interlaced

enter image description here

Include in body with \measureimage[1]{hike.png}, which does the following:

  1. Grab dimensions of hike.png
  2. Reset paperwidth and paperheight for TikZ nodes
  3. Reset pdfpaperwidth and pdfpaperheight for actual paper size output with \eject
  4. Add image overlay using TikZ node
  5. Apply grid over image (using \pdfpagewidth and \pdfpageheight for x and y max values) in 1 millimeter intervals.

Code

\documentclass{article}
\usepackage{fontspec}

\usepackage[%
%paperwidth=\inputwidth,  % Unneeded
%paperheight=\inputheight,% Unneeded
verbose,             % show the values of the parameters in the log file
]{geometry}

\usepackage{pdfpages}
\usepackage{tikz} % Support for drawing grid
\usetikzlibrary{backgrounds,calc}
\usepackage{atbegshi} % Add support for the showgrid overlay
\newcommand{\showgrid}{%
  \AtBeginShipoutNext{\AtBeginShipoutAddToBoxForeground{%
      \begin{tikzpicture}
        [
          overlay,
          remember picture,
          inner sep=0pt,
          outer sep=0pt,
          minor line/.style={help lines, draw=black!50, on background layer},
          major line/.style={help lines, draw=black},
          major number/.style={font=\fontsize{3}{5}\selectfont\bfseries},
          minor number/.style={font=\fontsize{1}{2}\selectfont},
        ]
        \pgfmathtruncatemacro\xmaxstep{\pdfpagewidth/1mm}% calculate needed steps in x direction
        \pgfmathtruncatemacro\ymaxstep{\pdfpageheight/1mm}% calculate needed steps in y direction
        \foreach \step in {0,1,...,\xmaxstep} {
          \pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
          \draw [\gridlineconfig] ($(current page.north west) + (\step mm,0)$) -- ($(current page.south west) + (\step mm,0)$);
        }
        \foreach \step in {0,1,...,\ymaxstep} {
          \pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
          \pgfmathsetmacro\numberconfig{ifthenelse(equal(int(mod(\step,10)),0),"major number","minor number")}%
          \draw [\gridlineconfig] ($(current page.north west) - (0,\step mm)$) -- ($(current page.north east) - (0,\step mm)$);
          \node [anchor=north,\numberconfig] at ($ (current page.north west) + (\step mm,0) $) {\step};
          \node [anchor=west,\numberconfig] at ($ (current page.north west) - (0,\step mm) $) {\step};
        }
      \end{tikzpicture}
    }%
  }%
}
\pagenumbering{gobble} % Remove Page Numbers (without fancyhdr)

\newsavebox{\measurebox}
\newlength{\measuredwidth}
\newlength{\measuredheight}

\newcommand\measureimage[2][1]{%
    \savebox{\measurebox}{\includegraphics[page=#1]{#2}}%
    \setlength{\measuredwidth}{\wd\measurebox}%
    \setlength{\measuredheight}{\ht\measurebox}%
    \savebox{\measurebox}{}%
    \paperwidth=\measuredwidth \paperheight=\measuredheight     % paper dimensions for TikZ page nodes
    \eject \pdfpagewidth=\measuredwidth \pdfpageheight=\measuredheight % set paper dimensions in XeLaTeX
    \tikz [overlay, remember picture] \node [anchor=north west,inner sep=0pt,outer sep=0pt] at (current page.north west) {\includegraphics[page=#1]{#2}};
    \showgrid
    }

\begin{document}
\measureimage[1]{hike.png} % measures the 2nd page of the file.

%Height: \the\measuredwidth % shows the width of the paper 

%Width: \the\measuredheight % shows the height of the paper
\end{document}

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.