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:
@@ -411,15 +411,35 @@ async function exportAsnadPdf(g: {
|
|||||||
rows: Record<string, unknown>[];
|
rows: Record<string, unknown>[];
|
||||||
}) {
|
}) {
|
||||||
error.value = '';
|
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) {
|
if (!invoiceSettings.value) {
|
||||||
await loadInvoiceSettings();
|
await loadInvoiceSettings();
|
||||||
}
|
}
|
||||||
printTahesabInvoicePdf({
|
|
||||||
|
const okPrint = printTahesabInvoicePdf({
|
||||||
group: g,
|
group: g,
|
||||||
settings: invoiceSettings.value,
|
settings: invoiceSettings.value,
|
||||||
customerName: props.personName?.trim() || 'مشتری',
|
customerName: props.personName?.trim() || 'مشتری',
|
||||||
moshtariCode: currentLink.value?.moshtariCode ?? null,
|
moshtariCode: currentLink.value?.moshtariCode ?? null,
|
||||||
|
targetWindow: preview,
|
||||||
});
|
});
|
||||||
|
if (!okPrint) {
|
||||||
|
error.value =
|
||||||
|
'خروجی فاکتور باز نشد. پاپآپ مرورگر را برای این سایت مجاز کنید و دوباره بزنید.';
|
||||||
|
preview?.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadAsnad() {
|
async function loadAsnad() {
|
||||||
|
|||||||
@@ -62,8 +62,10 @@ export function printTahesabInvoicePdf(opts: {
|
|||||||
settings: TahesabInvoiceSettingsDto | null;
|
settings: TahesabInvoiceSettingsDto | null;
|
||||||
customerName: string;
|
customerName: string;
|
||||||
moshtariCode?: number | null;
|
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 first = group.rows[0] ?? {};
|
||||||
const time = dash(first.Tarikh_Time ?? first.ZamanSabt, '');
|
const time = dash(first.Tarikh_Time ?? first.ZamanSabt, '');
|
||||||
const user = dash(first.User, '');
|
const user = dash(first.User, '');
|
||||||
@@ -106,9 +108,6 @@ export function printTahesabInvoicePdf(opts: {
|
|||||||
? `<p class="note foot-note">${escapeHtml(settings.footerText.trim())}</p>`
|
? `<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>
|
const html = `<!DOCTYPE html>
|
||||||
<html lang="fa" dir="rtl">
|
<html lang="fa" dir="rtl">
|
||||||
<head>
|
<head>
|
||||||
@@ -244,11 +243,45 @@ export function printTahesabInvoicePdf(opts: {
|
|||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
|
|
||||||
win.document.open();
|
const blob = new Blob([html], { type: 'text/html;charset=utf-8' });
|
||||||
win.document.write(html);
|
const url = URL.createObjectURL(blob);
|
||||||
win.document.close();
|
|
||||||
win.focus();
|
let win: Window | null =
|
||||||
setTimeout(() => {
|
targetWindow && !targetWindow.closed ? targetWindow : null;
|
||||||
win.print();
|
|
||||||
}, 300);
|
if (win) {
|
||||||
|
try {
|
||||||
|
win.location.href = url;
|
||||||
|
} catch {
|
||||||
|
try {
|
||||||
|
win.document.open();
|
||||||
|
win.document.write(html);
|
||||||
|
win.document.close();
|
||||||
|
} 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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user