Created
June 19, 2023 20:39
-
-
Save MohammedALREAI/74517517a8cd4c2467a1f34c14b192f8 to your computer and use it in GitHub Desktop.
test
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
import { jsPDF as JsPDF } from "jspdf"; | |
import 'jspdf-autotable'; | |
import * as XLSX from 'xlsx'; | |
const handleDownloadpdf = useCallback(() => { | |
const selectedRows = []; | |
dataFiltered.forEach((row) => { | |
if (selected.includes(row.id)) { | |
selectedRows.push(row); | |
} | |
}); | |
const doc = new JsPDF(); | |
const title = 'Invoice List'; | |
const titleFontSize = 18; | |
const { width, height } = doc.internal.pageSize; | |
// Calculate the position to center the title | |
const titleWidth = doc.getStringUnitWidth(title) * titleFontSize / doc.internal.scaleFactor; | |
const titleX = (width - titleWidth) / 2; | |
const titleY = 30; // Adjust the vertical position of the title as needed | |
// Add the logo image to the PDF | |
const logoWidth = 50; // Adjust the width of the logo as needed | |
const logoHeight = 50; // Adjust the height of the logo as needed | |
const logoX = (width - logoWidth) / 2; | |
const logoY = titleY - logoHeight - 10; // Adjust the vertical position of the logo as needed | |
doc.addImage("https://minimal-assets-api-dev.vercel.app/assets/images/avatars/avatar_1.jpg", 'PNG', logoX, logoY, logoWidth, logoHeight); | |
// Set the title font size and align it to center | |
doc.setFontSize(titleFontSize); | |
doc.text(title, titleX, titleY, { align: 'center' }); | |
// Add a new page for the table | |
doc.addPage(); | |
doc.autoTable({ | |
head: [ | |
[ | |
{ content: 'Invoice Number', styles: { halign: 'center', fillColor: [41, 128, 185], textColor: [255, 255, 255] } }, | |
{ content: 'Create Date', styles: { halign: 'center', fillColor: [41, 128, 185], textColor: [255, 255, 255] } }, | |
{ content: 'Due Date', styles: { halign: 'center', fillColor: [41, 128, 185], textColor: [255, 255, 255] } }, | |
{ content: 'Price', styles: { halign: 'center', fillColor: [41, 128, 185], textColor: [255, 255, 255] } }, | |
], | |
], | |
body: selectedRows.map((item) => Object.values(item)), | |
}); | |
// Save the PDF | |
doc.save('table.pdf'); | |
}, [dataFiltered, selected]); | |
// DOWNLOD XLSX | |
const handleDownloadXlsx = useCallback(() => { | |
let selectedRows = []; | |
dataFiltered.forEach((row) => { | |
if (selected.length === 0) { | |
selectedRows = dataFiltered; | |
return; | |
} | |
if (selected.includes(row.id)) { | |
selectedRows.push(row); | |
} | |
}); | |
const workSheet = XLSX.utils.json_to_sheet(selectedRows); | |
const workBook = XLSX.utils.book_new(); | |
// Add a title to the first row | |
XLSX.utils.sheet_add_aoa(workSheet, [['Invoice Number', 'Create Date', 'Due Date', 'Price']], { origin: -1 }); | |
XLSX.utils.book_append_sheet(workBook, workSheet, 'SheetName'); | |
XLSX.writeFile(workBook, 'table.xlsx'); | |
}, [dataFiltered, selected]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment