HTTP Client: How to create pdf when in HTTP body as JSON encapsulated

Answered

Hi, 

I have the situation that the HTTP client receives a PDF encapsulated by JSON in the HTTP body.

{  

"mimetype": "application/pdf",
 "base64String": “JVBERi0xLjQKJaqrrK0KMSAwIG9iago8PAovQ3JlYXRvciAoSU5GSU5JQ0EpCi9Qcm9kdWNlciAoSU5GSU5JQ0EpCi9DcmVhdGlvbkRhdGUgKEQ6MjAyNDA5MDkxMjE5MzdaKQo+PgplbmRvYmoKMiAwIG9iago8PAogIC9OIDMKICAvTGVuZ3RoIDMgMCBSCiAgL0ZpbHRlciAvRmxhdGVEZWNvZGUKPj4Kc3RyZWFtCnic7ZlnUFRZFoDve50TDd1Nk6HJSaKEBiTnJDmKCnQ3mRaaDCqKDI7ACC==”

}

The recommanded way to redirect it to a file >> output.pdf does not work becuase a JSON file is stored.

Honestly I didn't find any solution how to solve the issue with response handler and JavaScript.

Is there an example how the base64String will be decoded and store as a pdf in any folder. 

Thanks,

Markus

 

0
3 comments
Have you tried anything like below?

```
GET <URL>> {%const fs = require('fs');const pdfData = response.body.base64String;const buffer = Buffer.from(pdfData, 'base64');fs.writeFileSync('output.pdf', buffer);%}
```
0

Thanks Bond,

but it seems to be node.js. 

So far as I got it to work it looks like this:

import atob from "../../../pdf";

const byteString = atob(response.body.base64String);
const arrayBuffer = new ArrayBuffer(byteString.length);
const int8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < byteString.length; i++) {
    int8Array[i] = byteString.charCodeAt(i);
}
var blob = new Blob([int8Array], { type: 'application/pdf'});
var file = new File([blob], "output.pdf");

But I had to copy atob and further it does not work because Blob and File are unresolved types.

ArrayBuffer and Uint8Array are coming from lib.es5.d.ts which seems to be shipped by Intellij.

I assume I do not have enough JavaScript expierence.

0
Hello,
Please go to Settings | Languages & Frameworks | JavaScript | Libraries, click Download, choose '@types/worker-plugin', and click 'Download and install'. Does it help?
0

Please sign in to leave a comment.