# Set PDF/A properties using filecontents package

I would like to provide the follwing class:

\ProvidesClass{pdfatest}[2018/04/12 V0.1 PDF/A Test]

\newcommand{\cnumber}[1]{\def\@cnumber{#1}}
\newcommand{\cauthor}[1]{\def\@cauthor{#1}}

% Set PDF/A properties using pdfx and filecontens packages
\usepackage[a-3b]{pdfx}
\RequirePackage{filecontents}
\begin{filecontents}{\jobname.xmpdata}
\Title{Certificate of Calibration No \@cnumber} % not working because \@cnumber not defined
\Author{\@cauthor} % not working because \@cauthor not defined
\end{filecontents}


The problem is that I can't set the \Title and \Author inside the filecontents block when I'm using variables \@....

Using \hypersetup inside \AtBeginDocument I was able to set the properties, see the following code:

% Set PDF properties using the hyperref package.
% Not used, because it's not working with PDF/A.
\usepackage{hyperref}
\AtBeginDocument{
\hypersetup{
pdftitle=Certificate of Calibration No \@cnumber,
pdfauthor=\@cauthor
}
}


I tried as well to put filecontents block inside \AtBeginDocument. But I get an Undefined control sequence error if I do that.

I'm using my class for example like that:

\documentclass{pdfatest}

\cnumber{123-45678}
\cauthor{My Name}

\begin{document}
Bla bla ...
\end{document}


Any ideas how I can use the filecontents block with variables in my class definition?

The pdfx package should be loaded at the end of the preamble, this prevents \Title etc. being undefined when \begin{filecontents}...\end{filecontents} is used.

So load the etoolbox package and wrap the loading of pdfx into \AtEndPreamble.

The \@c.... macros are written unexpanded anyway and only expanded at the time of writing the meta data.

\ProvidesClass{pdfatest}[2018/04/12 V0.1 PDF/A Test]

\newcommand{\cnumber}[1]{\def\@cnumber{#1}}
\newcommand{\cauthor}[1]{\def\@cauthor{#1}}

% Set PDF/A properties using pdfx and filecontens packages
\RequirePackage{filecontents}

\RequirePackage{etoolbox}

\AtEndPreamble{
\RequirePackage[a-3b]{pdfx}
}
\begin{filecontents}{\jobname.xmpdata}
\Title{Certificate of Calibration No \@cnumber} % not working because \@cnumber not defined
\Author{\@cauthor} % not working because \@cauthor not defined
\end{filecontents}

\endinput