pdfjs
pdfjs copied to clipboard
How can I use png files in this API?
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
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).
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' });