[Tex/LaTex] Mail-merging documents from a database

databasepackages

I use the term database loosely here — it can be an actual database, or a spreadsheet (in any format including CSV) or can be package-dependent.

Is there a way to create mail-merged documents similar to what can be done in word processing packages? I found the mailmerge package which sort of works but it puts all the mail-merged data into one document.

Is there a package or way to generate new, individual documents for each part of the database? In other words, if I have a firstName value in whatever storage system, can I get LaTeX to generate a file called <firstName>.dvi for each of the names in the database? Or do I need to resort to a scripting language driver to pull that off?

EDIT

So to further explain what I am trying to do (and will now set it up based on the answer to Change document class per page ) :

I'm trying to create a "welcome to the company" sort of document that has a cover page, table of contents, welcome letter, then various sections of information. I've never tried this in anything other than a word processor where it's easy to set "master pages" or "template pages" for individual pages in the document. So I could use a letterhead template page for the welcome letter part and a standard article-type template for the rest and still have the letter numbered/referenced in the ToC.

If I use the approach on the answer there, how can I tell \pdfpages which page to include? Ultimately I would like to have many files like Bob.pdf, Sue.pdf, John.pdf etc. where each file is the same document with a custom letter embedded within.

Best Answer

You can build your TeX database, based on your needs simply by creating a list to hold the list of persons you sending your mailing. We will create a list with fields as shown in the table below, will also keep it sorted to make life easier.

enter image description here

This is done by creating first an empty list \let\alist\@empty

The format of the input file, is best to be in TeX format, to make the programming easier and to enable the use of a comma in the addresses. I have used the following format for capturing the fields, but please feel free to use your own:

\RB Nisbet|John|Mr.| 235, Highlands, Scotland | United Kingdom;
\RB Guevara|Che|Dr.| 527 Main Street, Havana | Cuba;
\RB Zapata|Emiliano|Mr.| 5237, Mexico City | Mexico;
\RB von Kleist-Schmenzin|Herr| Ewald| Greens, Dubberow| Germany;

Names are delimited by "|" and an ending semicolon. The standard part of the letter is on a separate file called stdletter.dat. I have generated it on the fly using the filecontents package. The MWE example is shown below.

\documentclass{article}
\usepackage{lstdoc,booktabs,filecontents}
\begin{filecontents}{stdletter.dat}
We are happy to enclose our new catalogue.
\vspace{20pt}

Regards,
\vspace{20pt}

Some CEO
\end{filecontents}
\begin{document}
\pagestyle{plain}
\makeatletter
\let\alist\@empty
\let\blist\@empty

\def\addtolist#1#2{%
  \lst@lAddTo#1{#2}
}

\def\addtolist#1#2{%
  \lst@lAddTo#1{#2}
}


\def\RB#1|#2|#3|#4|#5;{%
   \addtolist{\alist}{#1#2,}% 
   % macro for table
   \expandafter\gdef\csname#1#2@table\endcsname{\textit{#1}&#2&#3&#4\cr\relax}
   % macro for salutation
   \expandafter\gdef\csname#1#2@salut\endcsname{Dear #3 #2\relax}
   \lst@BubbleSort{\alist}
}

%% adding the data now
\RB Nisbet|John|Mr.| 235, Highlands, Scotland | United Kingdom;
\RB Guevara|Che|Dr.| 527 Main Street, Havana | Cuba;
\RB Zapata|Emiliano|Mr.| 5237, Mexico City | Mexico;
\RB von Kleist-Schmenzin|Herr| Ewald| Greens, Dubberow| Germany;

%% typesetting the table
\def\addresslist{%
\newsavebox{\tempbox}
\savebox{\tempbox}{
\centering
\begin{tabular}{llll}
  \toprule[1pt]
  First Name & Second Name & Salutation & Address\\
  \midrule
  \@for\i:=\alist \do{\csname\i @table\endcsname}
  \vspace{-14pt}\\\bottomrule
\end{tabular}}

\begin{table}
\usebox{\tempbox}
\caption{Client List}
\end{table}
}


\addresslist

\mbox{}\newpage

\@for\i:=\alist \do{\csname\i\endcsname
\csname\i \endcsname
\expandafter\csname \i @salut\endcsname
\par\medskip
\input{stdletter.dat}

\pagebreak
}

\makeatletter

\end{document}

The "article" part can be created the same way or preferably printed separately and resetting page numbering (much easier).

Related Question