PDF printout plugin

I am currently working on a plugin that can convert a PDF to an image printout and insert the images into a note.
I have a working setup but it is very much hacked together, as it calls a Python script I've written for everything related to PDF conversion. Naturally, I would like to re-write this as a native Joplin Plugin.

I am currently stuck with this as I cannot get a minimal example with PDF.js to work.

The minimal example would be something like this:

var pdfjs = require("pdfjs-dist");
const doc = await pdfjs.getDocument('/<path-to-pdf>').promise;

The error I am getting is the following:

    ERROR in ./node_modules/pdfjs-dist/build/pdf.js 924:33
    Module parse failed: Unexpected token (924:33)
    You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
    |   const httpHeaders = src.httpHeaders || null;
    |   const withCredentials = src.withCredentials === true;
    >   const password = src.password ?? null;
    |   const rangeTransport = src.range instanceof PDFDataRangeTransport ? src.range : null;
    |   const rangeChunkSize = Number.isInteger(src.rangeChunkSize) && src.rangeChunkSize > 0 ? src.rangeChunkSize : DEFAULT_RANGE_CHUNK_SIZE;
     @ ./src/index.ts 15:12-33

It seems to be related to the nullish coalescing operator not being defined. Asking ChatGPT and from trying to Google the answer, it seems like I have two options:

  1. Change the loader in webpack.config.js to use Babel and include the plugin
  2. Update the target versino inside tsconfig.json

I've had success with neither and would very much appreciate some help porting my script.

Can't you find a version of pdf.js that's compiled as commonJS? Or maybe build one yourself? Then you can require it as normal, without having to change the build system

That might be a better approach. I'll give it a shot and let you know of my findings.

Thanks!