examples using PDF::Template

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

  1. botfood

    botfood Guest

    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
    #1
    1. Advertising

  2. botfood

    Alan Mead Guest

    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
     
    Alan Mead, Dec 8, 2004
    #2
    1. Advertising

  3. 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<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 8, 2004
    #3
  4. botfood

    Scott Bryce Guest

    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
    #4
  5. botfood

    botfood Guest

    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
    #5
  6. 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
    >


    What about creating your document in Postscript and then using
    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
    #6
  7. botfood

    botfood Guest

    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 !

    I just KNEW that somebody had been down this road before...
    mucho gracias,
    d
     
    botfood, Dec 9, 2004
    #7
  8. botfood

    botfood Guest

    ....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
    #8
  9. botfood

    Nuba Guest

    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)

    Some links:
    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
    #9
  10. 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,
    colorlinks=true,
    linkcolor=blue,
    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,
    commentstyle=\color{white},
    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}


    \section{What is all this about?!?}

    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<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 9, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vasudevram
    Replies:
    0
    Views:
    551
    vasudevram
    Jul 22, 2006
  2. vasudevram
    Replies:
    0
    Views:
    347
    vasudevram
    Oct 27, 2006
  3. Ricardo Pog
    Replies:
    1
    Views:
    437
    Austin Ziegler
    Mar 26, 2008
  4. Sean Nakasone
    Replies:
    1
    Views:
    384
    Farrel Lifson
    Apr 14, 2008
  5. alwaysonnet

    PDF::Template - Create cascade PDF

    alwaysonnet, Jun 2, 2010, in forum: Perl Misc
    Replies:
    0
    Views:
    215
    alwaysonnet
    Jun 2, 2010
Loading...

Share This Page