# examples using PDF::Template

Discussion in 'Perl Misc' started by botfood, Dec 7, 2004.

1. ### botfoodGuest

I have a possible project I am looking into that is basically creating
a "mail-merge" utility that would chew through a big CSV spreadsheet
and merge the fields into a template to generate a big .pdf document.
The reason the client wants to go .pdf is to have a "print ready"
format.

After poking through CPAN, it looks like PDF::Template and/or
PDF::Create are along these lines.... It is a little unclear to me how
involved it would be to set up the template that I would "merge" into.
There seem to be a LOT of parameters that would need to be set up in a
root document, and I am wondering if there is a reasonably easy way to
do this.

Like...
- Can I take an existing authored .pdf page and clone all the
margins settings and stuff like that?

- Can I author a template with any special tags where I want to plug
in my variable fields?

If you have any tips, tutorials, or examples illustrating these modules
that would help me determine if this is the way to go....

TIA,
Dan

botfood, Dec 7, 2004

Star date: Tue, 07 Dec 2004 12:50:58 -0800, botfood's log:

Dan,

I think you'll find this group a whole lot more helpful if you give it a
try and then when you hit a sticking point, post about that specifically
with code.

> I have a possible project I am looking into that is basically creating
> a "mail-merge" utility that would chew through a big CSV spreadsheet
> and merge the fields into a template to generate a big .pdf document.
> The reason the client wants to go .pdf is to have a "print ready"
> format.

You could also consider creating a format that can be turned into PDF. If
you play your cards right, you might be able to generate multiple versions
(on-line, print-ready, editable, etc.) from one source.

> After poking through CPAN, it looks like PDF::Template and/or
> PDF::Create are along these lines.... It is a little unclear to me how
> involved it would be to set up the template that I would "merge" into.
> There seem to be a LOT of parameters that would need to be set up in a
> root document, and I am wondering if there is a reasonably easy way to
> do this.
>
> Like...
> - Can I take an existing authored .pdf page and clone all the margins
> settings and stuff like that?

Can you take a PDF file and use it this way? Try and see.. I doubt it
highly.

> - Can I author a template with any special tags where I want to plug in
> my variable fields?

Yeah. There are a million template solutions and you can always write
your own. I like Text::Template because it is simple and it uses Perl as
the template language. Template::Toolkit is the hot one and it is
feature-filled... "simple" is not applicable.

> If you have any tips, tutorials, or examples illustrating these modules
> that would help me determine if this is the way to go....

Have you tried googling "PDF::create" to see if you can find any code that
uses it?

-Alan

3. ### Michele DondiGuest

On 7 Dec 2004 12:50:58 -0800, "botfood" <> wrote:

>I have a possible project I am looking into that is basically creating
>a "mail-merge" utility that would chew through a big CSV spreadsheet
>and merge the fields into a template to generate a big .pdf document.
>The reason the client wants to go .pdf is to have a "print ready"
>format.

Not really an answer to your question, but have you considered using
pdf(La)TeX for this purpose? Of course you can still use perl to build
the source...

Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<ZS(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$\=/^J/?$/:"\r";print,redo}#JAPH,

Michele Dondi, Dec 8, 2004
4. ### Scott BryceGuest

botfood wrote:

> - Can I take an existing authored .pdf page and clone all the
> margins settings and stuff like that?

Probably not.

> - Can I author a template with any special tags where I want to plug
> in my variable fields?

Probably not.

A PDF file includes a cross reference that has byte offsets to all of
the objects in the file. Any modifications in the PDF file will require
an update of the cross reference.

> If you have any tips, tutorials, or examples illustrating these modules
> that would help me determine if this is the way to go....

The first thing is to become familiar with the PDF spec. The spec has
been available on Adobe's web site. I can't find it right now. The file
name is PDFReference15_v6.pdf. (A subsequent update may have a different
file name.)

PDF files were not designed for easy modification, so trying to fill in
a template can be as much effort as creating a new PDF from scratch.

As for PDF::Template and PDF::Create, I don't use them. I couldn't find
documentation for them, and they do much more than I need them to. I
wrote my own module to create simple PDFs. In order to use that module,
one would have to be very familiar with portions of the PDF spec.

Try posting to comp.text.pdf and see if anyone there has some ideas.

Scott Bryce, Dec 8, 2004
5. ### botfoodGuest

botfood wrote:
> I have a possible project I am looking into that is basically

creating
> a "mail-merge" utility that would chew through a big CSV spreadsheet
> and merge the fields into a template to generate a big .pdf document.
> The reason the client wants to go .pdf is to have a "print ready"
> format.
> --------------------------

after a night of poking around, and you all's input, it looks like
creating .pdf files on the fly is a pretty involved and painful
process. PDF::Template has potential for what I want to do, but
requires learning special XML syntax to set up the template, and buying
the PDFlib for about $450. PDF::Create has some simple capabilities, but neither look straightforward to use in a mail-merge application as there are some complex issues with pagination and text position. Neither module had very complete docs, and I couldn't find any reasonably complete tutorials or examples online. I read thru the overview and some syntax of the actual .pdf spec on the adobe site... and while a super simple layout might be pretty easy to code, it would get REAL deep real fast. so.... just thought I'd post a followup in case somebody googles later. Its looking to me like its a lot easier to generate the output is a text, html, or other man-readable format and then use a cheap "print to pdf" convertor at the backend. if there is somebody out there who has actually used PDF::Create and/or PDF::Template and would like to share some thoughts on the capabilities/limitations, ease of use, examples, etc, I still think it would be great to share some knowledge of these packages. I realize that some people feel this group is for asking questions after you get stuck... but I HOPE there are also people out their interested in sharing experiences with specific packages to help others evaluate alternatives before investing a lot of learning time and test code. d botfood, Dec 8, 2004 6. ### Constantin WiemerGuest botfood wrote: > botfood wrote: > >>I have a possible project I am looking into that is basically > > creating > >>a "mail-merge" utility that would chew through a big CSV spreadsheet >>and merge the fields into a template to generate a big .pdf document. >>The reason the client wants to go .pdf is to have a "print ready" >>format. >>-------------------------- > > > after a night of poking around, and you all's input, it looks like > creating .pdf files on the fly is a pretty involved and painful > process. PDF::Template has potential for what I want to do, but > requires learning special XML syntax to set up the template, and buying > the PDFlib for about$450. PDF::Create has some simple capabilities,
> but neither look straightforward to use in a mail-merge application as
> there are some complex issues with pagination and text position.
> Neither module had very complete docs, and I couldn't find any
> reasonably complete tutorials or examples online.
>
> I read thru the overview and some syntax of the actual .pdf spec on the
> adobe site... and while a super simple layout might be pretty easy to
> code, it would get REAL deep real fast.
>
> so.... just thought I'd post a followup in case somebody googles later.
> Its looking to me like its a lot easier to generate the output is a
> text, html, or other man-readable format and then use a cheap "print to
> pdf" convertor at the backend.
>
> if there is somebody out there who has actually used PDF::Create and/or
> PDF::Template and would like to share some thoughts on the
> capabilities/limitations, ease of use, examples, etc, I still think it
> would be great to share some knowledge of these packages.
>
> I realize that some people feel this group is for asking questions
> after you get stuck... but I HOPE there are also people out their
> interested in sharing experiences with specific packages to help others
> evaluate alternatives before investing a lot of learning time and test
> code.
>
> d
>

Ghostscript to convert it to PDF? Postscript is probably easier to
create on the fly than PDF (not sure, not much experience here) and
Ghostscript works quite well for creating PDF files. I just checked and
there is a module PostScript::Simple on CPAN (among quite a few others)
which might do what you want.

It is also quite simple to create a Postscript "template" with
placeholders in it and then replace these placeholders with your actual
data using Perl. To create such a template you can use for example a
word processor (or spreadsheet or whatever) and save the document as
Postscript to a file. I did something like that to provide a legacy
application with nicely formatted reports.

Hope this helps.

Regards,

Constantin

Constantin Wiemer, Dec 9, 2004
7. ### botfoodGuest

wow,

hey, this may be exactly what I need! The formatting looks simple, and
I think the printer will accept either PostScript or eps files. Looks
like a much cleaner solution than .pdf !

mucho gracias,
d

botfood, Dec 9, 2004
8. ### botfoodGuest

....after looking at what available, I'm a little confused about what to
do next. I have an old version of perl from Activestate that I use for
development (5.005 build 522) as a lowest common denominator. I am not
SURE what version my usual webhost as installed, but its probably 5.6
or 5.8 by now?

anyway, the issue is that while PostScript::Simple is listed on CPAN,
it is NOT in the activestate download ppm. there is a similar package
PostScript, but only compiled for perl 5.8 and by a different author
with different methods.

