简体   繁体   中英

PDF is blank when opened

I've been at this for hours and I'm not getting anywhere. I'm trying to download a PDF file from my server. I'm currently running this on localhost. When I open the PDF the PDF is a white blank page. Can someone please let me know what I'm missing. I've checked the PDF saved on the server and it is fine.

App - React, Express.

NPM - Axios, file-saver, html-pdf

Path - Front end React

.then(() =>
  axios.get(
    'http://localhost:3000/retailers/fetch/pdf',
    {
      headers: {
        'Content-type': 'application/json',
        'x-auth-token': this.props.token
      }
    },
    { responseType: 'blob' }
  )
)
.then((res) => {
  console.log('dd', res);
  const pdfBlob = new Blob([res.data], { type: 'application/pdf' });

  saveAs(pdfBlob, 'newPdf.pdf');
});

Path - Backend express

const options = {
  root: path.join(__dirname, '../'),
  dotfiles: 'deny',
  headers: {
    'x-timestamp': Date.now(),
    'x-sent': true
  }
};

var fileName = 'test.pdf';
res.sendFile(fileName, options, function (err) {
  if (err) {
    console.log('err', err);

    next(err);
  }
});

So the answer is simple. responseType: 'blob' was in the wrong place.

axios.get(
  'http://localhost:3000/retailers/fetch/pdf',
  {
    headers: {
      'Content-type': 'application/json',
      'x-auth-token': this.props.token
    },
    responseType: 'blob'
  }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM