`;
dom.output.innerHTML += captionCard;
});
dom.output.classList.remove('hidden');
dom.downloadBtn.classList.remove('hidden');
lucide.createIcons();
dom.output.querySelectorAll('.copy-caption-btn').forEach(btn => {
btn.addEventListener('click', (e) => copyText(captions[e.currentTarget.dataset.index], e.currentTarget));
});
}
function copyText(text, button) {
navigator.clipboard.writeText(text).then(() => {
const originalHTML = button.innerHTML;
button.innerHTML = 'Copied!';
button.disabled = true;
setTimeout(() => { button.innerHTML = originalHTML; button.disabled = false; lucide.createIcons(); }, 1500);
}).catch(err => console.error('Failed to copy text: ', err));
}
function downloadPDF() {
const { jsPDF } = window.jspdf;
const captions = Array.from(dom.output.querySelectorAll('.caption-text')).map(el => el.textContent);
if (!imageBase64 || captions.length === 0) return;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const margin = 40;
const pdfWidth = pdf.internal.pageSize.getWidth();
const contentWidth = pdfWidth - (margin * 2);
let yPos = margin;
pdf.setFont('Inter', 'bold');
pdf.setFontSize(18);
pdf.text('Generated Image Captions', pdfWidth / 2, yPos, { align: 'center' });
yPos += 30;
const img = new Image();
img.onload = function() {
const imgRatio = this.height / this.width;
const imgHeight = contentWidth * imgRatio;
pdf.addImage(this, 'PNG', margin, yPos, contentWidth, imgHeight);
yPos += imgHeight + 20;
captions.forEach((caption, index) => {
pdf.setFont('Inter', 'bold');
pdf.text(`Option ${index + 1}:`, margin, yPos);
yPos += 15;
pdf.setFont('Inter', 'normal');
const lines = pdf.splitTextToSize(caption, contentWidth);
pdf.text(lines, margin, yPos);
yPos += (lines.length * 12) + 20;
});
pdf.save('Image_Captions.pdf');
};
img.src = `data:${imageMimeType};base64,${imageBase64}`;
}
init();
});
