在windows上使用Nodejssharp无法将pdf转换为图像



所以我有一个Nodejs应用程序,它最初是在OSX(macbook(上构建的。这个过程的一部分是从用户上传的pdf文件的第一页创建缩略图。

以下是在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'
})
}
}

为了解释上面的代码,每次上传文件时,都会调用上面的函数来生成上传文件的缩略图(文件可以是图像类型或pdf(同样,上面的代码在OSX上运行良好。如果有帮助的话,该应用程序就是一个环回4应用程序。

然而,同样的代码不仅适用于pdf,而且适用于图像。我试过各种各样的东西,重新安装node_modules,升级/降级版本,我也研究过libvip,但基于sharp的网站,只需运行npm install sharp就可以完成这项工作,不需要为libvip进行其他配置。

在Windows上运行时返回的错误为Error: Input file contains unsupported image format

如果有人能帮我,我将不胜感激。

libvips PDF加载通常使用poppler进行渲染,poppler是GPL:

https://poppler.freedesktop.org/

许可证意味着sharp不能分发包含poppler的二进制文件。在macOS上,您使用的是自制程序中的libvip,所以这不是问题。

libvips也支持PDFium进行PDF渲染,但sharp还没有切换到它。sharp不允许你在windows上使用不同的libvips二进制文件,所以你可能没有选择。

最新更新