Last active
May 28, 2021 19:59
-
-
Save rarysson/b5fe2b219d1a4ba87d7bc61eb7cb7c31 to your computer and use it in GitHub Desktop.
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
function generatePDF() { | |
const doc = new jsPDF(); | |
const defaultFont = doc.getFont(); | |
const baseMargin = 8; | |
const marginBetweenSections = 2; | |
const smallFont = 7; | |
const mediumFont = 8; | |
const colors = { | |
primary: '#000000', | |
secondary: '#002060', | |
gray: '#d0cece', | |
lightGray: '#f2f2f2', | |
white: '#ffffff', | |
}; | |
const docSize = { | |
height: 297, | |
width: 210, | |
}; | |
const offsetYRect = 1; | |
let currentY = baseMargin; | |
const sectionRectDimensions = { | |
x: baseMargin, | |
width: docSize.width - baseMargin * 2, | |
height: 4, | |
}; | |
const checkPageEnd = (y) => { | |
if (y > docSize.height - baseMargin * 2) { | |
doc.addPage(); | |
return baseMargin; | |
} | |
return y; | |
}; | |
const addSectionHeader = (sectionTitle) => { | |
currentY += marginBetweenSections; | |
currentY = checkPageEnd(currentY); | |
const sectionY = currentY; | |
const center = { | |
x: sectionRectDimensions.x + Math.floor(sectionRectDimensions.width / 2), | |
y: sectionY + Math.floor(sectionRectDimensions.height / 2) + offsetYRect, | |
}; | |
doc | |
.setFont(defaultFont.fontName, 'bold') | |
.setFontSize(mediumFont) | |
.setTextColor(colors.primary) | |
.setDrawColor(colors.primary) | |
.setFillColor(colors.gray) | |
.rect(sectionRectDimensions.x, sectionY, sectionRectDimensions.width, sectionRectDimensions.height, 'FD') | |
.text(sectionTitle, center.x, center.y, { align: 'center' }); | |
currentY += marginBetweenSections * 3; | |
currentY = checkPageEnd(currentY); | |
}; | |
const generateColumn = (content, styles = {}, extraOptions = {}) => ({ content, styles, ...extraOptions }); | |
const generateTable = (dataBody, dataHead, extraOptions = {}) => { | |
const head = dataHead ? { head: dataHead } : {}; | |
doc.autoTable({ | |
...head, | |
body: dataBody, | |
theme: 'plain', | |
styles: { | |
fontSize: smallFont, | |
textColor: colors.primary, | |
}, | |
headStyles: { | |
fontSize: smallFont, | |
fontStyle: 'bold', | |
textColor: colors.primary, | |
fillColor: colors.white, | |
halign: 'center', | |
}, | |
startY: currentY, | |
margin: { left: baseMargin }, | |
tableWidth: docSize.width - baseMargin * 2, | |
tableLineWidth: 0.1, | |
tableLineColor: colors.primary, | |
...extraOptions, | |
}); | |
currentY = doc.lastAutoTable.finalY; | |
currentY = checkPageEnd(currentY); | |
}; | |
const randomColumns = (x) => { return Array(x).fill( | |
[ | |
generateColumn('RANDOM DATA'), | |
], | |
) } | |
addSectionHeader('RANDOM TITLE'); | |
generateTable([...randomColumns(10)]); | |
addSectionHeader('RANDOM TITLE'); | |
generateTable([...randomColumns(50)]); | |
addSectionHeader('RANDOM TITLE'); | |
generateTable([...randomColumns(50)]); | |
doc.save('random.pdf'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment