【发布时间】:2020-08-27 09:28:35
【问题描述】:
我有一个函数可以创建一个 pdf 文件并使用 pdfkit 和 nodemailer 将其发送到电子邮件中,有时我会收到一个无法打开的文件。无法弄清楚为什么会发生这种情况以及为什么它大部分时间都有效?当它失败时,我没有注意到任何特定情况,其中似乎没有任何公式(文本长度等)。有人可以指出我的 pdf 创建代码中是否存在一些明显的问题(例如使用 async/await)。
exports.sendTranscriptionToEmail = async (req, res) => {
let finalText = [];
let nickColorsArray = [];
const doc = new PDFDocument();
let filename = req.body.sessionName;
let text = [];
if (!filename || typeof filename != "string") {
return res.status(400).send({ message: "Incorrect Information!" });
}
// here I get the data from the database
try {
const rows = await knex("transcriptions").select("*").where({
conversation_id: filename,
});
if (!rows) {
return res.status(400).send({ message: "Transcription not found" });
}
// Stripping special characters
filename = encodeURIComponent(filename) + ".pdf";
res.setHeader(
"Content-disposition",
'attachment; filename="' + filename + '"'
);
res.setHeader("Content-type", "application/pdf");
doc.pipe(fs.createWriteStream(filename));
doc.fontSize(18).fillColor("black").text("Participants:", {
width: 410,
align: "center",
});
doc.moveDown();
nickColorsArray.forEach((n) => {
doc.fontSize(14).fillColor(n.color).text(n.nick, {
width: 410,
align: "center",
});
});
doc.moveDown();
doc.moveDown();
doc.fontSize(18).fillColor("black").text("Transcription:", {
width: 410,
align: "center",
});
doc.moveDown();
finalText.forEach((f) => {
doc
.fontSize(14)
.fillColor(f.color)
.text(f.word + " ", {
width: 410,
continued: true,
});
});
doc.end();
} catch (err) {
console.log("Something went wrong: ", err.message);
}
【问题讨论】:
-
看不到你在任何地方返回你的流:)
标签: javascript node.js asynchronous async-await pdfkit