Created
June 20, 2023 15:42
-
-
Save gaulatti/c11d93ee0c82f34576be4420eed462dd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { PDFDocument, PageSizes, rgb } = require('pdf-lib'); | |
const fs = require('fs'); | |
const pageSize = PageSizes.Legal; | |
const sheetSize = PageSizes.Legal[1] / 3; | |
const offset = 20; | |
const createBox = (page) => { | |
page.drawRectangle({ | |
x: offset, | |
y: pageSize[1] - offset - (pageSize[1] - offset * 2), | |
width: pageSize[0] - offset * 2, | |
height: pageSize[1] - offset * 2, | |
borderWidth: 1, | |
borderColor: rgb(0, 0, 0), | |
}); | |
page.drawLine({ | |
start: { x: offset * 4, y: pageSize[1] - offset }, | |
end: { x: offset * 4, y: offset }, | |
color: rgb(0, 0, 0), | |
}); | |
}; | |
const createSheet = (page, number = 1) => { | |
page.drawLine({ | |
start: { x: offset, y: pageSize[1] - offset - sheetSize * number }, | |
end: { | |
x: pageSize[0] - offset, | |
y: pageSize[1] - offset - sheetSize * number, | |
}, | |
color: rgb(0, 0, 0), | |
}); | |
page.drawLine({ | |
start: { | |
x: offset, | |
y: pageSize[1] - offset - sheetSize * number + sheetSize / 2, | |
}, | |
end: { | |
x: offset * 2, | |
y: pageSize[1] - offset - sheetSize * number + sheetSize / 2, | |
}, | |
color: rgb(1, 0, 0), | |
}); | |
}; | |
/** | |
* Sheets must be: | |
* All margins 0.2in | |
* Page Size 7in x 4.6in | |
*/ | |
const embedSheet = async (pdfDoc, page, pageSource, lane = 1) => { | |
if(pageSource) { | |
const processedSource = await pdfDoc.embedPage(pageSource); | |
page.drawPage(processedSource, { | |
x: offset * 4, | |
y: pageSize[1] - offset - (sheetSize * lane), | |
opacity: 1, | |
}); | |
} | |
} | |
async function createLegalSizePDF() { | |
const pdfDoc = await PDFDocument.create(); | |
const externalPdfBytes = fs.readFileSync('input.pdf'); | |
const externalPdfDoc = await PDFDocument.load(externalPdfBytes); | |
const externalSheets = externalPdfDoc.getPages(); | |
const sheetsPerPage = pageSize == PageSizes.Legal ? 3 : 2; | |
const pages = Math.ceil(externalSheets.length / sheetsPerPage); | |
for (let i = 0; i < pages; i++) { | |
const page = pdfDoc.addPage(pageSize); | |
await embedSheet(pdfDoc, page, externalSheets.shift()); | |
await embedSheet(pdfDoc, page, externalSheets.shift(), 2); | |
if (pageSize == PageSizes.Legal) { | |
await embedSheet(pdfDoc, page, externalSheets.shift(), 3); | |
} | |
createBox(page); | |
createSheet(page); | |
createSheet(page, 2); | |
if (pageSize == PageSizes.Legal) { | |
createSheet(page, 3); | |
} | |
} | |
const pdfBytes = await pdfDoc.save(); | |
fs.writeFileSync('legal_size.pdf', pdfBytes); | |
} | |
createLegalSizePDF().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment