Created
August 12, 2022 07:02
-
-
Save mnmly/2b6c8a69ff201c11f2c6ffdad5aff2ed to your computer and use it in GitHub Desktop.
pack-drawings.js #pdf #nodejs
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 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