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:
@@ -2004,7 +2004,19 @@
|
||||
}, 3000);
|
||||
|
||||
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.onload = () => {
|
||||
setTimeout(() => {
|
||||
|
||||
Reference in New Issue
Block a user