Skip to content

Instantly share code, notes, and snippets.

@gubleo
Created May 15, 2023 19:42
Show Gist options
  • Save gubleo/dc09aa96d6070393628a446c82949d80 to your computer and use it in GitHub Desktop.
Save gubleo/dc09aa96d6070393628a446c82949d80 to your computer and use it in GitHub Desktop.
Unifica imagens
const fs = require('fs');
const { PDFDocument, rgb } = require('pdf-lib');
async function criarPDFComImagens(imagens, nomeArquivo) {
const pdfDoc = await PDFDocument.create();
for (const imagem of imagens) {
const imagemBytes = fs.readFileSync(imagem);
const imagemObj = await pdfDoc.embedPng(imagemBytes);
const pagina = pdfDoc.addPage();
const { width, height } = imagemObj.scale(0.5);
pagina.drawImage(imagemObj, {
x: pagina.getWidth() / 2 - width / 2,
y: pagina.getHeight() / 2 - height / 2,
width,
height,
opacity: 0.7,
});
}
const pdfBytes = await pdfDoc.save();
fs.writeFileSync(nomeArquivo, pdfBytes);
console.log(`PDF criado: ${nomeArquivo}`);
}
const imagens = ['1.png', '2.png', '3.png '];
const nomeArquivo = 'imagens.pdf';
criarPDFComImagens(imagens, nomeArquivo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment