npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jspdf-dynamo

v1.0.7

Published

Generate data driven PDFs from dynamic templates

Downloads

12

Readme

jsPdf-Dynamo

jsPdf-Dynamo is a wrapper around the open-source JavaScript PDF generation library, jsPDF. Using a template driven approach, it enables the separation of layout and formatting logic from the placement of data.

jsPdf-Dynamo can be used with browser or NodeJs applications written in JavaScript or TypeScript.

Advantages of using jsPdf-Dynamo include:

  • The separation of layout and formatting logic from the placement of data. As a page is filled with data, jsPdf-Dynamo can insert new pages, with appropriate headings, footings, and bookmarks as required.
  • The ability to store corporate and application wide format settings in templates that can be maintained independently from the application. This is similar to the use of style sheets in web applications to enable visual consistency, reduce errors and minimise maintenance costs. These format settings can include colours, font styles and sizes, page headings, and more.

Install

Using npm:

npm install jspdf --save

Using yarn:

yarn add jspdf

Basic Concepts

There are a few basic concepts to be aware of when using jsPdf-Dynamo:

  • The functionality of jsPdf-Dynamo is implemented through the JsPdfDynamo class.
  • The initial page size, orientation and unit of measure are set when the JsPdfDynamo instance is instantiated.
  • Output is driven by a series of plain text 'commands'. These commands can be provided as a list of strings from multiple sources, including the JavaScript or TypeScript application, or loaded from 'templates' retrieved from a URL (browser only) or from local text files (NodeJs only).
  • Positions are specified relative to the left and top margin. The exception to this are margins which are measured from the appropriate edge of the page.
  • All measurements and positions are in the unit of measure specified when the instance of JsPdfDynamo is created. This can be millimeters, inches, or points. The exception to this are fonts, which are always specified in points.
  • A series of commands can be grouped and named. These groups of commands can then be processed one or more times. This is a similar concept to methods or procedures in more sophisticated computer languages.
  • All commands, command group names and variable names are case insensitive.
  • There are two kinds of variables, user variables and system maintained variables. User variables can be created and modified as required using script commands. As the name implies, system maintained variables are created and maintained and cannot be directly modified directly by script commands.

All commands are described in the documentation that can be found here

A Simple Example

In this example, there is a single template file, template.txt

[Initialise]
.SetVar themeColour blue
.SetFontName helvetica
.SetSpaceVert = 0.3
.SetMargin a 10
.SetMargin b 15
[End]

[TitlePage]
.SetLineWidth 0.8
.SetLineColor %themeColour%
.DrawBox  0 0 %_PageWidth% %_PageHeight% 0
.SetFontSize 36
.SetTextColour %themeColour%
.SetFontStyle Bold
.DrawTextBox 0 0 %_PageWidth% %_PageHeight% center center %title%
[End]

[DrawPage]
.Do Initialise TitlePage
[End]

The code to load this template, set the text of the title, process the commands, and save the document is:

import { JsPdfDynamo } from "jspdf-dynamo";

const commands = [
    // Load the template
    ".include path-to-template/template.txt",
    // Set the title
    ".SetVar title This is a title",
    // Process the template
    ".Do DrawPage",
    // Save the pdf
    ".SavePdf ./simple.pdf"
];
await pdfDynamo.processCommands(commands);

This, example can be found in the examples folder (additional examples to be added).