vue.jspdf.jspdfjs-dist

import pdfjslib into vuejs component


I would like to create a viewer PDF files into electron/vuejs application.

I need to use the pdfjs librarie.

For that I installed this package : "pdfjs-dist": "2.6.347"

and in my component vuejs I try to used this by doing this :

import pdfjsLib from "pdfjs-dist/webpack";

This import instruction seeems to be running good. But If a start used this like this :

created()
{
    pdfjsLib.getDocument()
}

I throw this error :

Cannot read property 'getDocument' of undefined"

I try lot of tricks but I don't find any solution for use this library in my component.

Anyone have a vuejs project with pdfjslib for viewing pdf ?


Solution

  • This seems to work:

    import("pdfjs-dist").then((pdfjsLib) => {
      pdfjsLib.getDocument();
    });
    

    It can be used inside either created() or mounted(). I'd personally use it in mounted.
    Working demo here.

    Also note calling getDocument() with no arguments seems to trigger some lib error (but that's outside the scope of current question):

    Invalid parameter in getDocument, need either Uint8Array, string or a parameter object