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

@devanshdeveloper/json-to-pdf

v1.0.3

Published

A lib for json to pdf

Downloads

7

Readme

JSON-to-PDF with Dynamic Content

This project demonstrates how to dynamically generate a PDF from a JSON object using the json-to-pdf package. The example generates a PDF with a simple restaurant menu using dynamic data, with on-screen rendering of the PDF and a download option.

Table of Contents

  • Installation
  • Usage
  • Features
  • Example
  • Contributing
  • License

Installation

npm install json-to-pdf

Ensure you also have a valid CSS file (style.css) in your project for custom styling.

Usage

  1. Import the necessary modules
import { JSONToPDF } from "json-to-pdf";
import "./style.css";
  1. Define the JSON structure This sample generates a PDF of a dynamic restaurant menu:
const jsonToPdf = new JSONToPDF({
  backgroundColor: "#ebe2c3",
  backgroundImage: "menu-border.png",
  payload: {
    menus: [
      {
        id: 1,
        name: "Dynamic Dish 1",
        description: "A dynamically delicious dish",
        availability: "In Stock",
        price: "$19.99",
      },
      {
        id: 2,
        name: "Dynamic Dish 2",
        description: "A dynamically exquisite creation",
        availability: "Out of Stock",
        price: "$24.99",
      },
    ],
  },
  page: {
    size: "A4",
    styles: { width: "500px" },
  },
  pageContent: { styles: { padding: "10px" } },
  elements: [{ tag: "menuElement" }],
  fixedElements: [
    {
      tag: "div",
      attributes: {
        styles: { fontSize: "12px" },
        html: "Restaurant Name",
      },
    },
    {
      tag: "div",
      attributes: {
        styles: {
          position: "absolute",
          bottom: "20px",
          right: "20px",
          fontSize: "12px",
        },
        html: "{{pagenumber}}",
      },
    },
  ],
  defineElements: [
    {
      name: "menuElement",
      iterator: "{{payload.menus}}",
      elements: [
        {
          tag: "div",
          attributes: {
            data: ["dish_id", "{{item.id}}"],
            styles: { display: "flex", flexDirection: "column", gap: "12px" },
          },
          children: [
            {
              tag: "div",
              attributes: {
                html: "{{item.name}}",
                styles: { fontSize: "16px", textAlign: "center" },
              },
            },
            {
              tag: "div",
              attributes: {
                html: "{{item.description}}",
                styles: { fontSize: "12px", textAlign: "center" },
              },
            },
            {
              tag: "div",
              attributes: {
                styles: { display: "flex", justifyContent: "space-between" },
              },
              children: [
                {
                  tag: "div",
                  attributes: { styles: { fontSize: "12px" }, html: "{{item.availability}}" },
                },
                {
                  tag: "div",
                  attributes: { styles: { fontSize: "12px" }, html: "{{item.price}}" },
                },
              ],
            },
          ],
        },
      ],
    },
  ],
});
  1. Paint PDF on the screen and enable download
jsonToPdf.paintPDFonScreen(document.querySelector(".print-container"));

document.querySelector("#pdf-btn").addEventListener("click", () => {
  jsonToPdf.download({
    onProgress: (current) => {
      document.querySelector("#pdf-btn").innerHTML = `Progress: ${current}%`;
    },
    onComplete: () => {
      document.querySelector("#pdf-btn").innerHTML = "Download Complete!";
    },
    onError: (error) => {
      document.querySelector("#pdf-btn").innerHTML = `Error: ${error.message}`;
    },
    filename: "menu.pdf",
    resolution: 2500,
  });
});
  1. HTML structure Ensure you have the following HTML structure:
<div class="print-container"></div>
<button id="pdf-btn">Download PDF</button>
  1. Add your style.css file
.print-container {
  /* Define custom styles for the rendered PDF */
}

Features

  • Dynamic PDF generation: The PDF content is generated dynamically from JSON data.
  • Custom styling: You can customize page layouts, content styles, and fixed elements like headers and footers.
  • Progress tracking: Shows progress updates while downloading the PDF.

Example

This example generates a restaurant menu with two dynamic dishes, allows for on-screen preview, and provides a downloadable PDF with a progress indicator.

Contributing

Feel free to fork the repository and submit pull requests for any improvements.

License

This project is licensed under the MIT License.