Background package command \backgroundsetup not working in environment

I am currently designing an application template for myself and maybe some colleagues and I have a little trouble with \backgroundsetup.

The structure of the document I am aiming for shall look as follows

    \begin{document}

\begin{coverletter}
some text
\end{coverletter}

\begin{application}
some text
\end{application}

and so on

\end{document}


In each environment, I would like to set the background based on a pdf-file. However, \backgroundsetup does not work if I call it inside either \newenvironment or \NewEnviron.

Can anyone help me to fix that?

Here a minimum example

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{environ}

\usepackage[pages=all]{background}
\backgroundsetup{
scale=1,
opacity=1,
angle=0
}

\newcommand*{\setbackground}[1]{\backgroundsetup{contents={\includegraphics[height=303mm, width=216mm]{#1}}}}

\NewEnviron{coverletter}{\setbackground{bg}\BODY}
%\setbackground{bg}

\begin{document}

\begin{coverletter}
This is a test
\end{coverletter}

\end{document}


bg is the background image and should work with any DinA4 pdf document.

• Most likely the change from \backgroundsetup gets lost inside the group formed by the environment -- it is unknown then when the page is shipped out
– user31729
Dec 14 '17 at 11:40
• Is \NewEnviron{coverletter}{\setbackground{ente}\BODY}[\clearpage] acceptable for you?
– user31729
Dec 14 '17 at 11:45

After some tinkering, I finally ended up with the following code. Just in case someone wants to do something similar in the future.

\newcommand{\topbar}{\draw[fill=dark, draw=none] (0,0) rectangle (216mm,-83mm);}
\newcommand{\narrowtopbar}{\draw[fill=dark, draw=none] (0,0) rectangle (216mm,-23mm);}
\newcommand{\sidepanel}{\draw[fill=light, draw=none] (13mm,-23mm) rectangle (83mm,-280mm);}
\newcommand{\footer}{\draw[fill=light, draw=none] (0mm,-303mm) rectangle (216mm,-290mm);%
\filldraw [fill=dark, draw=none] (184mm,-292.5mm)  coordinate (GeneralStart) -- ++(0,0) -- ++(+3.2,0) -- ++(0,-1.05) -- ++(-4.0,0) -- cycle;}

\newcommand{\setbg}[1]{%
\AtBeginPage{%
\begin{tikzpicture}[remember picture,overlay]
\node[xshift=-108mm, yshift=151.5mm] at (current page.center)
{%
\begin{tikzpicture}[remember picture, overlay]%
#1
\end{tikzpicture}
};
\end{tikzpicture}
}}


I can then call \setbg{\topbar\sidepanel\footer}\clearpage to set my page-background. Note, that the \AtBeginPage-command is provided by the bophook-package.