func-tbook-export.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const { replaceDocx, docsToPdf } = require("../../lib/docs");
  2. const Parse = global.Parse;
  3. const path = require("path")
  4. const TemplateDocxPath = path.join(__dirname,"template/模板-推荐申报表.docx")
  5. /**
  6. * 导出流程教材申报文件
  7. */
  8. async function exportProcessReportDocs(processId) {
  9. let query = new Parse.Query("EduTextbook")
  10. query.equalTo("eduProcess",processId);
  11. let textbookList = await query.find();
  12. for (let index = 0; index < textbookList.length; index++) {
  13. let textbook = textbookList[index];
  14. renderReportDocsByTextbook(textbook)
  15. }
  16. console.log(textbookList);
  17. }
  18. module.exports.exportProcessReportDocs = exportProcessReportDocs
  19. async function renderReportDocsByTextbook(textbook){
  20. let json = textbook.toJSON();
  21. // 圆圈选中未选 ○ 未选 ● 选中
  22. let circleCheck = ["○","●"];
  23. let squareCheck = [``,`☑`];
  24. // 方块选中未选 ○ 未选 ● 选中
  25. let bookData = {
  26. title:padString(json?.title,21),
  27. ISBN:padString(json?.ISBN,21),
  28. one:squareCheck[(json?.type=="单本"||json?.type=="单册")?1:0], // 单本/单册 ○ 未选 ● 选中
  29. full:squareCheck[json?.type=="全册"?1:0], // 全册
  30. }
  31. console.log(bookData)
  32. let bookid = json.code || json?.objectId;
  33. let tempFileName = path.join(`${bookid}${json.title}.docx`)
  34. replaceDocx(TemplateDocxPath,tempFileName,bookData,{onDocxComplete:(filePath)=>{
  35. // 需要API支持
  36. // docsToPdf(filePath)
  37. }})
  38. }
  39. function padString(str,width) {
  40. width = width || 21 // 目标宽度为21个单位
  41. spaceChar = "&#160;" // 占位符
  42. // 计算字符串的宽度
  43. let strWidth = 0;
  44. for (let char of str) {
  45. // 判断字符是否为中文
  46. if (char.match(/[\u4e00-\u9fa5]/)) {
  47. strWidth += 4; // 中文字符占4个单位
  48. } else {
  49. strWidth += 1; // 英文字符占1个单位
  50. }
  51. }
  52. const totalPadding = width - strWidth;
  53. // 如果已经达到或超过目标宽度,直接返回原字符串
  54. if (totalPadding <= 0) {
  55. return str;
  56. }
  57. // 计算左右两侧的空格数
  58. const leftPadding = Math.floor(totalPadding / 2) * 3;
  59. const rightPadding = Math.ceil(totalPadding / 2) * 3;
  60. // 生成填充空格的字符串
  61. const leftSpaces = spaceChar.repeat(leftPadding);
  62. const rightSpaces = spaceChar.repeat(rightPadding);
  63. // 返回补充后的字符串
  64. return leftSpaces + str + rightSpaces;
  65. }