Skip to content

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

@alessandroamella

Description

@alessandroamella

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à

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions