fix: use translated filename as PDF print title

Read Content-Disposition filename from the HTML download response,
strip the extension, inject it as <title> before printing. Browser
uses <title> as the filename when saving to PDF.
This commit is contained in:
2026-07-13 17:06:14 +08:00
parent c770b7348d
commit 206ef091cc

View File

@@ -2004,7 +2004,19 @@
}, 3000); }, 3000);
const ifr = document.getElementById('printFrame'); const ifr = document.getElementById('printFrame');
fetch(url).then(r => r.text()).then(h => { fetch(url).then(r => {
const cd = r.headers.get('content-disposition') || '';
const match = cd.match(/filename[^;=\n]*=['"]?([^'"\n;]+)['"]?/i);
const printTitle = match ? match[1].replace(/\.[^.]+$/, '') : '';
return r.text().then(h => ({ 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(() => {