Code Monkey home page Code Monkey logo

pdfmake-utils's Introduction

PdfMake Utils

NPM version NPM downloads Dependency Status

PdfMake Utils provides helper classes to use with PdfMake

Features

    ✓ Custom element class to display pdf document
    ✓ Load assets dynamically

Documentation

PdfAssetsLoader

A class to load dynamically fonts and arbitrary files.

Properties
  • ready: Becomes true when assets loading is done
  • vfs: a hash containing loaded files data
  • fonts: a hash containing loaded fonts data
  • pdfMake: pdfMake instance (optional)
Methods
  • registerFile Register a file to be loaded. Accepts a hash with following properties:
    • name: the file name
    • URL: URL where the file should be loaded from. If not set it will use name
  • registerFont Register a font to be loaded. Accepts a hash with following properties:
    • name: the font name
    • fileName: the font file name
    • URL: URL where the file should be loaded from. If not set it will use fileName
    • styles: an array with the styles that this file will be associated with
  • configurePdfMake Configure pdfMake vfs and fonts properties with the loaded data. Accepts the pdfMake instance as argument
  • load: loads the registered fonts / files and configure pdfMake if pdfMake property is assigned. Returns a promise that is resolved if all files loaded correctly or is rejected if any file fails to load.
Usage
import pdfmake from 'pdfmake/build/pdfmake'
import { PdfAssetsLoader } from 'pdfmake-utils'

const assetsLoader = new PdfAssetsLoader()
pdfmake.fonts = assetsLoader.fonts
pdfmake.vfs = assetsLoader.vfs

// register Roboto font, normal variant. Roboto-Regular.woff must be in root path
assetsLoader.registerFont({name: 'Roboto', fileName: 'Roboto-Regular.woff', styles: ['normal']})
// register Roboto font, bolditalics variant using a custom file location
assetsLoader.registerFont({name: 'Roboto', fileName: 'Roboto-MediumItalic.woff', URL: 'fonts/Roboto-MediumItalic.woff', styles: ['bolditalics']})

// register a file to be loaded in the root path
assetsLoader.registerFile({name: 'MyLogo.png'})
// register a file with a custom location
assetsLoader.registerFile({name: 'MyHeader.png', URL: 'images/sunshine.png'})

assetsLoader.load().then(() => {
  console.log('assets loaded')
}).catch(errors => {
  // errors -> array with all file loading errors
  console.error('assets loading', errors);
})
Standard fonts

The Pdf standard fonts can be loaded by setting fileName to the font name without extension, e.g., 'Courier' or 'Courier-Bold'. By default, it will load the respective *.afm file in root path.

To use standard fonts is necessary to call configurePdfMake

Usage
import pdfmake from 'pdfmake/build/pdfmake'
import { PdfAssetsLoader } from 'pdfmake-utils'

const assetsLoader = new PdfAssetsLoader()

// register a Times font that will use the standard Times-Roman font. Times-Roman.afm must be in root path
assetsLoader.registerFont({name: 'Times', fileName: 'Times-Roman', styles: ['normal']})
// registering standard Times-Italic with a custom afm file location
assetsLoader.registerFont({name: 'Times', fileName: 'Times-Italic', URL: 'fonts/Times-Italic.afm', styles: ['italics']})

// all possible standard fonts
assetsLoader.registerFont({name: 'Times', fileName: 'Times-Roman', styles: ['normal']})
assetsLoader.registerFont({name: 'Times', fileName: 'Times-Italic', styles: ['italics']})
assetsLoader.registerFont({name: 'Times', fileName: 'Times-Bold', styles: ['bold']})
assetsLoader.registerFont({name: 'Times', fileName: 'Times-BoldItalic', styles: ['bolditalics']})

assetsLoader.registerFont({name: 'Courier', fileName: 'Courier', styles: ['normal']})
assetsLoader.registerFont({name: 'Courier', fileName: 'Courier-Oblique', styles: ['italics']})
assetsLoader.registerFont({name: 'Courier', fileName: 'Courier-Bold', styles: ['bold']})
assetsLoader.registerFont({name: 'Courier', fileName: 'Courier-BoldOblique', styles: ['bolditalics']})

assetsLoader.registerFont({name: 'Helvetica', fileName: 'Helvetica', styles: ['normal']})
assetsLoader.registerFont({name: 'Helvetica', fileName: 'Helvetica-Oblique', styles: ['italics']})
assetsLoader.registerFont({name: 'Helvetica', fileName: 'Helvetica-Bold', styles: ['bold']})
assetsLoader.registerFont({name: 'Helvetica', fileName: 'Helvetica-BoldOblique', styles: ['bolditalics']})

assetsLoader.registerFont({name: 'Symbol', fileName: 'Symbol'})

assetsLoader.registerFont({name: 'ZapfDingbats', fileName: 'ZapfDingbats'})

assetsLoader.load().then(() => {
  assetsLoader.configurePdfMake(pdfMake)
}).catch(errors => {
  // will fail if one of the files fails to load 
  // configure pdfMake with the files that loaded correctly
  assetsLoader.configurePdfMake(pdfMake)
  console.error('assets loading', errors);
})

PdfViewer

