java.txt
window.jsPDF = window.jspdf.jsPDF;
document.getElementById('convert-btn').addEventListener('click', convertToPDF);
async function convertToPDF() { const images = document.getElementById('image-upload').files; if (images.length === 0) { alert('Please upload at least one image.'); return; }
const pdf = new jsPDF(); const pageSpread = document.getElementById('page-spread').value; const borderSize = parseInt(document.getElementById('border-size').value); const pageSize = document.getElementById('page-size').value;
for (let i = 0; i < images.length; i++) { const image = images[i]; const img = await loadImage(image);
// Define PDF page dimensions based on selected page size
let pdfWidth, pdfHeight;
if (pageSize === 'A4') {
pdfWidth = 210; // A4 width in mm
pdfHeight = 297; // A4 height in mm
} else if (pageSize === 'Letter') {
pdfWidth = 216; // Letter width in mm
pdfHeight = 279; // Letter height in mm
} else if (pageSize === 'A5') {
pdfWidth = 148; // A5 width in mm
pdfHeight = 210; // A5 height in mm
}
// Calculate scaling for the image
const scale = Math.min(pdfWidth / img.width, pdfHeight / img.height);
const scaledWidth = img.width * scale;
const scaledHeight = img.height * scale;
// Add a new page for every image except the first
if (i > 0) pdf.addPage();
// Handle double-page spread
if (pageSpread === 'double' && i % 2 === 0 && i + 1 < images.length) {
const nextImage = images[i + 1];
const nextImg = await loadImage(nextImage);
// Calculate scaling for the next image
const nextScale = Math.min(pdfWidth / nextImg.width, pdfHeight / nextImg.height);
const nextScaledWidth = nextImg.width * nextScale;
const nextScaledHeight = nextImg.height * nextScale;
// Add the first image to the left half of the page
pdf.addImage(
img,
'JPEG',
borderSize,
borderSize,
scaledWidth / 2 - borderSize,
scaledHeight - 2 * borderSize
);
// Add the second image to the right half of the page
pdf.addImage(
nextImg,
'JPEG',
scaledWidth / 2 + borderSize,
borderSize,
nextScaledWidth / 2 - borderSize,
nextScaledHeight - 2 * borderSize
);
i++; // Skip the next image since it's already processed
} else {
// Single image per page
pdf.addImage(
img,
'JPEG',
borderSize,
borderSize,
scaledWidth - 2 * borderSize,
scaledHeight - 2 * borderSize
);
}
}
// Provide download link for the PDF const downloadLink = document.getElementById('download-link'); downloadLink.href = pdf.output('bloburl'); downloadLink.download = 'converted.pdf'; downloadLink.style.display = 'block'; }
function loadImage(file) { return new Promise((resolve, reject) => { const img = new Image(); const reader = new FileReader(); reader.onload = (e) => { img.src = e.target.result; img.onload = () => resolve(img); }; reader.onerror = (e) => reject(e); reader.readAsDataURL(file); }); }