function printToPDF() { const container = document.createElement('div'); container.className = 'temp-print-container'; container.style.padding = '20px'; container.style.backgroundColor = '#fff'; container.style.direction = 'rtl'; container.style.fontFamily = "'Vazirmatn', 'Arial', sans-serif"; const title = document.createElement('h2'); title.textContent = document.getElementById('dynamic-title').textContent; title.style.textAlign = 'center'; container.appendChild(title); const fields = [ { label: 'نام کالا', id: 'product-name' }, { label: 'قیمت کالا در چین', id: 'china-product-price' }, { label: 'ارزش فوب (ریال)', id: 'product-price-rial' }, { label: 'هزینه حمل (ریال)', id: 'shipping-cost-rial' }, { label: 'هزینه گمرک (ریال)', id: 'customs-cost-rial' }, { label: 'هزینه مجوزات (ریال)', id: 'permits-cost-rial' }, { label: 'سایر هزینه‌ها (ریال)', id: 'other-costs-rial' }, { label: 'قیمت تمام‌شده (ریال)', id: 'total-cost-rial' } ]; fields.forEach(field => { const div = document.createElement('div'); div.style.marginBottom = '10px'; const label = document.createElement('strong'); label.textContent = field.label + ': '; const value = document.createElement('span'); value.textContent = document.getElementById(field.id).value || '0'; div.appendChild(label); div.appendChild(value); container.appendChild(div); }); const chartCanvas = document.getElementById('cost-chart'); html2canvas(chartCanvas).then(canvas => { const imgData = canvas.toDataURL('image/png'); const img = document.createElement('img'); img.src = imgData; img.style.width = '100%'; container.appendChild(img); document.body.appendChild(container); html2canvas(container).then(canvas => { const { jsPDF } = window.jspdf; const pdf = new jsPDF('p', 'mm', 'a4'); const imgData = canvas.toDataURL('image/png'); const imgProps = pdf.getImageProperties(imgData); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width; pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight); pdf.save('import-cost-report.pdf'); document.body.removeChild(container); }); }); }