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

obj2pdf

v1.5.0

Published

A npm package to convert an object to PDF

Downloads

28

Readme

obj2pdf

A server-side npm package to convert a JSON object to a PDF.

Build Status Coverage Status npm version

Table of contents

Dependencies

This package is dependent on the pdfmake package.

Installation

npm install obj2pdf --save

or

yarn add obj2pdf

Usage

  1. Import the package

Typescript:

import * as obj2pdf from 'obj2pdf';

or

Javascript:

const obj2pdf = require('obj2pdf');
  1. Now, simply use the exposed .generatePDF function which takes in a valid JSON object as the parameter. It returns a base64 encoded string containing the PDF data.

Provide a heading property if needed to generate a heading for the PDF which would be center aligned(see sample image below).

Each property(except heading) in the JSON corresponds a "Section" in the generated PDF (for eg. Employee Details, Employer Details in the below JSON example).

The value of each property(Section) in the JSON can be one of object, string or number.

An object value would specify sub-sections within a section(works atmost with 1 level nesting).

A string/number value prints it as-is below the Section(no sub-sections).

const inputJSON = {
  "heading": "PDF Heading",
  "Employee Details": {
    "First name": "John",
    "Last name": "Doe",
    "Gender": "Male"
  },
  "Employer Details": {
    "Name": "Google",
    "Location": "London"
  },
  "Currency": "£",
  "Amount": 10
};

obj2pdf.generatePDF(inputJSON)
  .then((pdfData) => {
    // do something with pdfData
  })
  .catch((err) => {
    console.log(`error caught : ${err}`);
  });

How to use the base64 PDF data string

The base64 encoded string response should look something like

data:application/pdf;base64,JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwogIC9Q...

This can then be used on client-side as the value to a href attribute of a HTML anchor element.

<a href="data:application/pdf;base64,JVBERi0xLjc…">
  Open PDF
</a>

Generated PDF Sample

If you used the JSON above, the generated PDF data upon viewing should look like

PDF Sample

Generated PDF Specs

  • Fonts being used are Roboto regular/bold
  • PDF heading is 15px, center aligned, bold.
  • Section heading is 12px, bold.
  • Sub-section heading is 10px, bold.
  • Section/Sub-section value is 8px.