help icon indicating copy to clipboard operation
help copied to clipboard

Save Octet-stream data as PDF file in javascript

Open sriramgroot opened this issue 4 years ago • 18 comments

  • Node.js Version: 8.10.0
  • OS: macOS Mojave - version 10.14.6 (18G3020)
  • Scope (install, code, runtime, meta, other?): Final output should be a PDF file and it should not be blank document
  • Module (and version) (if relevant):

How to convert the below mentioned stream data into PDF file in nodejs Stream data sample: https://gist.github.com/sriramgroot/33e6e4934081f3d1b9d70c88873f2ad9

Response Header Screenshot 2020-03-25 at 11 01 40 AM

sriramgroot avatar Mar 25 '20 16:03 sriramgroot

Found a solution for saving the file, and it if possible in Scenario 1.

  • In Octet-stream you will be receiving the response as "Encoded Binary Data"
  • Your need to convert the response data into "Decoded Base64 String"
  • Finally save it as PDF

Also we can use NPM package - Base64toPDF for making the listed process as automated.

sriramgroot avatar Mar 30 '20 02:03 sriramgroot

Team, If there is any proper/best solution regarding this question please reply in the comment

Thanks in advance.

sriramgroot avatar Mar 30 '20 02:03 sriramgroot

I'm also experiencing the same issue.. pdf-doesnt-downloaded-node-express-puppeteer

I get the following headers in the response: image

As well as the content body as base64: image

I am seeking for a way to download/save it as PDF.

Help

levitomer avatar May 19 '20 13:05 levitomer

I'm also experiencing the same issue.. pdf-doesnt-downloaded-node-express-puppeteer

I get the following headers in the response: image

As well as the content body as base64: image

I am seeking for a way to download/save it as PDF.

Help

Hi @levitomer

Please check this solution, it solved my issue

sriramgroot avatar May 19 '20 16:05 sriramgroot

Sorry @sriramgroot , I don't want to use external library such as Base64toPDF library.

Only node..

levitomer avatar May 20 '20 08:05 levitomer

@sriramgroot can you show me how to use that library in the client, in order to download the pdf?

levitomer avatar May 20 '20 14:05 levitomer

Hi @levitomer We could not use this library in frontend/browser. It should be used in Node.js

sriramgroot avatar May 20 '20 16:05 sriramgroot

hi @sriramgroot I'm using the library you mentioned like this

const base64 = require('base64topdf');
let decodedBase64 = base64.base64Decode(resp.data, 'file.pdf');

but I'm getting a corrupt pdf file, not able to open it. I'm using your Stream data sample and still get the corrupt pdf file.

Do you know why is this happening? are you using any special header in the request?

yossely avatar Oct 06 '20 00:10 yossely

I could solve my problem, I needed to add responseType: 'arraybuffer' option in the get request like:

Axios.get(
  URL,
  {
    headers: {
      Authorization: `Bearer TOKEN`,
    },
    responseType: 'arraybuffer', // <- I was missing this option
  }
)

With that responseType option I get a buffer in response.data image

And finally I can create the pdf file with:

var fs = require('fs');
fs.writeFileSync('docName.pdf', resp.data);

yossely avatar Oct 06 '20 14:10 yossely

Why is the responseType: 'arraybuffer' so important in this case though?

AdrianTudC avatar Oct 12 '20 14:10 AdrianTudC

@AdrianTudC without that option, the response.data is not parsed correctly, that's why I was getting a corrupt file when trying to save the pdf

yossely avatar Oct 16 '20 14:10 yossely

I know that, but I was trying to understand how is axios manipulating the data with and without that option. It seems like without it it's just stringified with the default utf8 encoding which leads to data loss. I just wanted to be sure that it's not something reversible in my code.

AdrianTudC avatar Oct 16 '20 15:10 AdrianTudC

@AdrianTudC responseType: 'arraybuffer' is important in this case because without it, axios interprets the binary response from the server as a string. Specifying this option tells axios to preserve the binary response and parse it correctly to a Buffer instead of a string.

skylarmb avatar Mar 30 '21 20:03 skylarmb

@yossely I'm getting a blank white pdf even with the responseType: 'arraybuffer'

'content-type': 'application/octet-stream',
'content-disposition': 'attachment;filename=944883.pdf',

chcalibrr avatar Dec 10 '21 02:12 chcalibrr

I found it works if I set responseType:'blob' and then generate the PDF link like so:

const url = window.URL.createObjectURL(new Blob([response.data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', 'myPDF.pdf')
document.body.appendChild(link)
link.click()

Hope this helps someone ¯\_(ツ)_/¯

levibeach avatar Jun 17 '22 19:06 levibeach

when i use axios, i make a mistake image

and i change the responseType: 'arraybuffer' config. it useful image

artsooter avatar Jan 03 '23 02:01 artsooter

@sriramgroot were you able to use the above suggestion

preveen-stack avatar Jun 22 '23 12:06 preveen-stack

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar May 13 '24 01:05 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Jun 12 '24 01:06 github-actions[bot]