Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

labels with RTF (porst from Nikolai Chuvakhin)

142 views
Skip to first unread message

DXTrim

unread,
Sep 6, 2003, 7:26:53 AM9/6/03
to
Greetings guys,

According to a post from Nikolai Chuvakhin it is possible to generate
dynamically Avery mailing labels using RTF.

Does anyone (or maybe Nikolai, if you are around) can point me into the
right direction to doing this?. I'm not very experienced with it.

The data for the labels should be dynamically retrieved using a MySQL db
query, but I can't figure out how to make them in rtf so the client can
print them.

Any help heartly appreciated.

DXtrim


Good Man

unread,
Sep 6, 2003, 3:38:50 PM9/6/03
to
"DXTrim" <*delete_nospam*al...@yahoo.com> wrote in news:3f59c46c$0$13861
$ba62...@reader0.news.skynet.be:


what about going PDF route?

Nikolai Chuvakhin

unread,
Sep 6, 2003, 4:57:08 PM9/6/03
to
"DXTrim" <*delete_nospam*al...@yahoo.com> wrote
in message news:<3f59c46c$0$13861$ba62...@reader0.news.skynet.be>...

>
> According to a post from Nikolai Chuvakhin it is possible to generate
> dynamically Avery mailing labels using RTF.
>
> Does anyone (or maybe Nikolai, if you are around) can point me into the
> right direction to doing this?. I'm not very experienced with it.

I am very much around, thank you. :)

> The data for the labels should be dynamically retrieved using a MySQL db
> query, but I can't figure out how to make them in rtf so the client can
> print them.

Here's the approach I use, since I am too lazy to properly learn
the RTF specifications:

1. Generate an empty page of Avery labels you want wo use
(I do this with Microsoft Word).
2. Reduce the page to one row of labels (that is, delete all
rows except one).
3. Put non-identical placeholders into the cells of the remaining
single row. For example, if your template has three labels per
row, your template will look something like this:

Name1 Name2 Name3
Address1 Address2 Address3
City1 City2 City3

The number of lines within a cell/label should correspond to
the number of lines in your desired final product. For example,
you may want to do domething like this instead of the above:

Name1 Name2 Name3
Address1 Address2 Address3
City1 City2 City3
Country1 Country2 Country3

