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

@s1seven/schema-tools-generate-pdf

v0.2.20

Published

Tool to generate pdf from certificates

Downloads

130

Readme

Schema-tools-generate-pdf

npm

The generate-pdf module is using pdfmake to generate PDF buffer | stream from a certificate as JSON or HTML.

Install

npm install @s1seven/schema-tools-generate-pdf

Using generate-pdf example

When using generatePdf, you can either pass in paths to local files or use remote versions. Note that you must always pass in the filepath to the font files as seen below.

Use remote options

const fonts = {
  NotoSans: {
    normal: `${__dirname}/../../fixtures/fonts/NotoSans-Regular.ttf`,
    bold: `${__dirname}/../../fixtures/fonts/NotoSans-Bold.ttf`,
    light: `${__dirname}/../../fixtures/fonts/NotoSans-Light.ttf`,
    italics: `${__dirname}/../../fixtures/fonts/NotoSans-Italic.ttf`,
  },
};
const generatePdfOptions = {
  fonts,
};
const pdfDoc = await generatePdf(validCertificate, generatePdfOptions);

Use local options

const { readFileSync } = require('fs');
const { generatePdf } = require('@s1seven/schema-tools-generate-pdf');
const styles = require(`${__dirname}/../generate-coa-pdf-template/utils/styles.js`);

const CoACertificate = JSON.parse(readFileSync(`${__dirname}/../../fixtures/CoA/v1.1.0/valid_cert.json`));
const translations = JSON.parse(readFileSync(`${__dirname}/../../fixtures/CoA/v1.1.0/translations.json`));
const extraTranslations = JSON.parse(readFileSync(`${__dirname}/../../fixtures/CoA/v1.1.0/extra_translations.json`));
const generatorPath = '../../dist/packages/generate-coa-pdf-template/generateContent.cjs';

(async function () {
  try {
    const fonts = {
      NotoSans: {
        normal: `${__dirname}/../../fixtures/fonts/NotoSans-Regular.ttf`,
        bold: `${__dirname}/../../fixtures/fonts/NotoSans-Bold.ttf`,
        light: `${__dirname}/../../fixtures/fonts/NotoSans-Light.ttf`,
        italics: `${__dirname}/../../fixtures/fonts/NotoSans-Italic.ttf`,
      },
    };

    const docDefinition = {
      pageSize: 'A4',
      pageMargins: [20, 20, 20, 40],
      footer: (currentPage, pageCount) => ({
        text: currentPage.toString() + ' / ' + pageCount,
        style: 'footer',
        alignment: 'center',
      }),
      // The value of `defaultStyle.font` must be the same as one of the keys from `fonts`,
      defaultStyle: {
        font: 'NotoSans',
        fontSize: 10,
      },
      styles,
    };

    const pdfDoc = await generatePdf(CoACertificate, {
      docDefinition,
      generatorPath,
      fonts,
      extraTranslations,
      translations,
    });

    const outputFilePath = './test.pdf';
    await writeFile(outputFilePath, pdfDoc);
  } catch (error) {
    console.error(error.message);
  }
})();

Overriding fonts

The font defaults to NotoSans, but this can be overridden by passing in a languageFontMap object to the options.

const { readFileSync } = require('fs');
const { generatePdf } = require('./dist/index');
const styles = require(`${__dirname}/../generate-coa-pdf-template/utils/styles.js`);

const CoACertificate = JSON.parse(readFileSync(`${__dirname}/../../fixtures/CoA/v1.1.0/valid_cert.json`));
const translations = JSON.parse(readFileSync(`${__dirname}/../../fixtures/CoA/v1.1.0/translations.json`));
const extraTranslations = JSON.parse(readFileSync(`${__dirname}/../../fixtures/CoA/v1.1.0/extra_translations.json`));
const generatorPath = '../../dist/packages/generate-coa-pdf-template/generateContent.cjs';

(async function () {
  try {
    const fonts = {
      NotoSans: {
        normal: `${__dirname}/../../fixtures/fonts/NotoSans-Regular.ttf`,
        bold: `${__dirname}/../../fixtures/fonts/NotoSans-Bold.ttf`,
        light: `${__dirname}/../../fixtures/fonts/NotoSans-Light.ttf`,
        italics: `${__dirname}/../../fixtures/fonts/NotoSans-Italic.ttf`,
      },
      NotoSansSC: {
        normal: `${__dirname}/../../fixtures/fonts/NotoSansSC-Regular.ttf`,
        bold: `${__dirname}/../../fixtures/fonts/NotoSansSC-Bold.ttf`,
        light: `${__dirname}/../../fixtures/fonts/NotoSansSC-Light.ttf`,
        italics: `${__dirname}/../../fixtures/fonts/NotoSansSC-Regular.ttf`, // SC doesn't have italic
      },
    };

    // The value must be the same as one of the keys from `fonts`,
    // The key must be a supported language
    const languageFontMap = {
      CN: 'NotoSansSC',
    };

    const docDefinition = {
      pageSize: 'A4',
      pageMargins: [20, 20, 20, 40],
      footer: (currentPage, pageCount) => ({
        text: currentPage.toString() + ' / ' + pageCount,
        style: 'footer',
        alignment: 'center',
      }),
      // The value of `defaultStyle.font` must be the same as one of the keys from `fonts`,
      defaultStyle: {
        font: 'NotoSans',
        fontSize: 10,
      },
      styles,
    };

    const pdfDoc = await generatePdf(CoACertificate, {
      docDefinition,
      generatorPath,
      fonts,
      extraTranslations,
      translations,
      languageFontMap,
    });

    const outputFilePath = './test.pdf';
    await writeFile(outputFilePath, pdfDoc);
  } catch (error) {
    console.error(error.message);
  }
})();

Troubleshooting

Dependencies

If the tests pass locally but fail in the CI, try updating the dependencies (including Ghostscript and GraphicsMagick). Ensure that the dependencies are the same locally as in the CI.

Styles

The styles (specifically the margins) at generate-coa-pdf-template/utils/styles.js are slightly different from the styles in the CoA-schemas repo. This should be obvious when visually comparing the fixture with the PDF created by the final should render PDF certificate using certificate object, local PDF generator script, styles and translations test. If this is the case, run the fixtures:pdf command again, using the styles from generate-coa-pdf-template/utils/styles.js.