fix(tahesab): open invoice PDF window synchronously on click

Awaiting settings before window.open left a blank tab; open about:blank on click then fill via blob URL.
This commit is contained in:
2026-10-24 14:09:46 +03:30
parent f50246dc46
commit 1ba352f127
2 changed files with 66 additions and 13 deletions
@@ -411,15 +411,35 @@ async function exportAsnadPdf(g: {
rows: Record<string, unknown>[];
}) {
error.value = '';
// باز کردن هم‌زمان با کلیک — وگرنه بعد از await پاپ‌آپ بلاک/خالی می‌شود
const preview = window.open('about:blank', '_blank');
if (preview) {
try {
preview.document.write(
'<!DOCTYPE html><html lang="fa" dir="rtl"><head><meta charset="utf-8" /><title>فاکتور</title></head><body style="font-family:Tahoma,sans-serif;padding:24px">در حال آماده‌سازی فاکتور…</body></html>',
);
preview.document.close();
} catch {
/* ignore */
}
}
if (!invoiceSettings.value) {
await loadInvoiceSettings();
}
printTahesabInvoicePdf({
const okPrint = printTahesabInvoicePdf({
group: g,
settings: invoiceSettings.value,
customerName: props.personName?.trim() || 'مشتری',
moshtariCode: currentLink.value?.moshtariCode ?? null,
targetWindow: preview,
});
if (!okPrint) {
error.value =
'خروجی فاکتور باز نشد. پاپ‌آپ مرورگر را برای این سایت مجاز کنید و دوباره بزنید.';
preview?.close();
}
}
async function loadAsnad() {
+42 -9
View File
@@ -62,8 +62,10 @@ export function printTahesabInvoicePdf(opts: {
settings: TahesabInvoiceSettingsDto | null;
customerName: string;
moshtariCode?: number | null;
}) {
const { group, settings, customerName, moshtariCode } = opts;
/** پنجره‌ای که هم‌زمان با کلیک کاربر باز شده (برای دور زدن بلاک پاپ‌آپ) */
targetWindow?: Window | null;
}): boolean {
const { group, settings, customerName, moshtariCode, targetWindow } = opts;
const first = group.rows[0] ?? {};
const time = dash(first.Tarikh_Time ?? first.ZamanSabt, '');
const user = dash(first.User, '');
@@ -106,9 +108,6 @@ export function printTahesabInvoicePdf(opts: {
? `<p class="note foot-note">${escapeHtml(settings.footerText.trim())}</p>`
: '';
const win = window.open('', '_blank', 'noopener,noreferrer,width=900,height=1200');
if (!win) return;
const html = `<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
@@ -244,11 +243,45 @@ export function printTahesabInvoicePdf(opts: {
</body>
</html>`;
const blob = new Blob([html], { type: 'text/html;charset=utf-8' });
const url = URL.createObjectURL(blob);
let win: Window | null =
targetWindow && !targetWindow.closed ? targetWindow : null;
if (win) {
try {
win.location.href = url;
} catch {
try {
win.document.open();
win.document.write(html);
win.document.close();
win.focus();
setTimeout(() => {
win.print();
}, 300);
} catch {
URL.revokeObjectURL(url);
return false;
}
}
} else {
// بدون noopener تا reference داشته باشیم
win = window.open(url, '_blank', 'width=960,height=1200');
if (!win) {
URL.revokeObjectURL(url);
return false;
}
}
const triggerPrint = () => {
try {
win?.focus();
win?.print();
} catch {
/* ignore */
} finally {
setTimeout(() => URL.revokeObjectURL(url), 60_000);
}
};
setTimeout(triggerPrint, 600);
return true;
}