Encoding PDF binary data to base64 not working with NodeJS
Asked Answered
C

3

11

I'm trying to get a PDF stream return that comes from a API and parse it to base64 to embbed it in the client side, the body of the API request is returning something like this:

    %PDF-1.5
%����
4 0 obj
<<
/Type/XObjcect
/Subtype/Image
/Width 799
/Height 70
/ColorSpace/DeviceGray
/BitsPerComponent 8
/Filter/FlateDecode
/Length 5181
>>
stream
x���=H#�������A�&�)���B���4iba�&O8H
.
.
. 
(The rest was omitted)

I'm trying to parse to base64 this way:

console.log(typeof body); // STRING
const encoded = new Buffer.from(body).toString('base64'); //PDF NOT WORKING

But when I'm getting this base64 and embedding it on the html it says that the file can't be oppened, the same thing happens when I trying to save it as .PDF file.

When I try to parse to base64 the same pdf but this time from a downloaded pdf, the base64 code when embedded in the html works fine.

  fs.readFile('/home/user/downloaded.pdf', function (err, data) {
    if (err) throw err;

    console.log(typeof data); //OBJECT
    const pdf = data.toString('base64'); //PDF WORKS
  });

I'm using const request = require('request'); to make the requests.

Catanddog answered 14/5, 2017 at 19:32 Comment(2)
Typeof body should be Buffer not a string, please show your code which make request. String can not contain binary data.Lundell
@Lundell Thank you for your answear, and yeah you are right, I add "encoding: null" in my request and I'm getting a buffer now instead of a string. I thought that parsing this string to a buffer would do the same, I don't know why it doesn't work.Catanddog
L
11

When you make your request you should set option encoding to null for getting Buffer instead of String.

request({
    method: 'GET',
    encoding: null,
    uri: 'http://youdomain.com/binary.data'
}, (err, resp, data)=>{
    console.log(typeof data) //should be an Object
    console.log(data.toString('base64'))
})
Lundell answered 14/5, 2017 at 19:43 Comment(2)
Hi, i tried the above code. it works fine and i also get the type as an object. though the data i still get is gibberish. any idea of what might the reason be?Batwing
Using axios, I set responseType: 'arrayBuffer', and responseEncoding: 'binary'. Now it works.Thank you any way, your answer pointed out the direction.Marshland
K
4

I've found solution on this article. You need add to your request some config.

axios.get({
    url: "https...",
    responseType: "arraybuffer",
    responseEncoding: "binary",
    headers: {
      "Content-Type": "application/pdf"
    }
});
Kalong answered 20/8, 2021 at 16:54 Comment(0)
J
1
const pdf2base64 = require('pdf-to-base64');
pdf2base64("test/sample.pdf")
    .then(
        (response) => {
            console.log(response);        }
    )
    .catch(
        (error) => {
            console.log(error);
        }
    )
Jonquil answered 9/8, 2021 at 7:50 Comment(1)
Hi, here are some guidelines : it is not recommended to post code-only answers, answers should provide more explanation about the code to make it the answer more useful and are more likely to attract upvotesBelayneh

© 2022 - 2024 — McMap. All rights reserved.