A Custom Element class that creates and displays a pdf in an iframe.

The assets and the pdfmake packages are loaded dynamically the first time an instance is created

By default, it uses window.pdfMake. Configure the pdfMake instance using getPdfMake callback

Properties
  • data The pdf document definition to be generated
Static Properties
  • getPdfMake Accepts a function that returns a pdfMake instance or a promise that resolves to a pdfMake instance
Static Methods
  • registerFile Register a file to be loaded. Same signature as PdfAssetsLoader registerFile method
  • registerFont Register a font to be loaded. Same signature as PdfAssetsLoader registerFont method
Usage
import { PdfViewer } from 'pdfmake-utils'

// lazy load pdfmake (webpack will do code split automatically)
PdfViewer.getPdfMake = () => import('pdfmake/build/pdfmake')

PdfViewer.registerFont({name: 'Roboto', fileName: 'Roboto-Regular.woff', styles: ['normal']})
PdfViewer.registerFont({name: 'Roboto', fileName: 'Roboto-Italic.woff', styles: ['italics']})
PdfViewer.registerFile({name: 'MyLogo.png'})

customElements.define('pdf-viewer', PdfViewer)

// use pdf-viewer element with, e.g, lit-html:
// <pdf-viewer .data=${docDefinition}></pdf-viewer>

License

Copyright © 2019 Luiz Américo. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.


Made with ♥ by Luiz Américo and contributors

pdfmake-utils's People

Contributors

blikblum avatar dependabot[bot] avatar rotzbua avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pdfmake-utils's Issues

Uncaught (in promise) Error: Font 'Roboto' in style 'normal' is not defined in the font section of the document definition.

Hi,

I am testing this library but unfortunately can't make it work. Here is what I am trying to do. At this point I am literally copying your example from README.

      const pdfContent = {
        content: ["My test"],
      };

      const assetsLoader = new PdfAssetsLoader();
      pdfMake.fonts = assetsLoader.fonts;
      pdfMake.vfs = assetsLoader.vfs;

      // register Roboto font, normal variant. Roboto-Regular.woff must be in root path
      assetsLoader.registerFont({
        name: "Roboto",
        fileName: "Roboto-Regular.woff",
        styles: ["normal"],
      });
      // register Roboto font, bolditalics variant using a custom file location
      assetsLoader.registerFont({
        name: "Roboto",
        fileName: "Roboto-MediumItalic.woff",
        URL: "fonts/Roboto-MediumItalic.woff",
        styles: ["bolditalics"],
      });

      // register a file to be loaded in the root path
      assetsLoader.registerFile({ name: "MyLogo.png" });
      // register a file with a custom location
      assetsLoader.registerFile({
        name: "MyHeader.png",
        URL: "images/sunshine.png",
      });

      assetsLoader
        .load()
        .then(() => {
          console.log("assets loaded");
        })
        .catch((errors) => {
          // errors -> array with all file loading errors
          console.error("assets loading", errors);
        });

      pdfMake
        .createPdf(pdfContent)
        .download("mytest.pdf");

and the error in browser console says:

Uncaught (in promise) Error: Font 'Roboto' in style 'normal' is not defined in the font section of the document definition.
    at FontProvider.provideFont (pdfmake.min.js:2:580246)
    at pdfmake.min.js:2:103003
    at Array.forEach (<anonymous>)
    at measure (pdfmake.min.js:2:102066)
    at TextTools.buildInlines (pdfmake.min.js:2:103564)
    at DocMeasure.measureLeaf (pdfmake.min.js:2:600645)
    at pdfmake.min.js:2:598540
    at StyleContextStack.auto (pdfmake.min.js:2:163364)
    at DocMeasure.measureNode (pdfmake.min.js:2:597427)
    at DocMeasure.measureVerticalContainer (pdfmake.min.js:2:601611)

Any idea of what I'm doing wrong?

thank you very much

Create LazyPdfMake

Add a class that handles loading assets as well the pdfMake bundle

Should accept fonts and files as params and a ready method

Example:

const fonts = {
  Roboto: {italics: '[url]'}
}
const pdfMake = new LazyPdfMake({fonts})
await pdfMake.ready()
pdfMake.instance // actual pdfMake
pdfMake.fileExists('[filePath]')

Dynamical load of standard fonts for pdf creation?

Hi,

is it possible to add standard fonts dynamically for pdf creation? (Browser)

I tested following script but it does not work:

assetsLoader.registerFont({
    name: 'Courier',
    fileName: 'Courier',
    URL: 'assets/font/data/Courier.afm',
    styles: ['normal']
});

Error:

ERROR Error: "File '../font/data/Courier.afm' not found in virtual file system"

It seems that the standard font are only stored in pdfMake.rawFiles and not processed further on.

pdfmake-lite bundling for 0.1.65

Hi, @blikblum . First, thanks for you awesome work with pdfmake-lite and pdfmake-utils.
We're using them on production and they helped us in reducing our bundle size.

I'm opening this issue over this repository because I didn't finded the proper repository for pdkmake-lite, so sorry by that. We noticed that pdfmake-lite is behind pdfmake by some versions, being 0.1.65 the last one.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.