Last active
November 20, 2020 19:35
-
-
Save th91vi/21af518ec2819417c2d7a9ef9e99672d to your computer and use it in GitHub Desktop.
Function to generate on the fly PDF containing tables, using jsPDF, jsPDF-autotable and moment
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
import jsPDF from 'jspdf'; | |
import 'jspdf-autotable'; | |
import moment from 'moment'; | |
const generateTableReport = (data) => { | |
// assuming your data is an array, inside an object | |
const { entries } = data; | |
const report = jsPDF(); | |
const tableColumn = ['Column 1', 'Column 2', 'Column 3', 'Column 4']; | |
const tableRows = []; | |
entries.forEach((entry) => { | |
const detailData = [ | |
// properties for example | |
entry.title, | |
entry.name, | |
entry.score, | |
moment(entry.date).format('LLL'), | |
]; | |
tableRows.push(detailData); | |
}); | |
const date = Date().split(' '); | |
const dateStr = date[0] + date[1] + date[2] + date[3] + date[4]; | |
report.autoTable(tableColumn, tableRows, { startY: 20, theme: 'plain' }); | |
report.save(`report__${dateStr}.pdf`); | |
}; | |
export default generateTableReport; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment