help
help copied to clipboard
Save Octet-stream data as PDF file in javascript
- 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
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.
Team, If there is any proper/best solution regarding this question please reply in the comment
Thanks in advance.
I'm also experiencing the same issue.. pdf-doesnt-downloaded-node-express-puppeteer
I get the following headers in the response:
As well as the content body as base64:
I am seeking for a way to download/save it as PDF.
Help
I'm also experiencing the same issue.. pdf-doesnt-downloaded-node-express-puppeteer
I get the following headers in the response:
As well as the content body as base64:
I am seeking for a way to download/save it as PDF.
Help
Hi @levitomer
Please check this solution, it solved my issue
Sorry @sriramgroot , I don't want to use external library such as Base64toPDF library.
Only node..
@sriramgroot can you show me how to use that library in the client, in order to download the pdf?
Hi @levitomer We could not use this library in frontend/browser. It should be used in Node.js
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?
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
And finally I can create the pdf file with:
var fs = require('fs');
fs.writeFileSync('docName.pdf', resp.data);
Why is the responseType: 'arraybuffer'
so important in this case though?
@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
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 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.
@yossely I'm getting a blank white pdf even with the responseType: 'arraybuffer'
'content-type': 'application/octet-stream',
'content-disposition': 'attachment;filename=944883.pdf',
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 ¯\_(ツ)_/¯
when i use axios, i make a mistake
and i change the responseType: 'arraybuffer'
config. it useful
@sriramgroot were you able to use the above suggestion
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.
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.