pdfjs icon indicating copy to clipboard operation
pdfjs copied to clipboard

How can I use png files in this API?

Open a9293340 opened this issue 3 years ago • 2 comments

Hi, this API is useful and solve many problems. In my project, I need to use png file to make a pdf. But new pdf.Image() can only use 'jpg' or 'pdf' types. What should I do to solve this problem

a9293340 avatar Dec 30 '20 03:12 a9293340

I am afraid that PNG support needs to be implemented and added to pdfjs either by me or via a contribution by someone else. It is actually a missing feature for a while now https://github.com/rkusa/pdfjs/issues/40. There is also no ETA for it, since I personally don't have a need for it, because I convert all image to JPEG before adding them to PDFs to reduce the resulting file size of the PDF, which might also be a solution for you (if you don't mind converting to a lossy image format and losing transparency).

rkusa avatar Dec 30 '20 10:12 rkusa

Can use lovell's sharp easily

https://github.com/lovell/sharp

const sharp = require('sharp');
const image_buffer = fs.readFileSync(image_path);
const image_buffer_jpeg = await sharp(image_buffer).resize(240, 144, { fit: 'contain' }).jpeg().toBuffer();
const pdf_image = new pdf.Image(image_buffer_jpeg);
pdf_document.image(pdf_image, { align: 'center' });

joshxyzhimself avatar Mar 09 '21 21:03 joshxyzhimself