Well, the problem is that chunk
contains only one piece of data. I suggest you first load all the data into memory and wait for the end
event in the following manner.
https.get(options2, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk.toString();
});
resp.on('end', () => {
execSync(`wkhtmltopdf --user-style-sheet ${data} -s A3 ${file.baseUrl} download/9782206307909/htmlOut.pdf`);
});
}).
But now the problem may be that your code will contain spaces and because of that, you will receive an error because the command line will be malformed. You could try to wrap the data into quotes when you are spawning the process like so.
execSync(`wkhtmltopdf --user-style-sheet "${data}" -s A3 ${file.baseUrl} download/9782206307909/htmlOut.pdf`);
But this can be broken if the data contains the"
sign. The solution will be to escape "
signs in the string. Though I think the better way to handle the situation will be to save the content into a file and then run a wkhtmltopdf
tool and point it to a file.
resp.on('end', async () => {
const fileName = Math.random().toString(36); // You should use something more collision resistnat.
await fs.promises.writeFile(fileName, data);
// Now you need to run the wkhtmltopdf to and point it to a file. I don't know how to do that :)
});