1.0.0-beta.4 • Published 7 years ago

yipt v1.0.0-beta.4

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

Yipt: Yipt is PDF Template

Yipt is a YAML based template engine generates PDF document.

Yipt uses PDFKit for PDF generation and JSONata for template variables.

Install

npm i yipt

Usage

CLI

yipt [options] <template> [output]

options

args

Example

yipt -p params.yml my-template.yml out.pdf

in JavaScript or TypeScript

import yipt from "yipt";
import * as PDFKit from "pdfkit";

const doc = new PDFKit();
const templateVariables = { foo: "template variables" };

yipt.render(doc, "/path/to/template.yml", templateVariables);
// yipt.render returns Promise<void>

It's also possible to create new Yipt instance.

import { Yipt } from "yipt";

const yipt = new Yipt();

Variable evaluation

Yipt supports template variables. Many of parameters in template are evaluated with template variables using JSONata.

Values in template other than text value such as content property of TextElement, are evaluated as a JSONata expression. Of course, you can also specify raw values instead of JSONata expressions for properties. Types of properties documented in this doc, are types of evaluated value.

On the other hand, in text, JSONata expression is wrapped with "{" and "}". For escape "\" is available.

Example

template:

yipt:
  version: "1.0"
  content:
    - type: text
      top: myVariableForTop
      left: myObject.left
      content: This text placed on (myObject.left, myVariableForTop).

variables:

{
  myVariableForTop: 10,
  myObject: { left: 20 },
}   

Then, "This text placed on (myObject.left, myVariableForTop)." is rendered at (20, 10).

template:

