node.jsimagepdfpdfkitcover

How to use 'cover' option in pdfkit?


I'm trying to make a pdf file with a list of images, one image per page. The image should cover the whole page and shouldn't be cropped. I don't want to use fit option cause i'll have to provide width and height and it will be a hassle. So I figured I should use cover option but I can't seem to figure out how to use it. I couldn't find the solution in the internet either. Even its documentation doesn't mention about how to use it. I tried some random code like following:

doc.image('path/to/image.png', {
  cover: true
});

but no luck. It shows following error:

(node:9004) UnhandledPromiseRejectionWarning: TypeError: options.cover is not iterable
    at PDFDocument.image (/var/www/html/Custom Manga Downloader/node_modules/pdfkit/js/pdfkit.js:4487:26)
    at Object.run (/var/www/html/Custom Manga Downloader/downloaders/holymanga.net/index.mjs:21:7)
    at __holymanga (/var/www/html/Custom Manga Downloader/server.js:36:13)
    at Object.<anonymous> (/var/www/html/Custom Manga Downloader/server.js:42:19)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)

Please, any help will be appreciated.

Thank You :)


Solution

  • The documentation page says:

    cover array provided - image is scaled proportionally to completely cover the rectangle defined by the passed width and height.

    So, it actually needs an array of [width, height] instead of a boolean. Provide it an array and it will work perfectly fine.

    Here's a snippet of what I am using:

    doc.image(`${url}-screenshot.png`, {
            cover: [doc.page.width - 100, doc.page.height - 300]
        });
    

    Cheers!