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.