1

so I have a Nodejs application that was originally built on OSX(macbook). A part of the process is to create a thumbnail from the first page of a pdf file that user uploaded.

Below is the code that is working fine on OSX.

const generateThumbnailForFile = async ( fileDirName: string, fileFolder: string, fileName: string, template?: string ) => {
      // this part will generate an image of pdf first page and composite to a template        
      if (template) {
                    
           return await sharp(fileDirName, {
               page: 0
           })
           .rotate()
           .resize({
               width: 350,
               height: 500,
               fit: sharp.fit.cover,
               position: sharp.position.top
           })
           .jpeg({
               quality: 100,
               chromaSubsampling: '4:4:4'
           })
           .composite([ { input: dir + 'templates/' + template, gravity: 'southeast' } ])
           .toFile(dir + 'thumbnail/' + fileFolder.replace(/[.*+?^${}()|/[\]\\]/g, '-') + '-' + uploadFileName + '.jpg')
           .then(( info: any ) => {                            
               thumbUrl = 'thumbnail/' + fileFolder.replace(/[.*+?^${}()|/[\]\\]/g, '-') + '-' + uploadFileName + '.jpg'
            })
            .catch(( err: any ) => {
                console.log('PDF ERROR HERE', err)
                thumbUrl = 'thumbnail/default-pdf.png'
             })
       }
       // this part will run if no template is supplied, mainly for images
         else {
            return await sharp(fileDirName)
                        .rotate()
                        .resize({
                            width: 500,
                            height: 500,
                            fit: sharp.fit.cover,
                            position: sharp.position.top
                        })
                        .toFile(dir + 'thumbnail/' + fileFolder.replace(/[.*+?^${}()|/[\]\\]/g, '-') + '-' + fileName)
                        .then(( info: any ) => {
                            thumbUrl = 'thumbnail/' + fileFolder.replace(/[.*+?^${}()|/[\]\\]/g, '-') + '-' + fileName
                        })
                        .catch(( err: any ) => {
                            console.log('Thumbnail generation error: ', err)
                            thumbUrl = 'thumbnail/default-pdf.png'
                        })
                }

            }

To explain the code above, every time a file is uploaded, the function above is called to generate the thumbnail for the file uploaded (the file can be image type or pdf) Again, the code above is working fine on OSX. If it helps, the application is a loopback4 application.

However, the same code does not work on Windows only for pdf but images will work. I've tried all sorts of stuff, re-installing node_modules, upgrading/downgrading version, I have also look into libvips but based on sharp's site, simply running npm install sharp should do the job and no other configuration for libvips is needed.

The error returns when running on Windows is Error: Input file contains unsupported image format

Appreciate it if someone can help me with this.

vasadia
  • 366
  • 5
  • 8
Rasyue
  • 60
  • 1
  • 9

1 Answers1

2

libvips PDF load usually uses poppler for rendering, and poppler is GPL:

https://poppler.freedesktop.org/

The licence means that sharp can't distribute binaries that include poppler. On macOS, you're using the libvips from homebrew, so this is not an issue.

libvips also supports PDFium for PDF rendering, but sharp hasn't yet switched to it. sharp does not let you use a different libvips binary on windows, so you are probably out of options.

jcupitt
  • 10,213
  • 2
  • 23
  • 39
  • thank you for the clarification. I have also come to realization that using sharp probably won't work for me. – Rasyue Jun 21 '21 at 02:24