index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. const fs = require('fs')
  2. const path = require('path')
  3. const compressing = require('compressing')
  4. const rimrif = require('rimraf')
  5. const shell = require('shelljs');
  6. const crypto = require('crypto');
  7. // 文档模板生成
  8. const PizZip = require("pizzip");
  9. const Docxtemplater = require("docxtemplater");
  10. // 文档转换
  11. import { Chromiumly } from "chromiumly";
  12. // Chromiumly.configure({ endpoint: "http://8.140.98.43/docs" });
  13. Chromiumly.configure({ endpoint: "http://123.57.204.89/docs" });
  14. const { LibreOffice } = require("chromiumly");
  15. // const { PDFEngines } = require("chromiumly");
  16. const tempDir = path.join(__dirname , "temp");
  17. if(!fs.existsSync(tempDir)){fs.mkdirSync(tempDir)};
  18. const OSS = require("ali-oss");
  19. const ALI_OSS_BUCKET = process.env.ALI_OSS_BUCKET || "hep-textbook"
  20. const ALI_OSS_ACCESS_KEY_ID = process.env.ALI_OSS_ACCESS_KEY_ID || "LTAI5t6AbTiAvXmeoVdJZhL3"
  21. const ALI_OSS_ACCESS_KEY_SECRET = process.env.ALI_OSS_ACCESS_KEY_SECRET || "KLtQRdIW69KLP7jnzHNUf7eKmdptxH"
  22. const bwipjs = require("bwip-js")
  23. export async function toBarCode(text){
  24. return new Promise(resolve=>{
  25. bwipjs.toBuffer({
  26. bcid:"code128",
  27. text:text,
  28. scale:1.5,
  29. height:3,
  30. includetext:false,
  31. textalign:"center"
  32. },(err,png)=>{
  33. if(err){
  34. console.error(err)
  35. resolve(null)
  36. }else{
  37. resolve(png)
  38. }
  39. })
  40. })
  41. }
  42. export async function uploadFileToOSS(filePath){
  43. let client = new OSS({
  44. // yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。
  45. region: "oss-cn-beijing",
  46. accessKeyId: ALI_OSS_ACCESS_KEY_ID,
  47. accessKeySecret: ALI_OSS_ACCESS_KEY_SECRET,
  48. // 填写Bucket名称。
  49. bucket: ALI_OSS_BUCKET || "hep-textbook",
  50. });
  51. let now = new Date();
  52. let fileName = getFileName(filePath);
  53. let fileKey = `export/report/${fileName}`;
  54. const r1 = await client?.put(fileKey, filePath);
  55. console.log('put success: %j', r1);
  56. return r1
  57. }
  58. export function getFileName(filePath) {
  59. // 使用 '/' 或 '\' 作为分隔符,分割路径
  60. const parts = filePath.split(/[/\\]/);
  61. // 返回最后一个部分,即文件名
  62. return parts.pop();
  63. }
  64. module.exports.uploadFileToOSS = uploadFileToOSS
  65. /**
  66. * 将给定的文件路径数组打包成指定名称的zip压缩包
  67. * @param {Array<string>} filePathList - 要打包的文件路径数组
  68. * @param {string} outputZipName - 输出的zip文件名称
  69. */
  70. export function createZip(filePathList, outputZipName) {
  71. let zipStream = new compressing.zip.Stream();
  72. return new Promise((resolve)=>{
  73. try {
  74. let outputPath = path.join(tempDir,outputZipName)
  75. // 遍历文件路径列表,将每个文件添加到zip流中
  76. for (const filePath of filePathList) {
  77. // 检查文件是否存在
  78. if (fs.existsSync(filePath)) {
  79. // 将文件添加到zip流中
  80. zipStream.addEntry(filePath);
  81. } else {
  82. console.error(`文件不存在: ${filePath}`);
  83. }
  84. }
  85. // 创建一个写入流
  86. const output = fs.createWriteStream(outputPath);
  87. // 使用 compressing 库的 zip 方法将文件打包
  88. // console.log(filePathList)
  89. // await compressing.zip.compressDir(filePathList, output);
  90. // 将zip流写入文件
  91. zipStream.pipe(output);
  92. output.on('finish', () => {
  93. // console.log(`成功创建压缩包: ${outputPath}`);
  94. resolve(outputPath)
  95. });
  96. output.on('error', (error) => {
  97. console.error('写入压缩包时出错:', error);
  98. resolve(null)
  99. });
  100. // console.log(`成功创建压缩包: ${outputPath}`);
  101. // return outputPath
  102. } catch (error) {
  103. console.error('创建压缩包时出错:', error);
  104. return null
  105. }
  106. })
  107. }
  108. module.exports.createZip = createZip
  109. const download = require('download')
  110. async function downloadUrl(url) {
  111. console.log(url)
  112. if(url?.startsWith("/")) {return url};
  113. let md5 = crypto.createHash('md5');
  114. let extname = path.extname(url)?.toLocaleLowerCase();
  115. let filename = md5.update(url).digest('hex') + extname;
  116. let filepath = path.join(tempDir,filename)
  117. // console.log(filename,filepath)
  118. try{
  119. // if(fs.existsSync(filepath)){fs.rmSync(filepath)} // 存在则删除
  120. if(fs.existsSync(filepath)){return filepath} // 存在则直接返回(md5相同)
  121. fs.writeFileSync(filepath, await download(url));
  122. return filepath
  123. }catch(err){
  124. console.error(err)
  125. return null
  126. }
  127. }
  128. /**
  129. * 将 DOCX 文件转换为 PDF
  130. *
  131. * @param {string} docxPath - 要转换的 DOCX 文件的路径
  132. * @param {string} outputPath - 输出 PDF 文件的路径
  133. * @returns {Promise<void>}
  134. */
  135. export async function docxToPdf(docxPath, outputPath,options) {
  136. let mergeFiles = options?.mergeFiles || []
  137. let merge = false;
  138. let mergeFileMap = {};
  139. if(mergeFiles?.length){
  140. let plist = []
  141. for (let index = 0; index < mergeFiles.length; index++) {
  142. let filePath
  143. plist.push((async ()=>{
  144. try{
  145. filePath = await downloadUrl(mergeFiles[index]);
  146. }catch(err){}
  147. if(filePath){
  148. mergeFileMap[index] = filePath // 按原有顺序整理
  149. // filePathList.push(filePath)
  150. }
  151. return
  152. })())
  153. }
  154. await Promise.all(plist);
  155. merge = true;
  156. }
  157. let filePathList = mergeFiles?.map((item,index)=>mergeFileMap[index]).filter(item=>item)
  158. // console.log("DOWNLOADED:",filePathList)
  159. filePathList = filePathList.map((filepath,index)=>{
  160. // 按顺序修改文件前缀数字为字母表顺序
  161. let fileDir = path.dirname(filepath);
  162. let abc = String.fromCharCode(96+(index+1)); // 字母顺序不会出现 把 1 10 11 12 放在一起的情况
  163. let num = index+110; // 数字顺序从百位开始,避免首数字排序错乱
  164. let fileName = num + "_" + path.basename(filepath)
  165. let orderPath = path.join(fileDir,fileName)
  166. fs.cpSync(filepath,orderPath);
  167. fs.readFileSync(filepath);
  168. return orderPath
  169. })
  170. try {
  171. let files = []
  172. if(docxPath){
  173. let docxBuffer = fs.readFileSync(docxPath);
  174. files.push({ data: docxBuffer, ext: "docx" })
  175. }
  176. files = [...files,...filePathList]
  177. let convertOpts = {
  178. files,
  179. properties: {
  180. // 设置页面属性,例如纸张大小和方向
  181. pageSize: 'A4',
  182. orientation: 'portrait',
  183. margin: {
  184. top: 0,
  185. right: 0,
  186. bottom: 0,
  187. left: 0
  188. }
  189. },
  190. pdfa: false, // 根据需要设置
  191. pdfUA: false, // 根据需要设置
  192. merge: merge, // 如果只转换一个文件,设置为false
  193. // metadata: {
  194. // // 你可以在这里添加元数据
  195. // },
  196. // losslessImageCompression: false,
  197. // reduceImageResolution: false,
  198. // quality: 90, // JPG 导出质量
  199. // maxImageResolution: 300 // 最大图像分辨率
  200. }
  201. // console.log("convertOpts",convertOpts)
  202. let pdfBuffer
  203. // 方式1:逐个合并
  204. // let pdfBuffer
  205. // for (let index = 1; index < files.length; index++) {
  206. // let file = files[index];
  207. // if(pdfBuffer){
  208. // convertOpts.files = [{data:pdfBuffer,ext:"pdf"},file]
  209. // }else{
  210. // convertOpts.files = [file]
  211. // }
  212. // pdfBuffer = await LibreOffice.convert(convertOpts);
  213. // }
  214. // 方式2:先合并pdf,后合并docx
  215. if(files?.length>4){
  216. let pdfList = files.slice(1);
  217. let mergedFile = await mergePdfListReduce(pdfList,convertOpts)
  218. convertOpts.files = [files[0],mergedFile]
  219. // console.log(convertOpts)
  220. pdfBuffer = await LibreOffice.convert(convertOpts);
  221. }else{
  222. pdfBuffer = await LibreOffice.convert(convertOpts);
  223. }
  224. // 方式3:全部合并
  225. // let pdfBuffer = await LibreOffice.convert(convertOpts);
  226. // 将 Buffer 写入输出文件
  227. fs.writeFileSync(outputPath, pdfBuffer);
  228. console.log(`成功输出 ${outputPath}`);
  229. return outputPath
  230. } catch (error) {
  231. console.error('转换失败:', error);
  232. return null
  233. }
  234. }
  235. module.exports.docxToPdf = docxToPdf
  236. const ImageModule = require("@slosarek/docxtemplater-image-module-free");
  237. const sizeOf = require("image-size");
  238. /**
  239. * 每三个pdf合并一次,直到合并为一个pdf为止
  240. * @param {} pdfList
  241. * @param {*} convertOpts
  242. * @returns
  243. */
  244. export async function mergePdfListReduce(pdfList,convertOpts){
  245. // console.log("pdfList",pdfList)
  246. let mergeList = []
  247. let plist = []
  248. let length = pdfList.length
  249. for (let index = 0; index < length; index++) {
  250. let file = pdfList.shift();
  251. // console.log(file,index,length)
  252. if(!file) break;
  253. let files = [file,pdfList.shift(),pdfList.shift()]; // 每次合并三个
  254. files=files?.filter(item=>item);
  255. // console.log(files)
  256. plist.push(new Promise(async resolve=>{
  257. if(files?.length==1){ // 单文件直接加载 自动获取后缀
  258. let onefile = files[0]
  259. if(!onefile?.ext){
  260. let extname = path.extname(files[0]).slice(1)?.toLocaleLowerCase();
  261. onefile = {data:fs.readFileSync(onefile),ext:extname}
  262. }
  263. resolve(onefile);
  264. }else{ // 多文件合并
  265. convertOpts.files = files;
  266. // console.log("多文件合并",convertOpts)
  267. let mergeBuffer = await LibreOffice.convert(convertOpts);
  268. resolve({data:mergeBuffer,ext:"pdf"})
  269. }
  270. }))
  271. }
  272. if(plist?.length){
  273. mergeList = await Promise.all(plist);
  274. }
  275. // console.log("mergeList",mergeList)
  276. if(mergeList?.length==1){
  277. return mergeList[0];
  278. }else{
  279. // console.log("mergePdfListReduce continue:",mergeList)
  280. return await mergePdfListReduce(mergeList,convertOpts)
  281. }
  282. }
  283. export function renderDocx(inputDocxPath, outputDocxName, options){
  284. let imageOptions = {
  285. getImage(tagValue,tagName) {
  286. if(!fs.existsSync(tagValue)){
  287. throw new Error(`Image not found: ${tagValue}`);
  288. }
  289. return fs.readFileSync(tagValue);
  290. },
  291. getSize(img) {
  292. const sizeObj = sizeOf(img);
  293. console.log(sizeObj);
  294. return [sizeObj.width, sizeObj.height];
  295. },
  296. };
  297. let outputDocxPath = path.join(tempDir,outputDocxName)
  298. // Load the docx file as binary content
  299. let content = fs.readFileSync(
  300. inputDocxPath,
  301. "binary"
  302. );
  303. // Unzip the content of the file
  304. let zip = new PizZip(content);
  305. let doc = new Docxtemplater(zip, {
  306. paragraphLoop: true,
  307. linebreaks: true,
  308. modules: [new ImageModule(imageOptions)],
  309. });
  310. // Render the document (Replace {first_name} by John, {last_name} by Doe, ...)
  311. Object.keys(options).forEach(key=>{ // 除去空值
  312. if(options[key]==undefined){
  313. options[key] = ""
  314. }
  315. })
  316. doc.render(options);
  317. // Get the zip document and generate it as a nodebuffer
  318. let buf = doc.getZip().generate({
  319. type: "nodebuffer",
  320. // compression: DEFLATE adds a compression step.
  321. // For a 50MB output document, expect 500ms additional CPU time
  322. compression: "DEFLATE",
  323. });
  324. // buf is a nodejs Buffer, you can either write it to a
  325. // file or res.send it with express for example.
  326. fs.writeFileSync(outputDocxPath, buf);
  327. return outputDocxPath
  328. }
  329. /**
  330. * docx 替换模板字符串内容
  331. * @example
  332. // 要替换内容的模板
  333. let inputDocx = 'cs.docx'
  334. // 替换完成的docx文件
  335. let outputDocx = 'dd.docx'
  336. // {{xx}} 处要替换的内容
  337. let replaceData = {
  338. name: '替换name处的内容',
  339. age: '替换age处的内容',
  340. }
  341. replaceDocx(inputDocx, outputDocx, replaceData)
  342. */
  343. export function replaceDocx(inputDocxPath, outputDocxPath, options,eventMap) {
  344. return new Promise((resolve,reject)=>{
  345. // 解压出来的临时目录
  346. let md5 = crypto.createHash('md5');
  347. let outmd5 = md5.update(outputDocxPath).digest('hex')
  348. let tempDocxPath = path.join(tempDir , outmd5)
  349. // 要替换的xml文件位置
  350. let tempDocxXMLName = path.join(tempDocxPath,`word/document.xml`)
  351. // 压缩文件夹为文件
  352. let dir_to_docx = (inputFilePath, outputFilePath) => {
  353. outputFilePath = path.join(tempDir,outputFilePath)
  354. // 创建压缩流
  355. let zipStream = new compressing.zip.Stream()
  356. // 写出流
  357. let outStream = fs.createWriteStream(outputFilePath)
  358. fs.readdir(inputFilePath, null, (err, files) => {
  359. if (!err) {
  360. files.map(file => path.join(inputFilePath, file))
  361. .forEach(file => {
  362. zipStream.addEntry(file)
  363. })
  364. }
  365. })
  366. // 写入文件内容
  367. zipStream.pipe(outStream)
  368. .on('close', () => {
  369. // 打包完成后删除临时目录
  370. // console.log(tempDocxPath)
  371. eventMap["onDocxComplete"]&&eventMap["onDocxComplete"](outputFilePath)
  372. shell.rm("-r",tempDocxPath)
  373. // rimrif.rimrafSync(tempDocxPath)
  374. resolve(true)
  375. })
  376. }
  377. // 替换word/document.xml文件中{{xx}}处的内容
  378. let replaceXML = (data, text) => {
  379. Object.keys(data).forEach(key => {
  380. text = text.replaceAll(`{{${key}}}`, data[key])
  381. })
  382. return text
  383. }
  384. // 解压docx文件替换内容重新打包成docx文件
  385. compressing.zip.uncompress(inputDocxPath, tempDocxPath)
  386. .then(() => {
  387. // 读写要替换内容的xml文件
  388. fs.readFile(tempDocxXMLName, null, (err, data) => {
  389. if (!err) {
  390. let text = data.toString()
  391. text = replaceXML(options, text)
  392. fs.writeFile(tempDocxXMLName, text, (err) => {
  393. if (!err) {
  394. dir_to_docx(tempDocxPath, outputDocxPath)
  395. } else {
  396. reject(err)
  397. }
  398. })
  399. } else {
  400. reject(err)
  401. }
  402. })
  403. }).catch(err => {
  404. reject(err)
  405. })
  406. })
  407. }
  408. module.exports.replaceDocx = replaceDocx
  409. function generateObjectId(inputString) {
  410. inputString = inputString || ""
  411. inputString = String(inputString)
  412. const hash = crypto.createHash('sha256').update(inputString).digest('hex');
  413. const objectId = hash;
  414. return objectId;
  415. }