const { replaceDocx, docsToPdf } = require("../../lib/docs"); const Parse = global.Parse; const path = require("path") const TemplateDocxPath = path.join(__dirname,"template/模板-推荐申报表.docx") /** * 导出流程教材申报文件 */ async function exportProcessReportDocs(processId) { let query = new Parse.Query("EduTextbook") query.equalTo("eduProcess",processId); let textbookList = await query.find(); for (let index = 0; index < textbookList.length; index++) { let textbook = textbookList[index]; renderReportDocsByTextbook(textbook) } console.log(textbookList); } module.exports.exportProcessReportDocs = exportProcessReportDocs async function renderReportDocsByTextbook(textbook){ let json = textbook.toJSON(); // 圆圈选中未选 ○ 未选 ● 选中 let circleCheck = ["○","●"]; let squareCheck = [``,`☑`]; // 方块选中未选 ○ 未选 ● 选中 let bookData = { title:padString(json?.title,21), ISBN:padString(json?.ISBN,21), one:squareCheck[(json?.type=="单本"||json?.type=="单册")?1:0], // 单本/单册 ○ 未选 ● 选中 full:squareCheck[json?.type=="全册"?1:0], // 全册 } console.log(bookData) let bookid = json.code || json?.objectId; let tempFileName = path.join(`${bookid}${json.title}.docx`) replaceDocx(TemplateDocxPath,tempFileName,bookData,{onDocxComplete:(filePath)=>{ // 需要API支持 // docsToPdf(filePath) }}) } function padString(str,width) { width = width || 21 // 目标宽度为21个单位 spaceChar = " " // 占位符 // 计算字符串的宽度 let strWidth = 0; for (let char of str) { // 判断字符是否为中文 if (char.match(/[\u4e00-\u9fa5]/)) { strWidth += 4; // 中文字符占4个单位 } else { strWidth += 1; // 英文字符占1个单位 } } const totalPadding = width - strWidth; // 如果已经达到或超过目标宽度,直接返回原字符串 if (totalPadding <= 0) { return str; } // 计算左右两侧的空格数 const leftPadding = Math.floor(totalPadding / 2) * 3; const rightPadding = Math.ceil(totalPadding / 2) * 3; // 生成填充空格的字符串 const leftSpaces = spaceChar.repeat(leftPadding); const rightSpaces = spaceChar.repeat(rightPadding); // 返回补充后的字符串 return leftSpaces + str + rightSpaces; }