I want a PDF (generated on my server) to be sent to my client and opened in a new tab before being downloaded. I've already been able to get it downloaded directly from the server to the client, but now I need it to be displayed instead of being downloaded.
I created an Express server in version 4.15.0 and my client was done in VueJS using the Quasar Framework. Here are some codes to help you understand the question:
Server:
pdfMake = printer.createPdfKitDocument(docDefinition);
let stream = pdfMake.pipe(fs.createWriteStream('../pdfs/Tabela.pdf'));
pdfMake.end();
stream.on('finish', function() { //Método síncrono que só efetua o download do arquivo
// depois que o PDF está criado
if (fs.existsSync('C:/Projetos/Relatorios/pdfs/Tabela.pdf')) {
let file = 'C:/Projetos/Relatorios/pdfs/Tabela.pdf';
res.send(file); //Este método está certo?? Se não, qual devo usar??
}
})
Client:
axios({
method: 'post',
url: '/server/gerarpdf',
responseType: 'arraybuffer',
data: this.pessoas
})
.then(function (response) {
let blob = new Blob([response.data], {type: 'application/pdf'})
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
//Comentei o método abaixo pois ele efetuava o download, qual método devo utilizar para que o PDF seja visualizado ao invés de baixado??
// link.download = 'TabelaTeste.pdf'
link.click()
})