marcdacz / compare-pdf

Standalone node module that compares pdfs
MIT License
62 stars 21 forks source link

Allow setting of pdfjsLib logging verbosity level #19

Closed humphreyn closed 2 years ago

humphreyn commented 2 years ago

Currently if there are any type of errors in pdfjsLib including warning and info's they are all printed out to the console. I think it would be better if we could control what level of error that is printed to the console by adding the verbosity level to the config.settings object like this:

const config = {
    paths: {
        actualPdfRootFolder: process.cwd() + '/data/newActualPdfs',
        baselinePdfRootFolder: process.cwd() + '/data/baselinePdfs',
        actualPngRootFolder: process.cwd() + '/data/actualPngs',
        baselinePngRootFolder: process.cwd() + '/data/baselinePngs',
        diffPngRootFolder: process.cwd() + '/data/diffPngs'
    },
    settings: {
        imageEngine: 'graphicsMagick',
        density: 100,
        quality: 70,
        tolerance: 0,
        threshold: 0.05,
        cleanPngPaths: true,
        matchPageCount: true,
        disableFontFace: true,
        verbosity: 0
    }
};

In pdfjsLib there are currently 3 verbosity levels:

const VerbosityLevel = {
  ERRORS: 0,
  WARNINGS: 1,
  INFOS: 5
};

IMO the default level should be 0 i.e. ERRORS but users can set the level they desire.

Then in the native.js code we could do something like this:

const pdfToPng = async (pdfFilePath, pngFilePath, config) => {
    try {
        const verbosity = config.settings.hasOwnProperty('verbosity') ? config.settings.hasOwnProperty('verbosity') : 0 // default verbosity to Error
        let pdfData = new Uint8Array(fs.readFileSync(pdfFilePath));
        let pdfDocument = await pdfjsLib.getDocument({
            disableFontFace: config.settings.disableFontFace,
            data: pdfData,
            cMapUrl: CMAP_URL,
            cMapPacked: CMAP_PACKED,
            verbosity: verbosity
        }).promise;

        for (let index = 1; index <= pdfDocument.numPages; index++) {
            await pdfPageToPng(pdfDocument, index, pngFilePath, pdfDocument.numPages === 1);
        }
    } catch (error) {
        throw error;
    }
};
marcdacz commented 2 years ago

this is a great idea mate. mind raising a PR with included tests? happy to merge it.

marcdacz commented 2 years ago

implemented in v1.1.6

humphreyn commented 2 years ago

Hi @marcdacz, Sorry I was going to raise a PR for this but Christmas got in the way. I see that you have added this functionality which is great