This piece of code:
var fs = require('fs');
PdfDocument = require('pdfkit');
var doc = new PdfDocument();
doc.text('asdf');
doc.image('./rid12.png',0,0);
doc.pipe(fs.createWriteStream('./test1.pdf'));
doc.end();
fails on rid12.png this way:
Error: Invalid filter algorithm: 255
at c:UsersstivDropboxProjectscanvasjsClients.devEditortrunknode_modulespdfkitnode_modulespng-jspng-node.js:242:21

@stiv-yakovenko reported this also on pngjs2 which I fixed.
I see you are using png-js your own decoder, which doesn't support interlace. This is an interlace file. You could consider switching to pngjs2 if you want to support interlace files and different bit sizes.
Ok, but how can I do this switching? I tried to fix png.js of pdfkit, but there is no way to access palette in async mode.
@lukeapage I'm also wondering how to make the switch to pngjs for pdfkit. Is this something a user of pdfkit can do or does pdfkit itself have to make the switch?
Well, I've solved the problem for my own purposes. Though I don't have enough resourses to publish this as opensource... :(
+1 I have problems with some PNG's. png-js used in this project is outdated and doesn't load PNG images properly :( I can make PR with better parser
I am ready to submit it to opensource if someone pays me....
+1
Did you ever make the PR @kazkovsky?
@alexbirkett No i don't have time right now, because we are releasing new product. I managed to change PNG's color type from palette based to rgba and this helped.
My nasty workaround is to re-encode pngs with https://www.npmjs.com/package/gm before sending them to pdfkit
I came across the same problem today and wrote a decent workaround using jimp
let doc = new pdfkit();
let image = await Jimp.read('images/logo.png');
image.getBuffer(Jimp.MIME_PNG, (err, result) => {
doc.image(result, 0,200, {width: 20, height: 20})
doc.end();
doc.pipe(res);
});
Even I also faced the same problem but with pdfmake and I tried so many packages but the best way to do is to extract the buffer and then assign it.
At first extract image buffer by jimp and then assign it and then print your pdf:
Jimp.read( URL , function (err, image) {
image.getBuffer(Jimp.MIME_PNG, function(err, result) {
// Now assign this result to your pdf image area
// For pdfmake {image: result }
});
});
Same problem.
Quick solution:
check if png with image-type
remove interlacing with pngjs
const fs = require('fs');
const { PNG } = require('pngjs');
const imageType = require('image-type');
const buffer = fs.readFileSync('in.png');
const { mime } = imageType(buffer);
switch (mime) {
case 'image/png':
const png = PNG.sync.read(buffer);
if (png.interlace) {
buffer = PNG.sync.write(png, { interlace: false });
}
break;
}
// Then you can pass buffer to the pdfkit
Is this getting fixed or should we fix ourselves ?
Based on @sajjad-shirazy answer
const fs = require('fs')
const { PNG } = require('pngjs')
const imageType = require('image-type')
const removePngInterlacing = file => {
const buffer = fs.readFileSync(file)
const { mime } = imageType(buffer)
if (mime === 'image/png') {
const png = PNG.sync.read(buffer)
if (png.interlace) {
return PNG.sync.write(png, { interlace: false })
}
}
return file
}
module.exports = removePngInterlacing
Is there a way to catch this error programmatically? The doc.image(...) and doc.end() don't throw any catchable errors, but callback in stream.on('finish', callback) never is being called.
Most helpful comment