Skip to content

Instantly share code, notes, and snippets.

@mnmly
Created August 12, 2022 07:02
Show Gist options
  • Save mnmly/2b6c8a69ff201c11f2c6ffdad5aff2ed to your computer and use it in GitHub Desktop.
Save mnmly/2b6c8a69ff201c11f2c6ffdad5aff2ed to your computer and use it in GitHub Desktop.
pack-drawings.js #pdf #nodejs
const fs = require('fs')
const PDFLib = require('pdf-lib')
const filepath = './220812_竣工図/220812_AUO_竣工図整理.pdf'
const PDFDocument = PDFLib.PDFDocument
const productionDwgFilePaths = [
'../08_issued/SETUP/220713/220713_SDR-2_commented.pdf',
'../08_issued/SETUP/220714/220714_F4-3_commented.pdf',
'../08_issued/SETUP/220722/220722_AUO_元赤坂建具図_0721_commented.pdf',
]
const run = async function() {
const pdfData = fs.readFileSync(filepath)
const srcDoc = await PDFDocument.load(pdfData)
// Load drawing files
const productionDwgDocs = await Promise.all((productionDwgFilePaths.map(async (filepath) => {
return await PDFDocument.load(fs.readFileSync(filepath))
})))
// Load pages
const productionDwgPages = await Promise.all(productionDwgDocs.map(async (doc) => {
let indices = Array.from(Array(doc.getPages().length).keys())
return await srcDoc.copyPages(doc, indices)
}))
// Insert pages
productionDwgPages.flat().forEach((p) => {
srcDoc.insertPage(srcDoc.getPages().length, p)
})
// Grab binary
const pdfBytes = await srcDoc.save()
// Write it out
fs.writeFile(filepath.replace(/\.pdf$/, '_packed.pdf'), pdfBytes, (err) => {
err && console.log(err)
})
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment