0.0.22 • Published 9 months ago

easy-react-pdf v0.0.22

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

Easy React PDF

License

Overview

The Easy React PDF is a library that simplifies the process of generating PDF files from JSON configurations. With this library, you can easily create complex PDF documents with various styles, layouts, and content. and make it easy for templating PDF files.

Installation

You can install the library using npm:

npm install easy-react-pdf

Note

you need install react, react-pdf and react-dom in your project

npm install react react-dom @react-pdf/renderer

Usage

Node js

Stream File

import { createStreamPDF, IPDFPage } from "easy-react-pdf";

async function main(template: IPDFPage) {
  const stream = await createStreamPDF(template);
  return stream.pipe(fs.createWriteStream("output.pdf"));
}

Create File

import { createFilePDF, IPDFPage } from "easy-react-pdf";

async function main(template: IPDFPage) {
  await createFilePDF(template, "path/to/file.pdf");
}

React

import { PDFViewer } from "@react-pdf/renderer";
import { Document } from "easy-react-pdf";

function Main(template: IPDFPage) {
  return (
    <PDFViewer>
      <Document {...template} />
    </PDFViewer>
  );
}

Example Template

{
  "contents": [
    {
      "type": "views",
      "style": {
        "width": "100%",
        "height": "100%",
        "alignItems": "center",
        "justifyContent": "center",
        "display": "flex"
      },
      "contents": [
        {
          "type": "text",
          "text": "{{name}}",
          "style": {
            "fontSize": 30,
            "fontWeight": "bold"
          }
        }
      ]
    }
  ],
  "document": {
    "title": "Payment Schedules"
  },
  "pages": {
    "size": "A4"
  }
}

API

Validations

import { validateJsonPDF, validateObjectPDF } from "easy-react-pdf";

const validateJson = validateJsonPDF(contents);

const validateObject = validateObjectPDF(contents);

this function return a valid object or throw a error

Template

import { replaceVariablesJson, replaceVariablesObject } from "easy-react-pdf";

const variables = [
  {
    name: "name",
    value: "John Doe",
  },
  {
    name: "age",
    value: 20,
  },
];

const templateJson = replaceVariablesJson(contents, variables);

const templateObject = replaceVariablesObject(contents, variables);
0.0.22

9 months ago

0.0.21

9 months ago

0.0.20

9 months ago

0.0.19

9 months ago

0.0.18

9 months ago

0.0.17

9 months ago

0.0.16

9 months ago

0.0.15

9 months ago

0.0.14

9 months ago

0.0.13

9 months ago

0.0.12

9 months ago

0.0.11

9 months ago

0.0.10

9 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago