blikblum/pdfmake-utils

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

Masber opened this issue · 1 comments

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

Any idea of what I'm doing wrong?

Yes.

You must call after assets are loaded

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