Has anyone achieved a success in converting a PNG to PDF via backend workflow in bubble?
I have a code but cannot properly execute it, nor I receive an error. I assume saving can be a problem but to be honest, I am not sure how to grab the error.
Any experience there?
const { PDFDocument, rgb } = require(‘pdf-lib’);
const fs = require(‘fs’);
async function pngToPdf(pngFilePath, outputPdfPath) {
const pdfDoc = await PDFDocument.create();
const pngImageBytes = fs.readFileSync(pngFilePath);
const pngImage = await pdfDoc.embedPng(pngImageBytes);
const pngDims = pngImage.scale(1);
const page = pdfDoc.addPage([pngDims.width, pngDims.height]);
page.drawImage(pngImage, {
x: 0,
y: 0,
width: pngDims.width,
height: pngDims.height,
});
const pdfBytes = await pdfDoc.save();
fs.writeFileSync(outputPdfPath, pdfBytes);
}
const pngFilePath = ‘DO A SEARCH FOR FILE INPUT’;
const outputPdfPath = ‘DO A SEARCH FOR FILE OUTPUT’;
pngToPdf(pngFilePath, outputPdfPath);