Versiones comparadas

Clave

  • Se ha añadido esta línea.
  • Se ha eliminado esta línea.
  • El formato se ha cambiado.

...

Bloque de código
languagejs
return apiRequest({ url, responseType: "arraybuffer" });

Una vez recibido, para descargarlo podemos utilizar el código de la siguiente función, pasándole el pdf y el nombre del archivo a descargar:

Bloque de código
languagejs
// Descargar un pdf (arraybuffer)
descargarPdf(pdfArrayBuf, nombre) {
  const blob = new Blob([pdfArrayBuf], {
    type: 'application/pdf',
  });
  // IE doesn't allow using a blob object directly as link href
  // instead it is necessary to use msSaveOrOpenBlob
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob);
    return;
  }
  // For other browsers:
  // Create a link pointing to the ObjectURL containing the blob.
  const data = window.URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.href = data;
  link.download = nombre;
  link.click();
  setTimeout(() => {
    // For Firefox it is necessary to delay revoking the ObjectURL
    window.URL.revokeObjectURL(data);
  }, 100);
},

Si lo que queremos es abrirlo en otra pestaña del navegador, lo podemos hacer con esta otra función:

Bloque de código
languagejs
abrirPdf() {
      const blob = new Blob([this.matriculaPdf], { type: 'application/pdf' });
      // IE doesn't allow using a blob object directly as link href
      // instead it is necessary to use msSaveOrOpenBlob
      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob);
        return;
      }
      // For other browsers:
      // Create a link pointing to the ObjectURL containing the blob.
      const data = window.URL.createObjectURL(blob);
      // window.open(data, '_blank');
      const link = document.createElement('a');
      link.href = data;
      link.target = '_blank';
      link.click();
      setTimeout(() => {
        // For Firefox it is necessary to delay revoking the ObjectURL
        window.URL.revokeObjectURL(data);
      }, 100);
    },


Visor de PDF

Para Vue tenemos disponible vue-pdf, que podemos descargar con:

...