`;
toneResultsContainer.innerHTML += card;
});
} else {
showError("Could not identify specific tones in the provided text.");
}
};
const showError = (message) => {
resultsSection.style.display = 'none';
errorSection.textContent = message;
errorSection.style.display = 'block';
};
// --- 5. PDF Generation ---
pdfDownloadButton.addEventListener('click', () => {
if (!lastAnalysis.analysis || !lastAnalysis.analysis.tones) return;
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
doc.setFontSize(20);
doc.text("Copywriting Tone Analysis Report", 105, 20, { align: 'center' });
doc.setFontSize(14);
doc.text("Analyzed Text", 14, 40);
doc.setFontSize(10);
const textLines = doc.splitTextToSize(lastAnalysis.text, 180);
doc.text(textLines, 14, 47);
const startY = 47 + textLines.length * 4 + 10;
const tableBody = lastAnalysis.analysis.tones.map(tone => [
tone.type,
tone.name,
`${tone.score}%`,
tone.explanation
]);
doc.autoTable({
head: [['Type', 'Tone Name', 'Confidence', 'Explanation']],
body: tableBody,
startY: startY,
theme: 'grid',
columnStyles: {
3: { cellWidth: 80 }
}
});
doc.save('Copywriting_Tone_Analysis.pdf');
});
});