Introduction
At the moment I’m preparing a custom class which is likely to be compiled with XeTeX or LuaTeX. It is for a book layout that should be professionally printed and therefor I want to set the TrimBox
of the output PDF. The problem is the size of the bleed is user defined by a key-val class option. Therefore I need to be able to use macros as part of the \special
/\pdfpageattr
commands.
General Idea
The general Idea is to use either \special
s (XeTeX) or \pdfpageattr
(LuaTeX, PDFTeX) to set the TrimBox. The distinction of the compiler can be done with LaTeX3 \sys_if_engine_
and also the TrimBox strings can be defined as token lists. Then I define a macro using the variable content (:V
) of these token lists to write the correct values to the PDF.
Code
This is the minimal example code I came up with.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn\makeatletter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% bleed value (keyval class option in real document)
\dim_new:N \g_@@_bleed_dim
\dim_set:Nn \g_@@_bleed_dim { 3mm }
%% set page size (and other stuff in the real document)
\usepackage { geometry }
\geometry { papersize = { 185mm , 245mm } }
%% set crop size
\usepackage [ center ] { crop }
\CROP@size {
\dim_eval:n { \paperwidth + 2 \g_@@_bleed_dim }
} {
\dim_eval:n { \paperheight + 2 \g_@@_bleed_dim }
}
%% new variant
\cs_generate_variant:Nn \dim_to_decimal_in_bp:n { N }
% values for the rim box
\tl_new:N \g_@@_trim_box_quadruple_tl
\tl_set:Nx \g_@@_trim_box_quadruple_tl {
\dim_to_decimal_in_bp:N \g_@@_bleed_dim \c_space_token
\dim_to_decimal_in_bp:N \g_@@_bleed_dim \c_space_token
\dim_to_decimal_in_bp:n { \paperwidth + \g_@@_bleed_dim } \c_space_token
\dim_to_decimal_in_bp:n { \paperheight + \g_@@_bleed_dim }
}
%% special for use with XeTeX
\tl_new:N \g_@@_trim_special_tl
\tl_set:Nx \g_@@_trim_special_tl {
pdf: \c_space_token put \c_space_token @thispage \c_space_token
<< \c_space_token /TrimBox \c_space_token [ \g_@@_trim_box_quadruple_tl ] \c_space_token >>
}
%% page attribute for use with LuaTeX and PDFTeX
\tl_new:N \g_@@_trim_pageattr_tl
\tl_set:Nx \g_@@_trim_pageattr_tl {
/TrimBox \c_space_token [ \g_@@_trim_box_quadruple_tl ]
}
%% command to set the specials for xetex
\cs_new:Npn \@@_special:n #1 {
\AtBeginShipout { \immediate \special { #1 } }
\immediate \special { #1 }
}
%% command to set the page attributes for pdftex/luatex
\cs_new:Npn \@@_pdfpageattr:n #1 {
\pdfpageattr { #1 }
}
%% make a variants to make sure we get the expaned content
\cs_generate_variant:Nn \@@_special:n { V }
\cs_generate_variant:Nn \@@_pdfpageattr:n { V }
%% [A] THIS DOES NOT WORK!
%% set the trim box according to engine
%\bool_if:nTF { \sys_if_engine_luatex_p: || \sys_if_engine_pdftex_p: } {
% \@@_pdfpageattr:V \g_@@_trim_pageattr_tl
%} {
% \sys_if_engine_xetex:TF {
% \usepackage{atbegshi}
% \@@_special:V \g_@@_trim_special_tl
% } { ERROR: UNKOWN MACHINE! }
%}
%% just a macro to show some debug info
\NewDocumentCommand { \debuginfo } { } {
\par\noindent
Machine: ~ \texttt{ \c_sys_engine_str }
\par\bigskip\noindent
special: \\
\mbox { \texttt { \g_@@_trim_special_tl } }
\par\medskip\noindent
pageattribute: \\
\mbox { \texttt { \g_@@_trim_pageattr_tl } }
}
\ExplSyntaxOff\makeatother %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% [B] THIS WORKS!
%% for pdftex and luatex
%\pdfpageattr { /TrimBox [8.50392 8.50392 532.91336 702.99211] }
%% for xetex
%\usepackage{atbegshi}
%\AtBeginShipout { \special { pdf: put @thispage << /TrimBox [8.50392 8.50392 532.91336 702.99211] >> } }
%\special { pdf: put @thispage << /TrimBox [8.50392 8.50392 532.91336 702.99211] >> }
\begin{document}
\section*{Test Document}
\debuginfo \clearpage Just another page \ldots
\end{document}
Result
Unfortunately my idea doesn’t work as expected.
When using the manual setup ([B]
in the code) everything is fine and the TrimBox is set up properly for PDFTeX, LuaTeX and XeTeX. But when using the auto generated token lists instead ([A]
in the code), it does not work: With XeTeX I get a PDF but without any TrimBox settings and with PDFTeX or LuaTeX the PDF file is corrupted and can’t be opened with a viewer even uncompressing it with cpdf
fails.
Question
Simple: What’s my mistake?
Notes
If this question leads to a robust code I would make a package out of it an publish it on CTAN.
While researching I found the following questions and want to make clear that this is not a duplicate question ;-)
- Embedding printer settings in pdf – as far as I understand
pdfprintclip
is only a vier setting but to be honest I did’t test it because I couldn’t figure out how to use it. - Problem with setting correct custom page size with 'geometry' in xelatex – is only about the visual page layout and not the PDF-TrimBox
- How to use macro inside \pdfpageattr – actually shows how to use a macro (aka dynamic values) for the TrimBox but I couldn’t transfer the answer to my LaTeX3 approach
- Producing PDF/X compliant document with XeLaTeX – contains only information about setting the box manually with XeTeX.
Furthermore the pdf
packages seems to be no help in this case. At least I can’t find anything about trimming in the manual …