4. Edit the layout of the labels (say, the default font is Times
New Roman 10 pt, but the Name placeholders should be Times New
Roman Bold 11 pt) and save the template as an RTF file.
5. Open the RTF file in a plain-text editor and break it into three
parts: header (everything before a "\trowd" instruction), the row
of labels (the part that starts with a "\trowd" instruction and
ends with a "\row" instruction) and footer (everything after
the "\row" instruction). Save all three parts as separate files
(let's call them h.txt, r.txt, and f.txt).

That's it with preparations. Now you need to actually write the
script. Let's say, you have a MySQL table called 'clients' with
the following fields:

firstname
lastname
street
city
postcode
country

You need the data from all these fields to appear on a mailing label.
Simple enough:

// assume that we already connected to the server and
// selected the database; further assume that no output
// had yet taken place
$query = 'SELECT firstname, lastname, street, ' .
'city, postcode, country FROM clients';
$result = mysql_query ()
if (mysql_num_rows ($result) == 0) {
die ('<p>Sorry, no records found');
}
header ('Content-type: application/msword');
// alternatively, you may try
// header ('Content-type: application/rtf');
header ('Content-Disposition: inline; filename: labels.rtf');
readfile ('h.txt');
// remember the header file we made?
$col = 1;
$row = file_get_contents ('r.txt');
// remember the row file we made?
$newrow = $row;
while ($label = mysql_fetch_array ($result)) {
$newrow = str_replace ('Name'.$col,
$label['firstname'] . ' ' . $label['lastname'],
$newrow);
$newrow = str_replace ('Address'.$col, $label['street'], $newrow);
$newrow = str_replace ('City'.$col,
$label['postcode'] . ' ' . $label['city'],
$newrow);
// Note that I am using the Dutch addressing system, with
// postcode preceding city name, in some countries, the
// convention is to do the reverse...
$newrow = str_replace ('Country'.$col, $label['country'], $newrow);
if ($col >= 3) {
echo $newrow;
$col = 1;
$newrow = $row;
} else {
$col++;
}
}
// Now we have run out of data; however, it is possible that
// we have one or more labels in the current row that still
// contain placeholders; we want to get rid of them now:
if ($col <> 1) {
for ($i = $col; $i <= 3; $i++) {
$newrow = str_replace ('Name'.$i, ' ', $newrow);
$newrow = str_replace ('Address'.$i, ' ', $newrow);
$newrow = str_replace ('City'.$i, ' ', $newrow);
}
echo $newrow;
}
readfile ('f.txt');
// remember the footer file we made?

Basically, this script outputs the RTF header, a number of rows of
labels, and finally, the RTF footer, to form a complete RTF document.
Since the template was generated for the specific Avery product,
there's no need to worry about pagination.

That's it, really. If you have any questions, send me an e-mail.

Cheers,
NC

Nikolai Chuvakhin

unread,
Sep 6, 2003, 9:08:35 PM9/6/03
to
n...@iname.com (Nikolai Chuvakhin) wrote in message
news:<32d7a63c.03090...@posting.google.com>...

>
> Here's the approach I use, since I am too lazy to properly learn
> the RTF specifications:
...
> if ($col >= 3) {
...

> for ($i = $col; $i <= 3; $i++) {

Forgot to mention that number 3 reflects the number of columns on
the sheet of labels; if you are using the layout with a different
number of columns, you should change this accordingly.

Cheers,
NC

Nikolai Chuvakhin

unread,
Sep 6, 2003, 9:19:38 PM9/6/03
to
Good Man <he...@letsgo.com> wrote in message
news:<Xns93EE9F2BC9...@206.127.4.10>...

>
> > According to a post from Nikolai Chuvakhin it is possible
> > to generate dynamically Avery mailing labels using RTF.
>
> what about going PDF route?

This is viable, too, but I think RTF is much easier. If you look
at my post in this thread, you will see that using Word-generated
label templates in RTF allowed me to produce a set of mailing
labels without even knowing their size or worrying about pagination.
All I need is the Avery product number... Also, in some (but, of
course, not all) circumstances having an editable output can be
considered desirable.

Cheers,
NC

DXTrim

unread,
Sep 7, 2003, 9:26:35 AM9/7/03
to
Hi Good Man,

PDF requires me PDFLib and the client is used to her Word stuff.

Thanks anyway for your response,

Cheers,

DXTrim

"Good Man" <he...@letsgo.com> wrote in message
news:Xns93EE9F2BC9...@206.127.4.10...

DXTrim

unread,
Sep 7, 2003, 10:06:55 AM9/7/03
to
Dear Nikolai,

Thank you, thank you very much. I have spent three hours trying to make it
work with no success. With one row works perfectly but when the row has to
repeat I get an error so I can't open the doc in Word. After many efforts I
realised that the spliting of the file in the text Editor is wrong and I
can't manage to make it work. The problem arises with more than 1 row.

I splitted before the \twrod stuff as you instructed, but I'm afraid there
are some curly braces { that are giving troubles.

If you could help me out I'd really appreciate it.

Thank again, this has been a great help already.

Keep well,


DXTrim


"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03090...@posting.google.com...

DXTrim

unread,
Sep 7, 2003, 10:32:08 AM9/7/03
to
Hi Nikolai,

It's OK, just that the row part starts with { has an extra \trowd in the
middle and ends with \row without the brace. I just pasted the source in
Word and highlighted all the instructions and saw it. It's working like a
charm... PICOBELLO !!!!

Bless you man, I owe you a beer.

DXTrim


"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03090...@posting.google.com...

Nikolai Chuvakhin

unread,
Sep 7, 2003, 3:31:22 PM9/7/03
to
"DXTrim" <*delete_nospam*al...@yahoo.com> wrote
in message news:<3f5b4155$0$443$ba62...@reader2.news.skynet.be>...

>
> It's working like a charm...

Glad you liked it. :)

> Bless you man, I owe you a beer.

No, you don't.

Cheers,
NC

0 new messages