html-to-pdf-studio

1.18.0 • Public • Published

html-to-pdf-studio

Converting HTML template to PDF files

Codacy Badge npm npm LICENSE Dependabot Status

System Requirements

Installing

  • yarn add html-to-pdf-studio

Usage

Compiling a handlebars HTML template

const fs = require("fs");
const path = require("path");
const { compileHTML, createPDF } = require("html-to-pdf-studio");
 
const cssPath = path.join(__dirname, "./example/templates/style.css");
const css = fs.readFileSync(cssPath, "utf8");
 
const htmlPath = path.join(__dirname, "./example/templates/index.html");
const html = fs.readFileSync(htmlPath, "utf8");
 
let data = require("./example/templates/data.json");
const dataBinding = Object.assign(data, { css });
 
const compiledHTML = compileHTML(html, dataBinding);

Create PDF file from final HTML compiled with handlebars previously demonstrated

const path = require("path");
const { createPDF } = require("html-to-pdf-studio");
 
const fileName = "invoide.pdf";
const outputPath = path.join(__dirname, "./example/output");
 
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath);
 
const pdfOptions = {
  format: "A4",
  headerTemplate: "<p></p>",
  footerTemplate: "<p></p>",
  displayHeaderFooter: false,
  margin: {
    top: "40px",
    bottom: "100px",
  },
  printBackground: true,
  path: path.join(outputPath, fileName),
};
 
(async () => {
  await createPDF(compiledHTML, pdfOptions);
})();

Inspiration

tranchuong - html_to_pdf

License

Copylefted (c) 2020 Henrique Carvalho da Cruz Licensed under the MIT license.

Dependencies (2)

Dev Dependencies (12)

Package Sidebar

Install

npm i html-to-pdf-studio

Weekly Downloads

1

Version

1.18.0

License

MIT

Unpacked Size

6.16 kB

Total Files

11

Last publish

Collaborators

  • henriquecarv