NAME

PDF::Collage::TemplatesCollection - Multiple templates for PDF::Collage

SYNOPSIS

use PDF::Collage::TemplatesCollection;
my $pctc = PDF::Collage::TemplatesCollection->new(%args);

# the collection has a list of `selectors`
say for sort { $a cmp $b } $pc->selectors;

# with a selector we can get a specific template
my $tmpl = $pctc->get($selector); # PDF::Collage::Template
my $pdf = $tmpl->render(\%data);
$pdf->saveas('/path/to/somefile.pdf');

# we can also render in one sweep
my $pdf = $pctc->render($selector, \%data);

# we can even leverage a default if there's only one selector
my $pdf = $ctc->render(\%data);

DESCRIPTION

This module implements a class that allows loading a collection of templates (each being a sequence of commands for basic PDF manipulation) and then "get" or "render" any of these templates over and over, based on different data for filling in the form.

The input is a Data::Resolver instance passed at construction time as "resolver", wrapping some data:

  • template definitions, i.e. data structures that are compatible with instantiating PDF::Collage::Template objects;

  • additional artifacts, most probably one or more PDF files that can be used to take pages to assemble the target PDF.

The definitions are supposed to be JSON file matching keys \A(?:\./)?definitions/(.*)\.json\z. The part in the capture group is one of the available "selectors".

INTERFACE

This module has an object-oriented interface.

Constructor

This class supports one constructor.

new

my $pct = PDF::Collage::Template->new(%args);
my $pct = PDF::Collage::Template->new(\%args);

Build a new object. The input arguments can be key-value pairs or a has reference, supporting key resolver to set the "resolver",

Returns a PDF::Collage::TemplatesCollection object.

Accesssors

This class supports the following accessor.

resolver

my $data_resolver_instance = $obj->resolver;

Retrieve the Data::Resolver instance underpinning the object.

Methods

This class supports the following methods.

contains

my $bool = $obj->contains($selector);

Check if the specific $selector is available.

get

my $tmpl = $obj->get($selector);

Get the PDF::Collage::Template object associated to the provided $selector.

render

my $pdf = $obj->render($selector, \%data);
my $pdf = $obj->render(\%data); # only if there's a single template

Wrapper to render a template, without retrieving it beforehand.

If the collection only contains one single selector, it's possible to skip passing it to this method.

Returns a PDF::Builder object.

selectors

my @list = $obj->selectors;

Get the list of available selector strings.

AUTHOR

Flavio Poletti <flavio@polettix.it>

COPYRIGHT AND LICENSE

Copyright 2023 by Flavio Poletti <flavio@polettix.it>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.