Mail-merging documents from a database

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.

-
The datatool package can be used to process a CSV file, and then you can extract the appropriate pages from the PDF file. –  Peter Grill Jan 8 '13 at 0:40
See if this answers your question: Example of mailmerge package with an Excel spreadsheet. If it does, we can close your question as a duplicate. –  Alan Munn Jan 8 '13 at 0:42
@AlanMunn I updated the question based on your answer to the other question. The issue isn't so much getting the data from a file/database into the document but how to create many versions of the same document with only one page "mail-merged." –  tpg2114 Jan 8 '13 at 1:01
So you want to have e.g. a master file welcome.tex which includes Bob-letter.pdf and then outputs welcome-bob.pdf, etc. for each person? You don't want 1 document which includes separate letters for Bob, Sue, etc. I would be inclined to script that part. –  Alan Munn Jan 8 '13 at 1:07
@AlanMunn Yes, sort of. But based on your suggestion on how to create the letters, I won't have Bob-letter.pdf, I'll just have letters.pdf which has Bob's letter on some page. So the welcome.tex used to create welcome-bob.pdf would need to know the same database as letters.pdf in order to know which order the pages are in. If that makes sense... –  tpg2114 Jan 8 '13 at 1:10

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.

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\RB#1|#2|#3|#4|#5;{%
% 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}
}

\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
\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}
}

\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).

-

A simple solution is the textmerg package to obtain a single pdf, but that you can split with some external tool as pdfsam or pdftk. A MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{eurosym}
\usepackage{textmerg}
\begin{document}

\Merge{data.dat}{
\hspace{.25\textwidth}
\begin{tabular}{rl}
Address & {\bf \Mr\ \Name\  \SurName }\\
& \\
Subject & \Donation{} donation \\
\end{tabular}
\vspace{2 cm}
Dear \Mr \Name,  bla bla bla ....

\newpage
}
\end{document}


Where the merged file data.dat is a plain text where the field separators are the carriage returns as in this example:

Mr.
Peter
Smith
209 \\$

Ms.
Maria
Rossi
Regina Elena, 113, Milano, Italy
145 \euro

-