Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercept CTRL + S in order to properly save PDF #371

Open
alessandroamella opened this issue Jan 13, 2025 · 0 comments
Open

Intercept CTRL + S in order to properly save PDF #371

alessandroamella opened this issue Jan 13, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@alessandroamella
Copy link
Member

Per me sarebbe comodo intercettare i tasti in modo che, quando si è su desktop e si visualizza un file PDF, premendo CTRL + S venga scaricato il PDF anziché l'HTML della pagina.

Si può intercettare il CTRL + S così, anche se con un po' di prove ho visto che non funziona all'interno dell'iframe del PDF viewer (quindi il focus dev'essere fuori dall'iframe).

Un modo estremamente crudo è

document.addEventListener('keydown', e => {
  if (e.ctrlKey && e.key === 's') {
    e.preventDefault();
    downloadPdf();
  }
});

function downloadPdf() {
  const pdfUrl = 'url-al-pdf';

  fetch(pdfUrl, { mode: 'cors' }) // esempio con fetch
    .then(res => res.blob())
    .then(blob => {
      const url = window.URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', 'nome-file.pdf');
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(url);
    });
}

Non è una cosa importante, giusto un nice to have di bassissima priorità

@alessandroamella alessandroamella added the enhancement New feature or request label Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: New
Development

No branches or pull requests

1 participant