Please fill in all fields in the previous steps to generate your USP.
';
showTab(tabs.length - 1);
return;
}
const templates = [
`For
${targetAudience} who struggle with
${problemSolved},
${productName} is the only product that provides
${keyBenefit} because of our
${differentiator}.`,
`
${productName} helps
${targetAudience} to achieve
${keyBenefit}, so they can overcome
${problemSolved}. Unlike other solutions, we offer
${differentiator}.`,
`The
${productName} provides
${targetAudience} with
${keyBenefit} to solve
${problemSolved}, uniquely featuring
${differentiator}.`
];
let resultsHTML = '
';
templates.forEach((template, index) => {
resultsHTML += `- Option ${index + 1}: ${template}
`;
});
resultsHTML += '
';
resultsContainer.innerHTML = resultsHTML;
pdfButtonContainer.style.display = 'block';
showTab(tabs.length - 1);
};
const downloadPDF = () => {
try {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
// --- Get Data ---
const productName = document.getElementById('product-name')?.value || 'N/A';
const targetAudience = document.getElementById('target-audience')?.value || 'N/A';
const problemSolved = document.getElementById('problem-solved')?.value || 'N/A';
const keyBenefit = document.getElementById('key-benefit')?.value || 'N/A';
const differentiator = document.getElementById('differentiator')?.value || 'N/A';
// --- Helper Variables ---
const pageHeight = doc.internal.pageSize.getHeight();
const pageWidth = doc.internal.pageSize.getWidth();
const margin = 20;
let cursorY = margin;
// --- PDF Header ---
doc.setFont('helvetica', 'bold');
doc.setFontSize(22);
doc.setTextColor(40, 52, 71); // Dark Blue-Gray
doc.text('Unique Selling Proposition Report', pageWidth / 2, cursorY, { align: 'center' });
cursorY += 10;
doc.setFont('helvetica', 'normal');
doc.setFontSize(11);
doc.setTextColor(128, 128, 128); // Gray
doc.text('Generated by the USP Generator Tool', pageWidth / 2, cursorY, { align: 'center' });
cursorY += 15;
doc.setDrawColor(221, 221, 221); // Light Gray Line
doc.line(margin, cursorY, pageWidth - margin, cursorY);
cursorY += 15;
// --- Inputs Section (using AutoTable) ---
doc.setFontSize(16);
doc.setFont('helvetica', 'bold');
doc.setTextColor(40, 52, 71);
doc.text('Your Inputs', margin, cursorY);
cursorY += 5;
const tableBody = [
['Product/Service', productName],
['Target Audience', targetAudience],
['Problem Solved', problemSolved],
['Key Benefit', keyBenefit],
['Differentiator', differentiator],
];
doc.autoTable({
startY: cursorY,
head: [], // No header needed for this key-value format
body: tableBody,
theme: 'grid',
styles: {
fontSize: 10,
cellPadding: 3,
},
headStyles: {
fillColor: [59, 130, 246]
},
columnStyles: {
0: { fontStyle: 'bold', cellWidth: 50 },
1: { cellWidth: 'auto' }
},
didDrawPage: (data) => {
// This hook allows us to get the final Y position after the table
cursorY = data.cursor.y;
}
});
cursorY += 15; // Spacing after the table
// --- Generated USPs Section ---
doc.setFontSize(16);
doc.setFont('helvetica', 'bold');
doc.setTextColor(40, 52, 71);
doc.text('Generated USPs', margin, cursorY);
cursorY += 8;
const resultsContainer = document.getElementById('results-container');
const listItems = resultsContainer.querySelectorAll('li');
listItems.forEach((item, index) => {
if (cursorY > pageHeight - 30) { // Check for page break
doc.addPage();
cursorY = margin;
}
doc.setFont('helvetica', 'bold');
doc.setFontSize(11);
doc.setTextColor(59, 130, 246); // Blue
doc.text(`Option ${index + 1}:`, margin, cursorY);
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
doc.setTextColor(80, 80, 80);
// Convert HTML to plain text for PDF
const tempDiv = document.createElement('div');
// clone the content to avoid modifying the original
tempDiv.innerHTML = item.innerHTML;
// Remove the "Option X:" part as we've added it manually
if (tempDiv.querySelector('strong')) {
tempDiv.querySelector('strong').remove();
}
// Replace
with nothing for cleaner text
const textContent = tempDiv.innerHTML.replace(//g, '').replace(/<\/strong>/g, '');
const textLines = doc.splitTextToSize(textContent, pageWidth - margin * 2 - 5); // 5 is an indent
doc.text(textLines, margin + 5, cursorY + 5);
cursorY += (textLines.length * 4.5) + 10; // Adjust cursor based on text lines
});
// --- Footer on every page ---
const pageCount = doc.internal.getNumberOfPages();
for (let i = 1; i <= pageCount; i++) {
doc.setPage(i);
doc.setFontSize(9);
doc.setTextColor(150);
const footerText = `Page ${i} of ${pageCount} | USP Report | Generated on: ${new Date().toLocaleDateString()}`;
doc.text(footerText, pageWidth / 2, pageHeight - 10, { align: 'center' });
}
doc.save('USP-Report.pdf');
} catch (error) {
console.error("Failed to generate PDF:", error);
// Optionally show an error to the user in the UI
const pdfButtonContainer = document.getElementById('pdf-button-container');
if (pdfButtonContainer) {
const errorMsg = document.createElement('p');
errorMsg.textContent = 'Sorry, there was an error creating the PDF.';
errorMsg.className = 'text-red-500 text-sm mt-2';
pdfButtonContainer.appendChild(errorMsg);
}
}
};
// Event Listeners
prevButton.addEventListener('click', () => navigate(-1));
nextButton.addEventListener('click', () => navigate(1));
generateButton.addEventListener('click', generateUSP);
downloadPdfButton.addEventListener('click', downloadPDF);
// Initial setup
showTab(0);
});