Pdfmake - cannot find font files on vfs

I have the following code that I use to test the disk PDFmake. I am having a problem with the location of the font files. I see documentation that seems to indicate that after pulling in the vfs_fonts file that I should see. However, this does not apply to me.

function createPdf(assessmentId: string): string {
  const pdfMake = require('pdfmake/build/pdfmake.js');
  const pdfFonts = require('pdfmake/build/vfs_fonts.js');
  pdfMake.vfs = pdfFonts.pdfMake.vfs;

  //required font setup, requires that you link to the fonts shipped with npm

  const fontDescriptors = {
    Roboto: {
      normal: 'Roboto-Regular.ttf',
      bold: 'Roboto-Medium.ttf',
      italics: 'Roboto-Italic.ttf',
      bolditalics: 'Roboto-Italic.ttf',
    }
  };
  const termpaper = new PdfLayout();
  const docDefinition = termpaper.layout
  const printer = new Printer(fontDescriptors);

  //get a reference to the PdfKit instance, which is a streaming interface

  const pdfDoc = printer.createPdfKitDocument(docDefinition);

  return "pdflocation";
}

When this code is executed, I get this error.

Error:

ENOENT: , "Roboto-Medium.ttf"      (native)      Object.fs.openSync(fs.js: 642: 18)      Object.fs.readFileSync(fs.js: 510: 33)      Object.fontkit.openSync(/user_code/ node_modules/pdfmake/node_modules/fontkit/index.js:43:19)      Function.PDFFont.open(/user_code/node_modules/pdfmake/node_modules/pdfkit/js/font.js:14:24)      PDFDocument.font(/user_code/node_modules/pdfmake/node_modules/pdfkit/js/mixins/fonts.js:39:28)      FontProvider.provideFont(/user_code/node_modules/pdfmake/src/fontProvider.js:49:58)      /user _code/node_modules/pdfmake/src/textTools.js:258:27      Array.forEach(native)      (/user_code/node_modules/pdfmake/src/textTools.js:240:13)

, ?

+4
6

. , , , . "fonts" , uri:

const fonts = {
    Roboto: {
        normal: 'fonts/Roboto-Regular.ttf',
        bold: 'fonts/Roboto-Medium.ttf',
        italics: 'fonts/Roboto-Italic.ttf',
        bolditalics: 'fonts/Roboto-MediumItalic.ttf'
    }
};

.

, .

+3
function createPdf(assessmentId: string): string {
  const pdfMake = require("pdfmake/build/pdfmake.js");
  const pdfFonts = require("pdfmake/build/vfs_fonts.js");
  pdfMake.vfs = pdfFonts.pdfMake.vfs;
  const projectId = "crds-wayfinder-int";
  const philstorage = new gcs({
    projectId: projectId
  });

  // required font setup, requires that you link to the fonts shipped with npm

  const termpaper = new PdfLayout();
  const docDefinition = termpaper.layout;

  // get a reference to the PdfKit instance, which is a streaming interface
  const pdfDoc = pdfMake.createPdf(docDefinition);
  pdfDoc.getBuffer((buffer: any) => {
    console.log('buffer: ', buffer);
  });

  console.log('pdfDoc: ', pdfDoc);

  return "pdflocation";
}
+1

, .

.

// , , , npm


:

    normal: '/Exampledir/pdfmake/Roboto-Regular.ttf',
    bold: 'Roboto-Medium.ttf',
    italics: 'Roboto-Italic.ttf',
    bolditalics: 'Roboto-Italic.ttf',

/
const pdfFonts = require ("pdfmake/

Eg.
const pdfFonts = require ("/pdfmake/

, , , cd./then cd/pdfmake/etc ls. .

+1

3 :

, 3 :

  • vfs_fonts.js,
  • doc-definition-object

1. vfs_fonts.js,

myProject/fonts/

grunt dump_dir ( Gruntfile.js, dump_dir)

/vfs _fonts.js -

BTW - myProject/fonts/( ), , , grunt dump_dir doc-definition-object

2.

pdfMake.createPdf(docDefinition) pdfMake.fonts :

{
   yourFontName: {
     normal: 'fontFile.ttf',
     bold: 'fontFile2.ttf',
     italics: 'fontFile3.ttf',
     bolditalics: 'fontFile4.ttf'
   },
   anotherFontName: {
     (...)
   }
}

- , doc-

     
  

4 : , , , ( myProject/fonts/)

  

pdfMake :

{
        Roboto: {
                normal: 'Roboto-Regular.ttf',
                bold: 'Roboto-Medium.ttf',
                italics: 'Roboto-Italic.ttf',
                bolditalics: 'Roboto-Italic.ttf'
        }
};

3. doc--

pdfmake "Roboto" , , , . - Style

var docDefinition = {
  content: (...),
  defaultStyle: {
    font: 'yourFontName'
  }
}

, , , myProject/fonts/, . :

const fontDescriptors = {
    Roboto: {
      normal: 'myProject/fonts/Roboto-Regular.ttf',
      bold: 'myProject/fonts/Roboto-Medium.ttf',
      italics: 'myProject/fonts/Roboto-Italic.ttf',
      bolditalics: 'myProject/fonts/Roboto-Italic.ttf',
    }
};

, . , .

+1

-. - , -, :

var fonts = require('imports?this=>window!pdfmake/build/vfs_fonts.js');

:

function createPdf(assessmentId: string): string {

  const pdfMake = require('pdfmake/build/pdfmake.js');

  const pdfFonts = require('pdfmake/build/vfs_fonts.js');// this line will give an error if you are working with webpack . 

var fonts = require ('?! import this => pdfmake / assembly window /vfs_fonts.js');// use this line to fix it

  pdfMake.vfs = pdfFonts.pdfMake.vfs;

  //required font setup, requires that you link to the fonts shipped with npm

  const fontDescriptors = {
    Roboto: {
      normal: 'Roboto-Regular.ttf',
      bold: 'Roboto-Medium.ttf',
      italics: 'Roboto-Italic.ttf',
      bolditalics: 'Roboto-Italic.ttf',
    }
  };
  const termpaper = new PdfLayout();
  const docDefinition = termpaper.layout
  const printer = new Printer(fontDescriptors);

  //get a reference to the PdfKit instance, which is a streaming interface

  const pdfDoc = printer.createPdfKitDocument(docDefinition);

  return "pdflocation";
}

Hope this can be helpful to others!

+1
source

I am using React and datatables.net. Here is what worked for me:

import React from 'react';
import $ from 'jquery';

import 'datatables.net-bs';
import 'datatables.net-buttons-bs';
import 'datatables.net-responsive-bs';
import 'datatables.net-buttons/js/buttons.colVis.js';
import 'datatables.net-buttons/js/buttons.flash.js';
import 'jszip';
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
import 'datatables.net-buttons/js/buttons.html5.js';
import 'datatables.net-buttons/js/buttons.print.js';

import 'bootstrap/dist/css/bootstrap.min.css';
import 'datatables.net-bs/css/dataTables.bootstrap.css';

$.DataTable = require('datatables.net');
pdfMake.vfs = pdfFonts.pdfMake.vfs; // This line solved the Roboto errors

require("datatables.net-buttons/js/buttons.colVis.js"); // Column visibility
require("datatables.net-buttons/js/buttons.html5.js"); // HTML 5 file export
require("datatables.net-buttons/js/buttons.flash.js"); // Flash file export
require("datatables.net-buttons/js/buttons.print.js"); // Print view button
0
source

Source: https://habr.com/ru/post/1693757/


All Articles