index.js 20 KB

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