یک کد برات ارسال میکنم مشکل دانلود پی دی اف را در این کد حل کن الان با زدن دکمه دانلود پی دی اف دانلود انجام نمیشه صبر کن کد رو برات ارسال کنم
Assistant
بله، کد را ارسال کنید تا بتوانم به شما کمک کنم. منتظر هستم!
User
Assistant
code
<label for="product-price">قیمت واحد (تومان):</label>
<input type="number" id="product-price" required>
<label for="product-quantity">تعداد:</label>
<input type="number" id="product-quantity" required>
<button id="add-product">اضافه کردن به فاکتور</button>
</div>
<div id="invoice">
<table>
<thead>
<tr>
<th>شرح کالا</th>
<th>قیمت واحد (تومان)</th>
<th>تعداد</th>
<th>مجموع (تومان)</th>
</tr>
</thead>
<tbody id="invoice-body">
<!-- محصولات اضافه شده در اینجا نمایش داده میشوند -->
</tbody>
<tfoot>
<tr>
<td colspan="3" style="text-align: left;"><strong>جمع کل</strong></td>
<td id="total"><strong>۰</strong></td>
</tr>
</tfoot>
</table>
<button id="download">دانلود فاکتور به صورت PDF</button>
</div>
<script>
let total = 0;
document.getElementById('add-product').addEventListener('click', () => {
const name = document.getElementById('product-name').value;
const price = parseInt(document.getElementById('product-price').value);
const quantity = parseInt(document.getElementById('product-quantity').value);
const sum = price * quantity;
if (name && !isNaN(price) && !isNaN(quantity)) {
// اضافه کردن محصول به جدول فاکتور
const tableBody = document.getElementById('invoice-body');
const newRow = tableBody.insertRow();
newRow.innerHTML = `
<td>${name}</td>
<td>${price.toLocaleString()}</td>
<td>${quantity}</td>
<td>${sum.toLocaleString()}</td>
`;
// به روزرسانی جمع کل
total += sum;
document.getElementById('total').innerHTML = total.toLocaleString();
// پاک کردن ورودیها
document.getElementById('product-name').value = '';
document.getElementById('product-price').value = '';
document.getElementById('product-quantity').value = '';
} else {
alert('لطفا تمام فیلدها را پر کنید');
}
});
document.getElementById('download').addEventListener('click', () => {
const { jsPDF } = window.jspdf;
html2canvas(document.querySelector("#invoice"), { scale: 2 }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'pt', 'a4');
const imgWidth = 190; // Width in mm
const pageHeight = pdf.internal.pageSize.height;
const imgHeight = (canvas.height * imgWidth) / canvas.width;
const heightLeft = imgHeight;
let position = 0;
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
const heightLeftAfter = heightLeft - pageHeight;
// Check if we need to add a new page
if (heightLeftAfter >= 0) {
position = heightLeft - heightLeftAfter;
pdf.addPage();
pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight);
}
pdf.save('faktor.pdf'); // Save the PDF
});
});
</script>
کدی که ارسال کردید بهدرستی برای دانلود PDF طراحی شده است، اما با توجه به نسخههای جدید html2canvas و jsPDF ممکن است مشکلاتی وجود داشته باشد. بهخصوص، نسخههای جدید html2canvas از Promise بهطور کامل استفاده میکنند و این ممکن است منجر به تغییراتی در نحوهی عملکرد آنها شود.
نکات برای بهبود کد:
استفاده از نسخههای جدید: اطمینان حاصل کنید که از آخرین نسخهی هر یک از کتابخانهها استفاده میکنید.
اطمینان از بارگزاری کامل صفحه: ممکن است لازم باشد که کد دانلود PDF را تنها بعد از بارگزاری کامل صفحه اجرایی کنید.
اصلاح کد شما:
در زیر نسخهی اصلاحشدهی کدی را که ارائه دادید میبینید. بهطور خاص، از نسخههای جدیدتر html2canvas و jsPDF استفاده میشود و به نوعی به سادگی کارکرد آنها را بهبود میبخشیم:
html
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="language" content="fa">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>نرم افزار صدور فاکتور</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<style>
body {
font-family: Tahoma, sans-serif;
direction: rtl;
text-align: right;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: right;
}
th {
background-color: #f2f2f2;
}
#invoice {
padding: 20px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<h2>فاکتور فروش</h2>
<div>
<label for="product-name">شرح کالا:</label>
<input type="text" id="product-name" required>
<label for="product-price">قیمت واحد (تومان):</label>
<input type="number" id="product-price" required>
<label for="product-quantity">تعداد:</label>
<input type="number" id="product-quantity" required>
<button id="add-product">اضافه کردن به فاکتور</button>
</div>
<div id="invoice">
<table>
<thead>
<tr>
<th>شرح کالا</th>
<th>قیمت واحد (تومان)</th>
<th>تعداد</th>
<th>مجموع (تومان)</th>
</tr>
</thead>
<tbody id="invoice-body">
<!-- محصولات اضافه شده در اینجا نمایش داده میشوند -->
</tbody>
<tfoot>
<tr>
<td colspan="3" style="text-align: left;"><strong>جمع کل</strong></td>
<td id="total"><strong>۰</strong></td>
</tr>
</tfoot>
</table>
<button id="download">دانلود فاکتور به صورت PDF</button>
</div>
<script>
let total = 0;
document.getElementById('add-product').addEventListener('click', () => {
const name = document.getElementById('product-name').value;
const price = parseInt(document.getElementById('product-price').value);
const quantity = parseInt(document.getElementById('product-quantity').value);
const sum = price * quantity;
if (name && !isNaN(price) && !isNaN(quantity)) {
// اضافه کردن محصول به جدول فاکتور
const tableBody = document.getElementById('invoice-body');
const newRow = tableBody.insertRow();
newRow.innerHTML = `
<td>${name}</td>
<td>${price.toLocaleString()}</td>
<td>${quantity}</td>
<td>${sum.toLocaleString()}</td>
`;
// به روزرسانی جمع کل
total += sum;
document.getElementById('total').innerHTML = total.toLocaleString();
// پاک کردن ورودیها
document.getElementById('product-name').value = '';
document.getElementById('product-price').value = '';
document.getElementById('product-quantity').value = '';
} else {
alert('لطفا تمام فیلدها را پر کنید');
}
});
document.getElementById('download').addEventListener('click', () => {
const { jsPDF } = window.jspdf;
html2canvas(document.querySelector("#invoice")).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'mm', 'a4');
const imgWidth = 190; // Width in mm
const imgHeight = (canvas.height * imgWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight); // Add image to PDF
pdf.save('faktor.pdf'); // Save the PDF
});
});
</script>
</body>
</html>
نکات مهم:
بررسی کنسول مرورگر: هرگونه خطا را در کنسول مرورگر (F12) بررسی کنید.
اجرا در مرورگرهای مختلف: هنگام تست، از مرورگرهای مختلف (Chrome، Firefox، Edge) استفاده کنید.
تنظیمات شبکه: اگر از منابع CDN استفاده میکنید، اطمینان حاصل کنید که اتصال اینترنت شما بدون مشکل باشد و منابع بارگذاری شوند.
اگر باز هم با مشکلی مواجه شدید، پیام بگذارید تا بیشتر به شما کمک کنم.