so.... I am unsure which way to go that would allow me to code and test
at home on my PC and have the most likely chance of asking my webhost
to install if they haven't already.

PostScript from activestate, or PostScript::Simple from CPAN ???? and
how do I get either of them running on my PC under old perl without
having compilers and all that?

botfood, Dec 9, 2004
9. ### NubaGuest

botfood <> wrote:
: I think the printer will accept either PostScript or eps files. Looks
: like a much cleaner solution than .pdf !

Then maybe it's worth looking at the Template Toolkit
(namespace Template at CPAN)

http://www.template-toolkit.org/examples/ps/index.html
http://www.template-toolkit.org/info.html

--
,::::,
:: : Nuba Rodrigues Princigalli _____________
:. O-O: | '\\\\\\
: > Web Design and Development | ' ____|_
____\ - ._. Computational Mathematics | + '||::::::
/' \ -::' .' | '||_____|
/. ,- O < \ www.nuba.tk '________|_____|
(\ <' ' |\ / '-.___ ___/____|___\___
_.\__\________\_\___/), |\\____________________| _ ' <<<:|
|_________'___o_o|

Nuba, Dec 9, 2004
10. ### Michele DondiGuest

On 8 Dec 2004 09:16:04 -0800, "botfood" <> wrote:

>after a night of poking around, and you all's input, it looks like
>creating .pdf files on the fly is a pretty involved and painful
>process. PDF::Template has potential for what I want to do, but
>requires learning special XML syntax to set up the template, and buying
>the PDFlib for about $450. PDF::Create has some simple capabilities, >but neither look straightforward to use in a mail-merge application as >there are some complex issues with pagination and text position. [snip] >if there is somebody out there who has actually used PDF::Create and/or >PDF::Template and would like to share some thoughts on the >capabilities/limitations, ease of use, examples, etc, I still think it >would be great to share some knowledge of these packages. Well, I can't share any experience on the packages you mention, but I may try insisting on the pdfLaTeX route. See this fictious example: #!/usr/bin/perl use strict; use warnings; die "Usage:$0 <template>\n" unless @ARGV == 1;

open my $fh, '|-', 'pdflatex' or die "Can't pipe into pdfLaTeX:$!\n";
select $fh; while (<>) { s/ <<CODE>> / \\lstinputlisting{$0} /x;
print;
}

__END__

called with the following template as argument

\documentclass[a4paper,12pt]{article}

\title{A Minimal Example}
\author{Me Myself}
\date{\today}

\usepackage{color}
\usepackage[bookmarks=true,
bookmarksnumbered=true,
pdfpagemode=None,
pdfstartview=FitH]{hyperref}
\makeatletter
\newcommand\org@maketitle{}
\let\org@maketitle\maketitle
\def\maketitle{%
\hypersetup{
pdftitle={\@title},
pdfauthor={\@author},
pdfsubject={Nothing more than a minimal example},
pdfkeywords={minimal example}
}%
\org@maketitle
}
\makeatother
\usepackage{listings}

\begin{document}

\maketitle

\lstset{language=Perl,
basicstyle=\small\ttfamily,
stringstyle=\color{blue},
keywordstyle=\color{red},
backgroundcolor=\color[gray]{0.80},
xleftmargin=2em,
frame=single,
framexleftmargin=2em,
showstringspaces=no,
numbers=left,
numberstyle=\tiny,
numbersep=2em}

This is just a minimal example{\ldots} here's the code I used to
generate
this document from a template:

<<CODE>>

\end{document}

Running the perl program will leave a texput.pdf output file in the
cwd.

If you want to see a sample output, you can find it at
<http://mosquito.scumm.it/~blazar/example.pdf>.

Of course all this is very rough, and I remember having seen more
refined examples somewhere, however I don't have any link offhand.
IIRC someone even set up a TeX sort of daemon especially aimed at
"this kinda things"...

>I realize that some people feel this group is for asking questions
>after you get stuck... but I HOPE there are also people out their
>interested in sharing experiences with specific packages to help others
>evaluate alternatives before investing a lot of learning time and test
>code.

Hope the above helps.

Of course the solution I'm proposing requires having a TeX system
installed, which is not a problem for me, since I need it anyway...

Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB=' ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<ZS(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$\=/^J/?$/:"\r";print,redo}#JAPH,

Michele Dondi, Dec 9, 2004