Node.js imagemagik错误:页面树中的页面太多.同时将pdf转换为png



我在将一些pdf转换为png缩略图时遇到了一些问题。

const outputStream = gm(Buffer.from(path, 'base64'))
.selectFrame(0)
.noProfile()
.quality(60)
.density(200)
.background('white')
.resize(600, 600)
.setFormat('png');

然后我得到了这个错误

在这里你可以看到那些pdfs

有什么方法可以修复这个错误,或者用其他方法获取pdf缩略图吗?

Flag-flat解决了我的问题:(

在某些文件格式(如Photoshop的PSD(中,复杂的图像可能由"层"(独立图像(表示,必须对其进行合成才能获得最终的呈现。flatten选项完成了这个组合。图像序列由通过依次合成每个图像而创建的单个图像代替,同时考虑合成运算符和页面偏移。尽管-platform对于消除图层非常有用,但它作为一种通用的合成工具也很有用。

所以我只生成扁平pdf,然后将其转换为图像

希望它能帮助

const flattenPDFStream = gm(Buffer.from(path, 'base64'))
.define('pdf:use-cropbox=true')
.selectFrame(0)
.flatten();
const flattenPDF = await gmToBuffer(flattenPDFStream);
const outputStream = gm(flattenPDF)
.selectFrame(0)
.noProfile()
.quality(90)
.background('white')
.resize(400, 400)
.setFormat('png');

GM到缓冲功能:

function gmToBuffer(data) {
return new Promise((resolve, reject) => {
data.stream((err, stdout, stderr) => {
if (err) { return reject(err); }
const chunks = [];
stdout.on('data', chunk => {
// Not best solution, but i need to control error message will not appear in pdf
if (!chunk.includes('Error')) {
chunks.push(chunk);
} else {
console.log(chunk.toString());
}
});
// these are 'once' because they can and do fire multiple times for multiple errors,
// but this is a promise so you'll have to deal with them one at a time
stdout.once('end', () => { resolve(Buffer.concat(chunks)); });
stderr.once('data', data => { reject(String(data)); });
});
});
}

最新更新