App.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
import React, { useState } from 'react'; import * as XLSX from 'xlsx'; import { PDFDocument, rgb, StandardFonts } from 'pdf-lib'; import "bootstrap/dist/css/bootstrap.min.css" function App() { const [excelData, setExcelData] = useState([]); // Handle Excel file upload const handleFileUpload = (event) => { const file = event.target.files[0]; const reader = new FileReader(); reader.onload = (e) => { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: 'array' }); const worksheet = workbook.Sheets[workbook.SheetNames[0]]; const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); setExcelData(jsonData); }; reader.readAsArrayBuffer(file); }; // Convert Excel data to PDF with table-like structure and download const handleDownloadPdf = async () => { const pdfDoc = await PDFDocument.create(); const page = pdfDoc.addPage([600, 800]); const font = await pdfDoc.embedFont(StandardFonts.Helvetica); const fontSize = 10; // Table positioning and dimensions const cellPadding = 5; const cellHeight = 20; let xPosition = 50; let yPosition = 750; const tableWidth = 500; excelData.forEach((row, rowIndex) => { const numCells = row.length; const cellWidth = tableWidth / numCells; row.forEach((cell, cellIndex) => { const cellText = cell ? String(cell) : ""; // Draw cell border page.drawRectangle({ x: xPosition + cellIndex * cellWidth, y: yPosition, width: cellWidth, height: cellHeight, borderColor: rgb(0.75, 0.75, 0.75), borderWidth: 1, }); // Draw cell text, center-aligned within each cell page.drawText(cellText, { x: xPosition + cellIndex * cellWidth + cellPadding, y: yPosition + cellPadding, size: fontSize, font, color: rgb(0, 0, 0), maxWidth: cellWidth - cellPadding * 2, }); }); // Move to the next row position yPosition -= cellHeight; }); // Save the PDF and trigger download const pdfBytes = await pdfDoc.save(); const blob = new Blob([pdfBytes], { type: 'application/pdf' }); const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = 'excel_to_pdf_table.pdf'; link.click(); }; return ( <div className="container mt-4"> <h1>Convert Excel to PDF with Table Structure</h1> <input type="file" accept=".xlsx, .xls" onChange={handleFileUpload} className="form-control mb-4" /> {excelData.length > 0 && ( <div> <h2>Excel Preview</h2> <table className="table table-bordered"> <tbody> {excelData.map((row, rowIndex) => ( <tr key={rowIndex}> {row.map((cell, cellIndex) => ( <td key={cellIndex}>{cell}</td> ))} </tr> ))} </tbody> </table> <button onClick={handleDownloadPdf} className="btn btn-primary mt-3"> Download as PDF </button> </div> )} </div> ); } export default App; |