PDF Toolkit Web Application
Based on your request, I'll create a web application for a comprehensive PDF toolkit. Here's the code for a responsive, feature-rich PDF toolkit that uses HTML, CSS, JavaScript with Tailwind and Shadcn UI components:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ultimate PDF Toolkit</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<script src="https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js"></script>
<script src="https://unpkg.com/pdfjs-dist@2.14.305/build/pdf.min.js"></script>
<script src="https://unpkg.com/downloadjs@1.4.7/download.min.js"></script>
</head>
<body class="bg-gray-100 min-h-screen">
<header class="bg-blue-600 text-white shadow-lg">
<div class="container mx-auto px-4 py-6">
<h1 class="text-3xl font-bold mb-2">Ultimate PDF Toolkit</h1>
<p class="text-xl">View, Edit, Compress, & More - All in One Solution</p>
</div>
</header>
<main class="container mx-auto px-4 py-8">
<!-- File Upload Section -->
<section class="bg-white rounded-lg shadow-md p-6 mb-8">
<h2 class="text-2xl font-semibold mb-4">Upload Your PDF</h2>
<div class="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center">
<input type="file" id="pdf-upload" accept=".pdf" class="hidden">
<label for="pdf-upload" class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-md cursor-pointer transition-colors">
Choose PDF File
</label>
<p class="mt-2 text-gray-500" id="file-name">No file selected</p>
</div>
</section>
<!-- Tools Container -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-6" id="tools-container" style="display: none;">
<!-- PDF Viewer -->
<section class="bg-white rounded-lg shadow-md p-6 col-span-1 md:col-span-2">
<h2 class="text-2xl font-semibold mb-4">PDF Viewer</h2>
<div class="flex justify-center space-x-4 mb-4">
<button id="prev-page" class="bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-md">Previous</button>
<span id="page-info" class="px-4 py-2">Page 1 of 1</span>
<button id="next-page" class="bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-md">Next</button>
<button id="zoom-in" class="bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-md">Zoom In</button>
<button id="zoom-out" class="bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-md">Zoom Out</button>
<button id="rotate" class="bg-gray-200 hover:bg-gray-300 px-4 py-2 rounded-md">Rotate</button>
</div>
<div class="border border-gray-300 rounded-lg overflow-auto h-96 flex justify-center">
<canvas id="pdf-canvas"></canvas>
</div>
</section>
<!-- Resize and Compress Tool -->
<section class="bg-white rounded-lg shadow-md p-6">
<h2 class="text-2xl font-semibold mb-4">Resize & Compress</h2>
<div class="space-y-4">
<div>
<label class="block text-gray-700 mb-2">Resize Options</label>
<select id="resize-preset" class="w-full p-2 border border-gray-300 rounded-md">
<option value="custom">Custom Size</option>
<option value="a4">A4 (210×297mm)</option>
<option value="letter">Letter (8.5×11in)</option>
<option value="legal">Legal (8.5×14in)</option>
</select>
</div>
<div id="custom-size" class="flex space-x-2">
<div class="w-1/2">
<label class="block text-gray-700 mb-2">Width (px)</label>
<input type="number" id="width" value="595" class="w-full p-2 border border-gray-300 rounded-md">
</div>
<div class="w-1/2">
<label class="block text-gray-700 mb-2">Height (px)</label>
<input type="number" id="height" value="842" class="w-full p-2 border border-gray-300 rounded-md">
</div>
</div>
<div>
<label class="block text-gray-700 mb-2">Compression Level</label>
<input type="range" id="compression" min="0" max="100" value="70" class="w-full">
<div class="flex justify-between text-sm text-gray-500">
<span>Low Quality</span>
<span>High Quality</span>
</div>
</div>
<button id="resize-compress-btn" class="w-full bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-md transition-colors">
Resize & Compress PDF
</button>
</div>
</section>
<!-- Crop Tool -->
<section class="bg-white rounded-lg shadow-md p-6">
<h2 class="text-2xl font-semibold mb-4">Crop PDF</h2>
<div class="space-y-4">
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-gray-700 mb-2">Left Margin</label>
<input type="number" id="crop-left" value="0" class="w-full p-2 border border-gray-300 rounded-md">
</div>
<div>
<label class="block text-gray-700 mb-2">Top Margin</label>
<input type="number" id="crop-top" value="0" class="w-full p-2 border border-gray-300 rounded-md">
</div>
<div>
<label class="block text-gray-700 mb-2">Right Margin</label>
<input type="number" id="crop-right" value="0" class="w-full p-2 border border-gray-300 rounded-md">
</div>
<div>
<label class="block text-gray-700 mb-2">Bottom Margin</label>
<input type="number" id="crop-bottom" value="0" class="w-full p-2 border border-gray-300 rounded-md">
</div>
</div>
<div>
<label class="block text-gray-700 mb-2">Apply To</label>
<select id="crop-apply" class="w-full p-2 border border-gray-300 rounded-md">
<option value="all">All Pages</option>
<option value="current">Current Page Only</option>
<option value="range">Page Range</option>
</select>
</div>
<div id="page-range" class="flex space-x-2" style="display: none;">
<div class="w-1/2">
<label class="block text-gray-700 mb-2">From Page</label>
<input type="number" id="range-from" value="1" min="1" class="w-full p-2 border border-gray-300 rounded-md">
</div>
<div class="w-1/2">
<label class="block text-gray-700 mb-2">To Page</label>
<input type="number" id="range-to" value="1" min="1" class="w-full p-2 border border-gray-300 rounded-md">
</div>
</div>
<button id="crop-btn" class="w-full bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-md transition-colors">
Crop PDF
</button>
</div>
</section>
<!-- Page Numbers Tool -->
<section class="bg-white rounded-lg shadow-md p-6">
<h2 class="text-2xl font-semibold mb-4">Add Page Numbers</h2>
<div class="space-y-4">
<div>
<label class="block text-gray-700 mb-2">Numbering Style</label>
<select id="numbering-style" class="w-full p-2 border border-gray-300 rounded-md">
<option value="arabic">1, 2, 3, ...</option>
<option value="roman">i, ii, iii, ...</option>
<option value="alphabetic">A, B, C, ...</option>
</select>
</div>
<div>
<label class="block text-gray-700 mb-2">Position</label>
<select id="position" class="w-full p-2 border border-gray-300 rounded-md">
<option value="bottom-center">Bottom Center</option>
<option value="bottom-right">Bottom Right</option>
<option value="bottom-left">Bottom Left</option>
<option value="top-center">Top Center</option>
<option value="top-right">Top Right</option>
<option value="top-left">Top Left</option>
</select>
</div>
<div>
<label class="block text-gray-700 mb-2">Font Size</label>
<input type="number" id="font-size" value="12" min="6" max="72" class="w-full p-2 border border-gray-300 rounded-md">
</div>
<div>
<label class="block text-gray-700 mb-2">Start From Page</label>
<input type="number" id="start-page" value="1" min="1" class="w-full p-2 border border-gray-300 rounded-md">
</div>
<button id="page-numbers-btn" class="w-full bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-md transition-colors">
Add Page Numbers
</button>
</div>
</section>
<!-- Page Management Tool -->
<section class="bg-white rounded-lg shadow-md p-6 col-span-1 md:col-span-2">
<h2 class="text-2xl font-semibold mb-4">Manage Pages</h2>
<div class="space-y-6">
<div id="page-thumbnails" class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-5 gap-4">
<!-- Thumbnails will be added here dynamically -->
</div>
<div class="flex flex-wrap gap-2">
<button id="delete-page-btn" class="bg-red-500 hover:bg-red-600 text-white font-medium py-2 px-4 rounded-md transition-colors">
Delete Selected
</button>
<button id="extract-page-btn" class="bg-green-500 hover:bg-green-600 text-white font-medium py-2 px-4 rounded-md transition-colors">
Extract Selected
</button>
<button id="merge-btn" class="bg-purple-500 hover:bg-purple-600 text-white font-medium py-2 px-4 rounded-md transition-colors">
Merge with Another PDF
</button>
<button id="save-changes-btn" class="bg-blue-500 hover:bg-blue-600 text-white font-medium py-2 px-4 rounded-md transition-colors">
Save All Changes
</button>
</div>
</div>
</section>
</div>
</main>
<footer class="bg-gray-800 text-white py-6">
<div class="container mx-auto px-4 text-center">
<p>© 2025 Ultimate PDF Toolkit. All rights reserved.</p>
<p class="mt-2">Free, secure, and easy to use. No registration required.</p>
</div>
</footer>
<script>
// PDF handling variables
let pdfDoc = null;
let pdfBytes = null;
let pageNum = 1;
let pageRendering = false;
let pageNumPending = null;
let scale = 1.0;
let rotation = 0;
let canvas = document.getElementById('pdf-canvas');
let ctx = canvas.getContext('2d');
// Initialize the PDF.js worker
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://unpkg.com/pdfjs-dist@2.14.305/build/pdf.worker.min.js';
// File upload handling
document.getElementById('pdf-upload').addEventListener('change', function(e) {
const file = e.target.files[0];
if (file && file.type === 'application/pdf') {
document.getElementById('file-name').textContent = file.name;
document.getElementById('tools-container').style.display = 'grid';
const reader = new FileReader();
reader.onload = function(event) {
const typedArray = new Uint8Array(event.target.result);
pdfBytes = typedArray;
loadPdfDocument(typedArray);
};
reader.readAsArrayBuffer(file);
}
});
// Load the PDF document
function loadPdfDocument(typedArray) {
pdfjsLib.getDocument({ data: typedArray }).promise.then(function(pdf) {
pdfDoc = pdf;
document.getElementById('page-info').textContent = `Page ${pageNum} of ${pdfDoc.numPages}`;
document.getElementById('range-to').value = pdfDoc.numPages;
renderPage(pageNum);
createThumbnails();
});
}
// Render a specific page
function renderPage(num) {
pageRendering = true;
pdfDoc.getPage(num).then(function(page) {
const viewport = page.getViewport({ scale: scale, rotation: rotation });
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: ctx,
viewport: viewport
};
const renderTask = page.render(renderContext);
renderTask.promise.then(function() {
pageRendering = false;
if (pageNumPending !== null) {
renderPage(pageNumPending);
pageNumPending = null;
}
});
});
document.getElementById('page-info').textContent = `Page ${num} of ${pdfDoc.numPages}`;
}
// Create thumbnails for all pages
function createThumbnails() {
const thumbnailContainer = document.getElementById('page-thumbnails');
thumbnailContainer.innerHTML = '';
for (let i = 1; i <= pdfDoc.numPages; i++) {
const thumbnailDiv = document.createElement('div');
thumbnailDiv.className = 'border border-gray-300 rounded-lg p-2 cursor-pointer hover:bg-gray-100 relative';
thumbnailDiv.dataset.page = i;
const thumbnailCanvas = document.createElement('canvas');
thumbnailDiv.appendChild(thumbnailCanvas);
const pageNumLabel = document.createElement('div');
pageNumLabel.className = 'absolute bottom-0 right-0 bg-blue-500 text-white text-xs px-2 py-1 rounded-tl-md';
pageNumLabel.textContent = i;
thumbnailDiv.appendChild(pageNumLabel);
thumbnailDiv.addEventListener('click', function() {
pageNum = parseInt(this.dataset.page);
renderPage(pageNum);
// Toggle selection
this.classList.toggle('ring-2');
this.classList.toggle('ring-blue-500');
});
thumbnailContainer.appendChild(thumbnailDiv);
// Render thumbnail
pdfDoc.getPage(i).then(function(page) {
const viewport = page.getViewport({ scale: 0.2 });
thumbnailCanvas.height = viewport.height;
thumbnailCanvas.width = viewport.width;
const renderContext = {
canvasContext: thumbnailCanvas.getContext('2d'),
viewport: viewport
};
page.render(renderContext);
});
}
}
// Navigation buttons
document.getElementById('prev-page').addEventListener('click', function() {
if (pageNum <= 1) return;
pageNum--;
renderPage(pageNum);
});
document.getElementById('next-page').addEventListener('click', function() {
if (pageNum >= pdfDoc.numPages) return;
pageNum++;
renderPage(pageNum);
});
document.getElementById('zoom-in').addEventListener('click', function() {
scale += 0.25;
renderPage(pageNum);
});
document.getElementById('zoom-out').addEventListener('click', function() {
if (scale <= 0.25) return;
scale -= 0.25;
renderPage(pageNum);
});
document.getElementById('rotate').addEventListener('click', function() {
rotation = (rotation + 90) % 360;
renderPage(pageNum);
});
// Resize preset change
document.getElementById('resize-preset').addEventListener('change', function() {
const preset = this.value;
const customSizeDiv = document.getElementById('custom-size');
if (preset === 'custom') {
customSizeDiv.style.display = 'flex';
} else {
customSizeDiv.style.display = 'none';
// Set dimensions based on preset
if (preset === 'a4') {
document.getElementById('width').value = 595; // A4 width in points
document.getElementById('height').value = 842; // A4 height in points
} else if (preset === 'letter') {
document.getElementById('width').value = 612; // Letter width in points
document.getElementById('height').value = 792; // Letter height in points
} else if (preset === 'legal') {
document.getElementById('width').value = 612; // Legal width in points
document.getElementById('height').value = 1008; // Legal height in points
}
}
});
// Crop apply selection change
document.getElementById('crop-apply').addEventListener('change', function() {
const pageRange = document.getElementById('page-range');
if (this.value === 'range') {
pageRange.style.display = 'flex';
} else {
pageRange.style.display = 'none';
}
});
// Resize and compress button
document.getElementById('resize-compress-btn').addEventListener('click', async function() {
const width = parseInt(document.getElementById('width').value);
const height = parseInt(document.getElementById('height').value);
const compressionLevel = parseInt(document.getElementById('compression').value) / 100;
try {
const { PDFDocument } = PDFLib;
const pdfDoc = await PDFDocument.load(pdfBytes);
// Resize all pages
const pages = pdfDoc.getPages();
for (let i = 0; i < pages.length; i++) {
const page = pages[i];
page.setSize(width, height);
}
// Save with compression level
const resizedPdfBytes = await pdfDoc.save({
useObjectStreams: false,
addDefaultPage: false,
compress: compressionLevel > 0
});
// Download the modified PDF
download(resizedPdfBytes, "resized_compressed.pdf", "application/pdf");
} catch (error) {
console.error('Error resizing PDF:', error);
alert('Error resizing PDF. Please try again.');
}
});
// Crop button
document.getElementById('crop-btn').addEventListener('click', async function() {
const left = parseInt(document.getElementById('crop-left').value);
const top = parseInt(document.getElementById('crop-top').value);
const right = parseInt(document.getElementById('crop-right').value);
const bottom = parseInt(document.getElementById('crop-bottom').value);
const applyTo = document.getElementById('crop-apply').value;
try {
const { PDFDocument } = PDFLib;
const pdfDoc = await PDFDocument.load(pdfBytes);
const pages = pdfDoc.getPages();
// Determine which pages to crop
let pagesToCrop = [];
if (applyTo === 'all') {
pagesToCrop = Array.from({ length: pages.length }, (_, i) => i);
} else if (applyTo === 'current') {
pagesToCrop = [pageNum - 1];
} else if (applyTo === 'range') {
const fromPage = parseInt(document.getElementById('range-from').value) - 1;
const toPage = parseInt(document.getElementById('range-to').value) - 1;
pagesToCrop = Array.from({ length: toPage - fromPage + 1 }, (_, i) => i + fromPage);
}
// Apply cropping
for (const pageIndex of pagesToCrop) {
if (pageIndex < 0 || pageIndex >= pages.length) continue;
const page = pages[pageIndex];
const { width, height } = page.getSize();
page.setCropBox(left, bottom, width - left - right, height - top - bottom);
}
// Save the modified PDF
const croppedPdfBytes = await pdfDoc.save();
download(croppedPdfBytes, "cropped.pdf", "application/pdf");
} catch (error) {
console.error('Error cropping PDF:', error);
alert('Error cropping PDF. Please try again.');
}
});
// Page numbers button
document.getElementById('page-numbers-btn').addEventListener('click', async function() {
const numberingStyle = document.getElementById('numbering-style').value;
const position = document.getElementById('position').value;
const fontSize = parseInt(document.getElementById('font-size').value);
const startPage = parseInt(document.getElementById('start-page').value) - 1;
try {
const { PDFDocument, rgb, StandardFonts } = PDFLib;
const pdfDoc = await PDFDocument.load(pdfBytes);
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const pages = pdfDoc.getPages();
// Add page numbers to each page
for (let i = startPage; i < pages.length; i++) {
const page = pages[i];
const { width, height } = page.getSize();
let pageNumber = i - startPage + 1;
// Format page number based on style
let formattedNumber = String(pageNumber);
if (numberingStyle === 'roman') {
formattedNumber = toRoman(pageNumber);
} else if (numberingStyle === 'alphabetic') {
formattedNumber = toAlphabetic(pageNumber);
}
// Determine position coordinates
let x = 0, y = 0;
const textWidth = helveticaFont.widthOfTextAtSize(formattedNumber, fontSize);
if (position.includes('bottom')) {
y = 20;
} else if (position.includes('top')) {
y = height - 20;
}
if (position.includes('center')) {
x = (width - textWidth) / 2;
} else if (position.includes('right')) {
x = width - textWidth - 20;
} else if (position.includes('left')) {
x = 20;
}
page.drawText(formattedNumber, {
x: x,
y: y,
size: fontSize,
font: helveticaFont,
color: rgb(0, 0, 0),
});
}
// Save the modified PDF
const numberedPdfBytes = await pdfDoc.save();
download(numberedPdfBytes, "numbered.pdf", "application/pdf");
} catch (error) {
console.error('Error adding page numbers:', error);
alert('Error adding page numbers. Please try again.');
}
});
// Helper function to convert numbers to Roman numerals
function toRoman(num) {
const romanNumerals = [
{ value: 1000, numeral: 'M' },
{ value: 900, numeral: 'CM' },
{ value: 500, numeral: 'D' },
{ value: 400, numeral: 'CD' },
{ value: 100, numeral: 'C' },
{ value: 90, numeral: 'XC' },
{ value: 50, numeral: 'L' },
{ value: 40, numeral: 'XL' },
{ value: 10, numeral: 'X' },
{ value: 9, numeral: 'IX' },
{ value: 5, numeral: 'V' },
{ value: 4, numeral: 'IV' },
{ value: 1, numeral: 'I' }
];
let result = '';
for (const { value, numeral } of romanNumerals) {
while (num >= value) {
result += numeral;
num -= value;
}
}
return result.toLowerCase();
}
// Helper function to convert numbers to alphabetic (A, B, C, ...)
function toAlphabetic(num) {
let result = '';
while (num > 0) {
const remainder = (num - 1) % 26;
result = String.fromCharCode(65 + remainder) + result;
num = Math.floor((num - 1) / 26);
}
return result;
}
// Delete selected pages
document.getElementById('delete-page-btn').addEventListener('click', async function() {
const selectedThumbnails = document.querySelectorAll('#page-thumbnails div.ring-blue-500');
if (selectedThumbnails.length === 0) {
alert('Please select at least one page to delete.');
return;
}
try {
const { PDFDocument } = PDFLib;
const pdfDoc = await PDFDocument.load(pdfBytes);
// Get pages to delete (in reverse order to maintain indices)
const pagesToDelete = Array.from(selectedThumbnails)
.map(thumb => parseInt(thumb.dataset.page) - 1)
.sort((a, b) => b - a); // Sort in descending order
// Remove pages
for (const pageIndex of pagesToDelete) {
pdfDoc.removePage(pageIndex);
}
// Save the modified PDF
const modifiedPdfBytes = await pdfDoc.save();
pdfBytes = modifiedPdfBytes;
// Reload the PDF viewer
loadPdfDocument(modifiedPdfBytes);
} catch (error) {
console.error('Error deleting pages:', error);
alert('Error deleting pages. Please try again.');
}
});
// Extract selected pages
document.getElementById('extract-page-btn').addEventListener('click', async function() {
const selectedThumbnails = document.querySelectorAll('#page-thumbnails div.ring-blue-500');
if (selectedThumbnails.length === 0) {
alert('Please select at least one page to extract.');
return;
}
try {
const { PDFDocument } = PDFLib;
const srcPdfDoc = await PDFDocument.load(pdfBytes);
const newPdfDoc = await PDFDocument.create();
// Get pages to extract
const pagesToExtract = Array.from(selectedThumbnails)
.map(thumb => parseInt(thumb.dataset.page) - 1)
.sort((a, b) => a - b); // Sort in ascending order
// Copy pages to new document
for (const pageIndex of pagesToExtract) {
const [copiedPage] = await newPdfDoc.copyPages(srcPdfDoc, [pageIndex]);
newPdfDoc.addPage(copiedPage);
}
// Save the new PDF
const extractedPdfBytes = await newPdfDoc.save();
download(extractedPdfBytes, "extracted_pages.pdf", "application/pdf");
} catch (error) {
console.error('Error extracting pages:', error);
alert('Error extracting pages. Please try again.');
}
});
// Merge with another PDF
document.getElementById('merge-btn').addEventListener('click', function() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.pdf';
input.onchange = async function(e) {
const file = e.target.files[0];
if (file && file.type === 'application/pdf') {
try {
const reader = new FileReader();
reader.onload = async function(event) {
const secondPdfBytes = new Uint8Array(event.target.result);
const { PDFDocument } = PDFLib;
const firstPdfDoc = await PDFDocument.load(pdfBytes);
const secondPdfDoc = await PDFDocument.load(secondPdfBytes);
// Copy pages from second PDF to first PDF
const copiedPages = await firstPdfDoc.copyPages(
secondPdfDoc,
secondPdfDoc.getPageIndices()
);
// Add copied pages to first PDF
for (const page of copiedPages) {
firstPdfDoc.addPage(page);
}
// Save the merged PDF
const mergedPdfBytes = await firstPdfDoc.save();
pdfBytes = mergedPdfBytes;
// Reload the PDF viewer
loadPdfDocument(mergedPdfBytes);
};
reader.readAsArrayBuffer(file);
} catch (error) {
console.error('Error merging PDFs:', error);
alert('Error merging PDFs. Please try again.');
}
}
};
input.click();
});
// Save all changes
document.getElementById('save-changes-btn').addEventListener('click', function() {
if (pdfBytes) {
download(pdfBytes, "modified.pdf", "application/pdf");
}
});
</script>
</body>
</html>