2
 ...
 PdfWriter.getInstance(document, outputStream); // outputstream from java 

I successfully downloaded this pdf file by fetch api and postman, but failed by axios, even though the size was normal.

// I successfully downloaded this pdf file by fetch api and postman, but failed by axios, even though the size was normal(blank content).
axios({
 url: `xxx`,
 method: 'get',
 responseType: 'arraybuffer'
}).then(resp => {
  const url = window.URL.createObjectURL(new Blob([resp.data], {type:"application/pdf"}));
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', 'test.pdf');
  document.body.appendChild(link);
  link.click();
})

Can someone help me?

SnowShaper
  • 33
  • 3
  • Does this article provide any insight? For some reason my spidy senses are focusing on arraybuffer and the application/pdf, but I don't really know. https://stackoverflow.com/questions/60454048/how-does-axios-handle-blob-vs-arraybuffer-as-responsetype – Dan Chase Jun 17 '21 at 03:28
  • What if you change the arraybuffer to stream, like in this problem report? https://github.com/axios/axios/issues/1392 – Dan Chase Jun 17 '21 at 03:30
  • @DanChase . I tried, but it still didnt work. – SnowShaper Jun 17 '21 at 03:59
  • Open developer tools, go to the network tab, and do it again with that open, do you get a 200 response code? Put a breakpoint at const url=, and put the cursor over "resp." and see if data has anything. – Dan Chase Jun 17 '21 at 04:26
  • @DanChase. The code is 200. And I find out that the response.data returned by axios is the same as the response when I request the server by postman. – SnowShaper Jun 17 '21 at 04:40
  • In that case, it sounds like Axios is working correctly, and something must be up with the 2nd half of the code. Maybe start out by separating it a bit, instead of saying new Blob, set a variable ahead of time and inspect to make sure the Blob has the same data. – Dan Chase Jun 17 '21 at 05:11
  • @DanChase. I provisionally use fetch api instead. I may try it latter if I have time. BTW, really really thx for your help. – SnowShaper Jun 17 '21 at 11:02
  • @DanChase. I have sovled this problem. It caused by Mock's responseType confliction. – SnowShaper Jun 18 '21 at 03:37

1 Answers1

0

Remove or uninstall MockJS, and try it again!

SnowShaper
  • 33
  • 3