Skip to content

Instantly share code, notes, and snippets.

@th91vi
Last active November 20, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save th91vi/21af518ec2819417c2d7a9ef9e99672d to your computer and use it in GitHub Desktop.
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
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