html
<!DOCTYPE html>
<html lang="en" class="h-full bg-gray-100">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Image & PDF Converter</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Bootstrap CSS CDN -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- PDF.js CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.9.179/pdf.min.js"></script>
</head>
<body class="h-full flex flex-col items-center p-6">
<h1 class="text-3xl font-bold mb-6">Image & PDF Converter</h1>
<div class="container max-w-3xl bg-white p-6 rounded shadow-md w-full">
<ul class="nav nav-tabs mb-4" id="converterTabs" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="img2pdf-tab" data-bs-toggle="tab" data-bs-target="#img2pdf" type="button" role="tab" aria-controls="img2pdf" aria-selected="true">Image to PDF</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="pdf2img-tab" data-bs-toggle="tab" data-bs-target="#pdf2img" type="button" role="tab" aria-controls="pdf2img" aria-selected="false">PDF to Image</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="history-tab" data-bs-toggle="tab" data-bs-target="#history" type="button" role="tab" aria-controls="history" aria-selected="false">Download History</button>
</li>
</ul>
<div class="tab-content" id="converterTabsContent">
<!-- Image to PDF -->
<div class="tab-pane fade show active" id="img2pdf" role="tabpanel" aria-labelledby="img2pdf-tab">
<div class="mb-3">
<label for="imageFiles" class="form-label">Select Images (JPG, PNG, GIF)</label>
<input class="form-control" type="file" id="imageFiles" accept="image/*" multiple />
</div>
<button id="convertImgToPdf" class="btn btn-primary">Convert to PDF</button>
</div>
<!-- PDF to Image -->
<div class="tab-pane fade" id="pdf2img" role="tabpanel" aria-labelledby="pdf2img-tab">
<div class="mb-3">
<label for="pdfFile" class="form-label">Select PDF File</label>
<input class="form-control" type="file" id="pdfFile" accept="application/pdf" />
</div>
<button id="convertPdfToImg" class="btn btn-primary">Convert First Page to Image</button>
<div id="pdfImageResult" class="mt-4"></div>
</div>
<!-- Download History -->
<div class="tab-pane fade" id="history" role="tabpanel" aria-labelledby="history-tab">
<h3 class="mb-3">Your Download History</h3>
<ul id="downloadHistory" class="list-group"></ul>
<button id="clearHistory" class="btn btn-danger mt-3">Clear History</button>
</div>
</div>
</div>
<!-- jsPDF CDN for image to PDF -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script>
// Utility: Cookie functions
function setCookie(name, value, days = 365) {
const d = new Date();
d.setTime(d.getTime() + days*24*60*60*1000);
document.cookie = name + "=" + encodeURIComponent(value) + ";path=/;expires=" + d.toUTCString();
}
function getCookie(name) {
const cookies = document.cookie.split('; ');
for (const c of cookies) {
const [key, val] = c.split('=');
if (key === name) return decodeURIComponent(val);
}
return null;
}
// Download history management
function addToHistory(filename, type) {
let history = JSON.parse(getCookie('downloadHistory') || '[]');
history.unshift({ filename, type, date: new Date().toLocaleString() });
if (history.length > 20) history.pop(); // limit history size
setCookie('downloadHistory', JSON.stringify(history));
renderHistory();
}
function renderHistory() {
const history = JSON.parse(getCookie('downloadHistory') || '[]');
const ul = document.getElementById('downloadHistory');
ul.innerHTML = '';
if (history.length === 0) {
ul.innerHTML = '<li class="list-group-item">No downloads yet.</li>';
return;
}
for (const item of history) {
const li = document.createElement('li');
li.className = 'list-group-item d-flex justify-content-between align-items-center';
li.innerHTML = `<span>${item.filename} <small class="text-muted">(${item.type})</small></span><span class="text-muted">${item.date}</span>`;
ul.appendChild(li);
}
}
document.getElementById('clearHistory').addEventListener('click', () => {
setCookie('downloadHistory', '[]', -1);
renderHistory();
});
// Image to PDF conversion
document.getElementById('convertImgToPdf').addEventListener('click', async () => {
const files = document.getElementById('imageFiles').files;
if (!files.length) {
alert('Please select at least one image.');
return;
}
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
for (let i = 0; i < files.length; i++) {
const file = files[i];
const imgData = await readFileAsDataURL(file);
const img = new Image();
await new Promise((res) => {
img.onload = res;
img.src = imgData;
});
// Calculate dimensions to fit A4 (210 x 297 mm)
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
let width = img.width;
let height = img.height;
// Scale image to fit page
if (width > height) {
if (width > pdfWidth) {
height = (pdfWidth / width) * height;
width = pdfWidth;
}
} else {
if (height > pdfHeight) {
width = (pdfHeight / height) * width;
height = pdfHeight;
}
}
if (i > 0) pdf.addPage();
pdf.addImage(imgData, 'JPEG', (pdfWidth - width) / 2, (pdfHeight - height) / 2, width, height);
}
const pdfBlob = pdf.output('blob');
const url = URL.createObjectURL(pdfBlob);
const filename = `converted_${Date.now()}.pdf`;
downloadFile(url, filename);
addToHistory(filename, 'PDF');
URL.revokeObjectURL(url);
});
// PDF to Image conversion (first page only)
document.getElementById('convertPdfToImg').addEventListener('click', async () => {
const file = document.getElementById('pdfFile').files[0];
if (!file) {
alert('Please select a PDF file.');
return;
}
const arrayBuffer = await file.arrayBuffer();
const loadingTask = pdfjsLib.getDocument({ data: arrayBuffer });
const pdf = await loadingTask.promise;
const page = await pdf.getPage(1);
const viewport = page.getViewport({ scale: 2 });
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
await page.render({ canvasContext: context, viewport }).promise;
const imgDataUrl = canvas.toDataURL('image/png');
const resultDiv = document.getElementById('pdfImageResult');
resultDiv.innerHTML = `<h5>First Page Image:</h5><img src="${imgDataUrl}" alt="PDF page image" class="border rounded" />`;
// Download image
const filename = `pdf_page1_${Date.now()}.png`;
downloadFile(imgDataUrl, filename);
addToHistory(filename, 'Image');
});
// Helper: read file as DataURL
function readFileAsDataURL(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(file);
});
}
// Helper: download file
function downloadFile(url, filename) {
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
}
// Initial render of history
renderHistory();
</script>
<!-- Bootstrap JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>