Created
May 15, 2023 19:42
-
-
Save gubleo/dc09aa96d6070393628a446c82949d80 to your computer and use it in GitHub Desktop.
Unifica imagens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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