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
- fits page geometry to an inputted file precisely
- draws a grid over the entire document (see How can I make a perfect page grid that fits my page for measuring purposes in TikZ?)
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}
pdfinfo
can show you the dimension of a PDF file.pdfinfo
tex.stackexchange.com/a/26531/13552pdfinfo
.\includegraphics
the first page into a box and measure the box.