Im working with playwright and i get the image from screenshot:
const image = await elementHandle.screenshot({
fullPage : true,
clip: {
width: 1200,
height: 1500,
}
});
It returns me the buffer of an image. So i need to convert the image buffer to pdf buffer.
Already used some plugins but it saves the pdf, and i need just the buffer of it (without saving it).
Like this plugin:
const pages = [
image
]
imgToPDF(pages, 'A4').pipe(fs.createWriteStream('temp.pdf'));
Any ideas?
I've made it with help of the 'canvas' package: https://www.npmjs.com/package/canvas
import Canvas from 'canvas';
function imageToPdf(imageBuffer) {
const img = new Canvas.Image();
img.src = imageBuffer;
const canvas = Canvas.createCanvas(img.width, img.height, 'pdf');
const context = canvas.getContext('2d');
context.drawImage(img, 0, 0, img.width, img.height);
return canvas.toBuffer();
}