# How to use macro inside \pdfpageattr

I would like to use macro to set /MediaBox, /BleedBox, /CropBox and /TrimBox equal to the actual page size. But it seems that \pdfpageattr is reading list of tokens and is not a "regular macro" (if I understand well). As I'm not an expandafterguru, I was not able to achive this.

The following MNWE is not producing the desired result :

\documentclass{article}

\makeatletter
\edef\pwpt{\strip@pt\dimexpr0.996264009963\paperwidth\relax} %paper width in PS points
\edef\phpt{\strip@pt\dimexpr0.996264009963\paperheight\relax} %paper height in PS points
\makeatother

\begin{document}

Paper width is \pwpt\ PS points

Paper height is \phpt\ PS points

\pdfpageattr{
/MediaBox [0 0 \pwpt \phpt]
/BleedBox [0.00000 0.00000 \pwpt \phpt]
/CropBox [0 0 \pwpt \phpt]
/TrimBox [0.00000 0.00000 \pwpt \phpt]
}

\end{document}


There is no compilation error, but the produced pdf is not valid.

Note: I was trying to automate this answer.

Here is a solution using \expandafter and \space.

With \pdfcompresslevel 0 and \pdfobjcompresslevel 0 into the preamble, you can verify directly the PDF document:

 /MediaBox [0 0 611.9985 791.99806]
/BleedBox [0.00000 0.00000 611.9985 791.99806]
/CropBox [0 0 611.9985 791.99806]
/TrimBox [0.00000 0.00000 611.9985 791.99806]

\documentclass{article}

\pdfcompresslevel 0
\pdfobjcompresslevel 0

\makeatletter
\edef\pwpt{\strip@pt\dimexpr0.996264009963\paperwidth\relax} %paper width in PS points
\edef\phpt{\strip@pt\dimexpr0.996264009963\paperheight\relax} %paper height in PS points
\makeatother

\begin{document}

Paper width is \pwpt\ PS points

Paper height is \phpt\ PS points

\edef\mypdfpageattr{
/MediaBox [0 0 \pwpt\space\phpt]^^J
/BleedBox [0.00000 0.00000 \pwpt\space\phpt]^^J
/CropBox [0 0 \pwpt\space\phpt]^^J
/TrimBox [0.00000 0.00000 \pwpt\space\phpt]
}
\expandafter\pdfpageattr\expandafter{\mypdfpageattr}
\end{document}

• Thanks ! How stupid I am. I have tried the same expandafter code but without the \space ... and so the space was gobbled by tex. Remark : the ^^J seems not necessary to produce valid pdf. – Kpym Jun 6 '15 at 13:28
• @Kpym ^^J is just cosmetic (add newline into the PDF code). – Paul Gaborit Jun 6 '15 at 14:48

I use luatex and had some trouble with the accuracy of the calculation of the bp-lengths: The measurement of the distances between the Media|BleedBox and the TrimBoxes reported e.g. 4.97mm instead of 5mm (Acrobat preflight reported another distance of 4.98mm).

After I used expl3's \dim_to_decimal_in_bp the problems are gone: 5mm == 5mm. ;-)

The following code should demonstrate the [slightly] differences (at least regarding the width):

\RequirePackage{luatex85}
\pdfcompresslevel=0
\pdfobjcompresslevel=0
\documentclass[12pt]{article}
\usepackage[%
paperwidth=540mm,
paperheight=226mm,
margin=0cm,
]{geometry}

\makeatletter
\edef\pwpt{\strip@pt\dimexpr0.996264009963\paperwidth\relax}
\edef\phpt{\strip@pt\dimexpr0.996264009963\paperheight\relax}
\makeatother

%Alternative conversion using expl3:
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\convert}{m}{%
\fp_eval:n
{
round ( \dim_to_decimal_in_bp:n { #1 } , 3 )
}
}
\ExplSyntaxOff

\edef\mypdfpageattr{
/MediaBox[%
0
0
\pwpt\space
\phpt]^^J
/MediaBox[%
0
0
\convert{\paperwidth}\space
\convert{\paperheight}]^^J
}
\expandafter\pdfpageattr\expandafter{\mypdfpageattr}

\begin{document}

These dimensions (at least the width) do not match the auto-generated MediaBox
definition:

Paper width is \pwpt\ PS points ($\leftarrow$ differs from auto-generated
MediaBox definition)

Paper height is \phpt\ PS points ($\leftarrow$ seems ok)

\bigskip

The following results do match the automatically added MediaBox (open pdf
file in text editor for details):

Paper width is \convert{\paperwidth}\ PS points ($\leftarrow$ seems better)

Paper height is \convert{\paperheight}\ PS points ($\leftarrow$ seems better)
\end{document}


Note: The two MediaBox definitions add a second and a third /MediaBox[... statement, to the resulting pdf file (open pdf in text editor, for details): Don't know the cause for the first and whether it can be replaced.