LaTeX-Easy-Templates

 view release on metacpan or  search on metacpan

lib/LaTeX/Easy/Templates.pm  view on Meta::CPAN

      fullname => 'Gigi Comp',
      addresslines => [
        'Apt 5',
        '25, Jen Way',
        'Balac'
      ],
      postcode => '1An34',
      # this assumes that ./templates/images/logo.png exists, else comment it out:  
      logo => $logo_filename,
    };
    my @labels_data = map {
      {
        recipient => {
          fullname => "Teli Bingo ($_)",
          addresslines => [
            'Apt 5',
            '25, Jen Way',
            'Balac'
          ],
          postcode => '1An34',
        },
        sender => $sender,
      }
    } (1..42); # create many labels yummy

    my $latter = LaTeX::Easy::Templates->new({
      'debug' => {
        'verbosity' => $verbosity,
        'cleanup' => $cleanup
      },
      'processors' => {
        'custom-labels' => {
        'template' => {
          'filepath' => $template_filename,
        },
        'latex' => {
          'filepath' => 'xyz.tex',
          'latex-driver-parameters' => {
            'format' => $latex_driver_and_format,
          }
        }
        },
      }
    });
    die "failed to instantiate 'LaTeX::Easy::Templates'" unless defined $latter;

    my $ret = $latter->format({
      'template-data' => \@labels_data,
      'output' => {
        'filepath' => $output_filename,
      },
      'processor' => 'custom-labels',
    });
    die "failed to format the document, most likely latex command has failed." unless defined $ret;
    print "$0 : done, output in '$output_filename'.\n";

This is the result in very low resolution:

=begin HTML

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDABoSExcTEBoXFRcdGxofJ0AqJyMjJ084PC9AXVJiYVxSWllndJR+Z22Mb1laga+CjJmepqemZHy2w7ShwZSjpp//wgALCADdAlgBAREA/8QAGQABAQEBAQEAAAAAAAAAAAAAAAECAwQF/9oACAEBAAAAAfpgAAAAAAAAAAABAAAUBAAAKAgAAKSgc...

=end HTML

=head1 EXAMPLE: NESTED PERL DATA STRUCTURES TO PDF

Thanks to the amazing work put in L<Text::Xslate>
one can have access to user-defined Perl functions,
Perl modules and macros from inside a template file.

This allows recusrsion which makes possible walking and
printing a nested Perl data structure with this
simple template:

    %templates/nested-data-structures/nested-data-structures.tex.tx
    \documentclass[12pt]{article}
    \begin{document}

    : macro walk -> $d {
    :   if( ref($d) == 'ARRAY' ){
    $\lbrack$
    :     for $d -> $item {
    :       walk($item);
    :     }
    $\rbrack,$
    :   } elsif( ref($d) == 'HASH' ){
    $\{$
    :     for $d.kv() -> $pair {
            <: $pair.key() :> $=>$
    :       walk($pair.value())
    :     }
    $\},$
    :   } elsif( ref($d) == '' ){
          <: $d :>,
    :   } else {
          beginUNKNOWN <: $d :> endUNKNOWN
    :   }
    : } # macro

    <: walk($data) :>

    \end{document}

First we create a macro which walks the input data structure
and recurses into it until a scalar is found.

The function C<ref()> is Perl's builtin but it is not available
from inside an L<Text::Xslate> template. So, we create our own
function for doing this and pass it on to the L<Text::Xslate>'s
constructor, as was demonstrated previously with the
C<templater-parameters> hash pass to L<LaTeX::Easy::Templates>'s
L<constructor|<new()>.

Here is a Perl script to render any data structure into PDF:

    use strict;
    use warnings;

    use LaTeX::Easy::Templates;
    use FindBin;



( run in 1.308 second using v1.01-cache-2.11-cpan-5b529ec07f3 )