Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[nodejs] Extract first page of a PDF using pdf-lib

01
02
03
04
05
06
07
08
09
10
11
const { PDFDocument } = require('pdf-lib')
 
// file = { fileName: 'test1.pdf, content: arraybuffer }    
 
const originalPdf = await PDFDocument.load(file.content, { ignoreEncryption: true })
const newPdf = await PDFDocument.create()
const [firstPage] = await newPdf.copyPages(originalPdf, [0]) // <-- 0 is the first page
newPdf.addPage(firstPage)
const firstPagePdf = await newPdf.save()
 
file.content = Buffer.from(firstPagePdf)