fix: set parent page title before print for correct PDF filename
Chrome/Edge uses parent page title (not iframe title) when printing to PDF. Temporarily swap document.title to translated stem before print(), restore it 1s later after dialog opens.
This commit is contained in:
@@ -2010,18 +2010,14 @@
|
|||||||
const printTitle = match ? match[1].replace(/\.[^.]+$/, '') : '';
|
const printTitle = match ? match[1].replace(/\.[^.]+$/, '') : '';
|
||||||
return r.text().then(h => ({ h, printTitle }));
|
return r.text().then(h => ({ h, printTitle }));
|
||||||
}).then(({ h, printTitle }) => {
|
}).then(({ h, printTitle }) => {
|
||||||
if (printTitle) {
|
|
||||||
if (/<title>/i.test(h)) {
|
|
||||||
h = h.replace(/<title>[^<]*<\/title>/i, `<title>${printTitle}</title>`);
|
|
||||||
} else {
|
|
||||||
h = h.replace(/<head>/i, `<head><title>${printTitle}</title>`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ifr.srcdoc = h;
|
ifr.srcdoc = h;
|
||||||
ifr.onload = () => {
|
ifr.onload = () => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
ifr.contentWindow.focus();
|
ifr.contentWindow.focus();
|
||||||
|
const originalTitle = document.title;
|
||||||
|
if (printTitle) document.title = printTitle;
|
||||||
ifr.contentWindow.print();
|
ifr.contentWindow.print();
|
||||||
|
setTimeout(() => { document.title = originalTitle; }, 1000);
|
||||||
}, 500);
|
}, 500);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user