3

I am trying to use pdfmake JS library in an offline webpage. I downloaded the CDN file from https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.66/pdfmake.min.js and saved it offline in ..app/js/pdfmake.js

I loaded the library using require:

require([
   'js/pdfmake.js',
...])

I test the library using the example below:

var buttonTS = document.getElementById("buttonTS")
butonTS.addEventListener("click", function(){
            
  var docDefinition = {
    content: [
            'First paragraph',
            'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
        ]
    };

    var now = new Date();

    var pdf = pdfmake.createPdf(docDefinition);
    pdf.write('pdfs/basics.pdf').then(() => {
        console.log(new Date() - now);
    }, err => {
        console.error(err);
    });
}

But the library does not load, and throws the error:

Uncaught ReferenceError: pdfmake is not defined

This is odd because I have other libraries (SweetAlert), which work with no problems.

Thanks for any help!

Litwos
  • 1,278
  • 4
  • 19
  • 44
  • `pdfmake.createPdf(docDefinition);` Where do you instantiate `pdfmake`? Not familliar with that library, but I assume you need to instantiate an object to work with first. – icecub Aug 10 '20 at 10:00
  • I don't need to instantiate pdfmake. It should work as a module and give me access to all functions. This is how I use other libraries as well. I am testing this example: https://github.com/bpampuch/pdfmake/blob/master/examples/basics.js – Litwos Aug 10 '20 at 10:05
  • Ah. Well look at the example code and at your code. The example: `var pdfmake = require('..` VS your code `require([...` – icecub Aug 10 '20 at 10:08
  • I instantiated the library as you suggested, but now it says `Uncaught TypeError: pdf.write is not a function` – Litwos Aug 10 '20 at 11:33

0 Answers0