Обновление: проверенный код, это работает для меня:
const pdf = require('html-pdf');
const path = require('path');
const fs = require('fs');
let dir = path.join(__dirname, '/test');
let test = new Promise((resolve, reject) => {
pdf.create(html).toStream(function (err, stream) {
if (err) return reject(err);
// making directory if doesn't exist
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
stream.pipe(fs.createWriteStream(path.join(dir, `${data.link}.pdf`)));
});
});
В качестве альтернативы, вы можете использовать shelljs
:
const pdf = require('html-pdf');
const path = require('path');
const fs = require('fs');
const shell = require('shelljs');
let dir = path.join(__dirname, '../../../assets/docs/idv_docs');
let test = new Promise((resolve, reject) => {
// getting our html file
pdf.create(html).toStream(function (err, stream) {
if (err) return reject(err);
// making directory if doesn't exist
shell.mkdir('-p', dir);
// creating the pdf file
stream.pipe(fs.createWriteStream(path.join(dir, `${data.link}.pdf`)));
});
});