yipt:
  version: "1.0"
  content:
    - |
    Hello {foo.bar}! You can also use JSONata expression a + 1 = {a + 1}.
    \{but this never evaluted} because \{ is escaped.
    Then "\\\{" and "\\\\" is rendered as "\{" and "\\".

variables:

{
  foo: { bar: "world" },
  a: 2
}

output:

    Hello world! You can also use JSONata expression a + 1 = 3.
    {but this never evaluted} because { is escaped.
    Then "\{" and "\\" is rendered as "{" and "\".

Template

Absatract

Yipt template is a YAML file whose root object has yipt property, which is a Yipt object. And Yipt object has content property, which is an array of Element object or string. If element of content is a string, it is treated as TextElement.

Yipt renders Elements in order of array.

Element object is one of:

Element nameDescription
TextElementRenders text.
ImageElementRenders JPEG/PNG image.
VectorElementRenders vector graphics.
FontElementSetup font for text.
PageBreakElementInsert page break.
BlockElementGroups elements and control offset.
CaseElementLogical flow control in similar way to SQL's CASE syntax.
ForeachElementLoop flow control.

Spec

Yipt Object

PropertyTypeDescription
versionstringfixed "1.0". Version of template format.
contentContent[]List of content.
Content

Content is one of Element or string.

TextElement

PropertyTypeDescription
contentstringThe text to print with variable reference.
topnumberThe position in y axis.
leftnumberThe position in x axis.
optionsTextOptionsOptions for text rendering.
TextOptions

See Text in PDFKit for more detail.

ImageElement

PropertyTypeDescription
srcstringPath to image(JPEG/PNG) file.
topnumberThe position in y axis.
leftnumberThe position in x axis.
optionsImageOptionsOptions for text rendering.
ImageOptions

See Images in PDFKit for more detail.

VectorElement

PropertyTypeDescription
pathsVectorPath[]Ordered array of vector paths.
topnumberThe position in y axis.
leftnumberThe position in x axis.
render"stroke" | "fill" | "fillAndStroke"How render the path.
lineWidthnumberLine width. lineWidth 0 means hair line.
cap"butt" | "round" | "square"Line cap.
join"miter" | "round" | "bevel"Line join.
dashDashParameterLine dash style.
colorstringLine color. It allows a hex color string, or use any of the named CSS colors.
fillColorstringFill color. It allows a hex color string, or use any of the named CSS colors.
windingRule"even-odd" | "non-zero"Winding rules define how a path is filled.
opacitynumberOpacity of vector graphic. It value could be 0 to 1.
VectorPath

Type property of VectorPath could be one of:

  • line
  • verticalLine
  • horizontalLine
  • quadraticCurve
  • bezierCurve
  • move
  • rect
  • roundedRect
  • ellipse
  • circle
  • polygon

The first 5 types(lines and curves) use end point of previous path as start point.(this means they connect previous path). Use move for change start point of them if you want. (0, 0) is used as start position if one of them placed in the first of paths array. Please note that end points of the last 5 types(shapes) is undefined.

VectorPath object has additional properties for each types.

doc.y is set to maximum y of graphics after rendering vector.

line
PropertyTypeDescription
tonumber, numberLine end point in x, y.
verticalLine & horizontalLine
PropertyTypeDescription
lengthnumberLine length.
quadraticCurve
PropertyTypeDescription
tonumber, numberCurve end point in x, y.
anchornumber, numberAnchor point in x, y.
bezierCurve
PropertyTypeDescription
tonumber, numberCurve end point in x, y.
anchorsnumber, number, number, numberAnchor points in x1, y1, x2, y2.
move
PropertyTypeDescription
tonumber, numberMove to x, y.
rect
PropertyTypeDescription
posnumber, numberPosition of rectangle in left, top.
roundedRect
PropertyTypeDescription
posnumber, numberPosition of rectangle in left, top.
cornerRadiusnumberRadius of rounded corner.
ellipse
PropertyTypeDescription
centernumber, numberCenter coordinate in x, y.
radiusXnumberRadius of x axis.
radiusYnumberRadius of y axis.
circle
PropertyTypeDescription
centernumber, numberCenter coordinate in x, y.
polygon
PropertyTypeDescription
verticesnumber[]Coordinates of vertices in x1, y1, x2, y2, ...

FontElement

You can set or register founts using FontElement. Once fount is configured, it affects all text rendering (even where out of BlockElement) until another FontElement configures font.

PropertyTypeDescription
sizenumberSet font size.
namestringFont name to be used. Standard font name or registered font name.
srcstringPath to font file for register or temporally use.
familystringFont family for TrueType Collection or Datafork TrueType font file. This property is only used with src property.
registerAsstringRegister font as this value name. This property requires src property.
setOnRegisterbooleanWhether setting font on register or not. This property is only used with registerAs.

Default is false unless size property is set together.

PageBreakElement

PropertyTypeDescription
optionsPageOptionsOptions for new page after page break.

NOTE: PageOptions is never evaluated with variables.

PageOptions
PropertyTypeDescription
sizenumber, number | stringPage size in width, height or string such as "A4".
layout"portrait" | "landscape"Page layout. Default is "portrait".
marginsMarginsMargins of the new page.
marginnumberSyntax sugar for margins if all margins is the same.
Margins
PropertyTypeDescription
topnumberThe top margin.
leftnumberThe left margin.
bottomnumberThe bottom margin.
rightnumberThe right margin.

BlockElement

PropertyTypeDescription
topnumberTop position of the block.
leftnumberLeft position of the block.
contentContent[]List of content.

CaseElement

PropertyTypeDescription
conditionsCaseCondition[]Ordered list of conditions.
elseContentThis content will be rendered when any conditions isn't satisfied.
CaseCondition
PropertyTypeDescription
whenbooleanThe condition to be satisfied. Usually this is JSONata condition expression.
contentContentThis content will be rendered if condition is satisfied.

ForeachElement

PropertyTypeDescription
itemsany[]Items to be iterate.
contentContentThis will be rendered foreach loop.

In the loop, special variables are available.

Loop variables
AccessorTypeDescription
$foreachForeachCurrent loop.
$foreach.totalnumberTotal number of loop items.
$foreach.indexnumberCurrent item index.
$foreach.countnumberThe loop count.
$foreach.itemanyCurrent item.
$loopsForeach[]$loops[0] is the same as $foreach. $loops[1] is one outer loop.

Example

You can see example in tests directory.