Convert JPG to PDF
Combine your images into a single, professional PDF document. Drag and drop to reorder pages.
Drop Images Here
JPG, PNG supported. Combine multiple files.
PDF Settings
Fit to Image (Variable)
A4 (Standard)
Letter (US)
Portrait
Landscape
Auto (Detect)
No Margin
Small (20px)
Large (50px)
Files (0)
Drag thumbnails to reorder pages
${name}
#
`;
imageGrid.appendChild(div);
updateBadges();
}function removeImage(id) {
const el = document.getElementById(id);
if(el) el.remove();
images = images.filter(img => img.id !== id);
updateCount();
updateBadges();
if(images.length === 0) {
resetApp();
}
}function updateCount() {
fileCountEl.innerText = images.length;
}function updateBadges() {
const items = document.querySelectorAll(‘.image-item’);
items.forEach((item, index) => {
item.querySelector(‘.page-badge’).innerText = index + 1;
});
}async function generatePDF() {
if(images.length === 0) return;btnConvert.disabled = true;
btnText.innerText = ‘Building PDF…’;
btnLoader.classList.remove(‘hidden’);try {
const { PDFDocument, PageSizes } = PDFLib;
const pdfDoc = await PDFDocument.create();// Get Order from DOM
const orderedIds = Array.from(imageGrid.children).map(el => el.id);for (const id of orderedIds) {
const imgData = images.find(img => img.id === id);
if(!imgData) continue;// Embed Image
let image;
const imgBytes = await fetch(imgData.dataUrl).then(res => res.arrayBuffer());
// Helper to check format signature
// Simple fallback: try png, catch, try jpg
try {
image = await pdfDoc.embedJpg(imgBytes);
} catch (e) {
try {
image = await pdfDoc.embedPng(imgBytes);
} catch (e2) {
console.warn(“Could not embed image”, imgData.file.name);
continue;
}
}// Determine Page Size
const sizeSetting = pdfSize.value; // fit, a4, letter
const orientation = pdfOrientation.value; // portrait, landscape, auto
const margin = parseInt(pdfMargin.value);let pageWidth, pageHeight;
// Dimensions logic
if (sizeSetting === ‘fit’) {
// Use image dimensions + margin
pageWidth = image.width + (margin * 2);
pageHeight = image.height + (margin * 2);
} else {
// Standard sizes (points)
const standard = sizeSetting === ‘a4’ ? PageSizes.A4 : PageSizes.Letter;
// Handle Orientation
let isLandscape = false;
if (orientation === ‘landscape’) isLandscape = true;
else if (orientation === ‘auto’) isLandscape = image.width > image.height;
pageWidth = isLandscape ? standard[1] : standard[0];
pageHeight = isLandscape ? standard[0] : standard[1];
}const page = pdfDoc.addPage([pageWidth, pageHeight]);
// Calculate scaled image dimensions to fit page minus margins
const availW = pageWidth – (margin * 2);
const availH = pageHeight – (margin * 2);
let imgW = image.width;
let imgH = image.height;
// Scale down if too big
const scaleW = availW / imgW;
const scaleH = availH / imgH;
const scale = Math.min(scaleW, scaleH, 1); // Don’t scale up if smaller and ‘fit’ mode isn’t used, but logic above handles fit differently.
// For standard pages, we usually scale to fit.
const drawW = imgW * scale;
const drawH = imgH * scale;// Center
const x = (pageWidth – drawW) / 2;
const y = (pageHeight – drawH) / 2;page.drawImage(image, {
x: x,
y: y,
width: drawW,
height: drawH,
});
}const pdfBytes = await pdfDoc.save();
const blob = new Blob([pdfBytes], { type: ‘application/pdf’ });
const url = URL.createObjectURL(blob);// Show Success
editorStage.classList.add(‘hidden’);
successStage.classList.remove(‘hidden’);
downloadBtn.href = url;
downloadBtn.download = ‘images_combined.pdf’;} catch (err) {
console.error(err);
alert(‘Error creating PDF.’);
} finally {
btnConvert.disabled = false;
btnText.innerText = ‘Create PDF’;
btnLoader.classList.add(‘hidden’);
}
}function resetApp() {
fileInput.value = ”;
images = [];
imageGrid.innerHTML = ”;
dropzone.classList.remove(‘hidden’);
editorStage.classList.add(‘hidden’);
successStage.classList.add(‘hidden’);
}function toggleDarkMode() {
isDarkMode = !isDarkMode;
document.documentElement.classList.toggle(‘dark’);
const icon = document.getElementById(‘theme-icon’);
if(isDarkMode) {
icon.classList.remove(‘fa-moon’);
icon.classList.add(‘fa-sun’);
} else {
icon.classList.remove(‘fa-sun’);
icon.classList.add(‘fa-moon’);
}
}// — JAVASCRIPT SEPARATION END —Using JPG To PDF
The JPG To PDF utility is designed for PDF document tasks. Check the on-page controls for available formats, limits and options. Always review the output for accuracy before sharing or publishing it.
Practical tips
- Keep your original files or source information as a backup.
- Choose suitable options using the controls provided by this tool.
- Check the generated result and retry with different settings if needed.
Explore related tools
Compress PDF and Merge PDF are available for related tasks.