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:
@@ -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>`;
|
||||
|
||||
win.document.open();
|
||||
win.document.write(html);
|
||||
win.document.close();
|
||||
win.focus();
|
||||
setTimeout(() => {
|
||||
win.print();
|
||||
}, 300);
|
||||
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();
|
||||
} 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