-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpng_to_pdf.js
More file actions
32 lines (24 loc) · 789 Bytes
/
png_to_pdf.js
File metadata and controls
32 lines (24 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const fs = require('fs');
const PDFDocument = require('pdf-lib').PDFDocument;
async function pngToPdf(pngPath, pdfPath) {
// Baca file PNG
const pngBuffer = fs.readFileSync(pngPath);
// Buat PDFDocument baru
const pdfDoc = new PDFDocument();
// Tambahkan gambar ke PDFDocument
const pngImage = await pdfDoc.embedPNG(pngBuffer);
// Tambahkan halaman baru ke PDFDocument
const page = pdfDoc.addPage([pngImage.width, pngImage.height]);
// Tempelkan gambar ke halaman
page.drawImage(pngImage, {
x: 0,
y: 0,
width: pngImage.width,
height: pngImage.height,
});
// Generate buffer dari PDFDocument
const pdfBuffer = await pdfDoc.save();
// Tulis buffer ke file PDF
fs.writeFileSync(pdfPath, pdfBuffer);
}
pngToPdf('input.png', 'output.pdf');