1

For an API I need the possibility to create a byte array from a local pdf file.

I've already checked StackOverflow, but the existing solutions do not work. Whenever I try to execute the code I get a bad request from the soap API. If I send zero bytes then I get an error message that was sending zero bytes. The credentials also work. The only thing I can not solve is how I can generate a byte array from a pdf file and send it via node-soap.

Here my code:

var fs = require('fs');
var zlib = require('zlib');
var soap = require('soap');
var path_to_wsdl = "url to wsdl";

var data = {};

data['Authentication'] = {
    'Username': "myusername",
    'Password': "mypass"
}

data['Options'] = {
    'Testmode': true
}

data['Recipient'] = {
    'Company': "Firma",
    'FirstName': 'Vorname',
    'Name': 'Name',
    'Address': 'Street Nr.',
    'Zip': "68753",
    'City': 'Stadt',
    'Country': 'DE'
}

var smabuf = fs.createReadStream('test.pdf');

data['File'] = {
    'Filename': "test.pdf",
    'Bytes': smabuf,
    'Color': false,
    'Duplex': false,
    'Enveloping': 0
}

soap.createClient(path_to_wsdl, function (err, client) {
    if (err) {
        console.log(err);
    }
    client.BriefManager.BriefManagerSoap.CreateJob(data, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            console.log(result);
        }
    });
});
sm_a
  • 11
  • 4

1 Answers1

0

I was running through this issue and my solution was to use fs library to read the target file in base64 encoding and send it within the request.

/**
 * Get a file buffer as a bytes array string
 * @param filePath Path to the file
 */
 getFileAsBytesArray(filePath) {
    return fs.readFileSync(filePath, { encoding: 'base64' });
 }

Then use it in the file bytes field.