Excel to PDF Converter | Free Online XLS/XLSX to PDF Tool | DailyTools
`;
excelPreview.style.display = 'block';
excelPlaceholder.style.display = 'none';
};
reader.readAsArrayBuffer(file);
}
async function convertToPdf() {
if (!excelFile || !workbook) {
alert('Please select an Excel file first.');
return;
}
progressContainer.style.display = 'block';
progressBar.style.width = '0%';
progressText.textContent = 'Processing Excel file...';
convertBtn.disabled = true;
try {
// Simulate processing steps with progress updates
progressBar.style.width = '30%';
progressText.textContent = 'Converting data...';
// Get the first sheet (for simplicity)
const firstSheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[firstSheetName];
// Convert to HTML
progressBar.style.width = '50%';
progressText.textContent = 'Generating PDF structure...';
const html = XLSX.utils.sheet_to_html(worksheet, { editable: false });
// Create a simple PDF (simplified version - in a real app you'd use a proper PDF library)
progressBar.style.width = '70%';
progressText.textContent = 'Creating PDF document...';
// This is a simplified version - in reality you'd use a proper PDF generation library
// with more sophisticated layout handling
const { PDFDocument, rgb } = PDFLib;
const pdfDoc = await PDFDocument.create();
// Add a page
const page = pdfDoc.addPage([595, 842]); // A4 size in points
// Draw some text (simplified representation)
page.drawText('Excel to PDF Conversion', {
x: 50,
y: 800,
size: 20,
color: rgb(0, 0, 0),
});
page.drawText('Document: ' + excelFile.name, {
x: 50,
y: 770,
size: 12,
color: rgb(0, 0, 0),
});
// Simulate table content
const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
let yPosition = 700;
const rowHeight = 15;
const colWidth = 100;
// Draw headers
if (jsonData.length > 0) {
const headers = jsonData[0];
let xPosition = 50;
headers.forEach((header, i) => {
page.drawText(header || `Column ${i+1}`, {
x: xPosition,
y: yPosition,
size: 10,
color: rgb(0, 0, 0),
});
xPosition += colWidth;
});
yPosition -= rowHeight;
}
// Draw rows
for (let i = 1; i < Math.min(jsonData.length, 20); i++) {
const row = jsonData[i];
let xPosition = 50;
row.forEach((cell, j) => {
page.drawText(cell !== undefined ? String(cell) : '', {
x: xPosition,
y: yPosition,
size: 8,
color: rgb(0, 0, 0),
});
xPosition += colWidth;
});
yPosition -= rowHeight;
if (yPosition < 50) break; // Prevent going off page
}
progressBar.style.width = '90%';
progressText.textContent = 'Finalizing PDF...';
const pdfBytes = await pdfDoc.save();
pdfUrl = URL.createObjectURL(new Blob([pdfBytes], { type: 'application/pdf' }));
pdfPreview.src = pdfUrl;
pdfPreview.style.display = 'block';
pdfPlaceholder.style.display = 'none';
progressBar.style.width = '100%';
progressText.textContent = 'Conversion complete!';
downloadBtn.disabled = false;
convertBtn.disabled = false;
} catch (error) {
console.error('Conversion error:', error);
progressText.textContent = 'Error during conversion. Please try again.';
convertBtn.disabled = false;
}
}
function downloadPdf() {
if (!pdfUrl) return;
const a = document.createElement('a');
a.href = pdfUrl;
a.download = excelFile.name.replace(/\.[^/.]+$/, '') + '.pdf';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
function resetTool() {
excelFile = null;
workbook = null;
pdfUrl = null;
fileInput.value = '';
fileInfo.textContent = 'No file selected (Supports XLSX, XLS, CSV)';
previewContainer.style.display = 'none';
excelPreview.srcdoc = '';
excelPreview.style.display = 'none';
excelPlaceholder.style.display = 'flex';
pdfPreview.src = '';
pdfPreview.style.display = 'none';
pdfPlaceholder.style.display = 'flex';
downloadBtn.disabled = true;
progressContainer.style.display = 'none';
convertBtn.disabled = false;
// Reset options to defaults
landscapeOption.checked = true;
fitToPage.checked = true;
includeGridlines.checked = true;
repeatHeaders.checked = true;
pageSize.value = 'A4';
marginSize.value = '10';
}
});