| | |
| | | import ImageModule from 'docxtemplater-image-module-free'; |
| | | import Pizzip from 'pizzip'; |
| | | import FileSaver from 'file-saver'; |
| | | import { renderAsync } from 'docx-preview'; |
| | | import printJS from 'print-js'; |
| | | import pdfMake from 'pdfmake'; |
| | | |
| | | /** |
| | | * 等比例缩放图片 |
| | |
| | | }; |
| | | } |
| | | |
| | | export const exportDocx = (tempDocpath, data, zipName, imageSize) => { |
| | | function prepareDocxtemplater(tempDocpath, data, imageSize) { |
| | | return new Promise((resolve, reject) => { |
| | | JSZipUtils.getBinaryContent(tempDocpath, (error, content) => { |
| | | if (error) { |
| | |
| | | } |
| | | doc.compile(); |
| | | doc.resolveData(data).then(() => { |
| | | try { |
| | | doc.render(); |
| | | } catch (error) { |
| | | console.log(error); |
| | | throw error; |
| | | } |
| | | const out = doc.getZip().generate({ |
| | | type: 'blob', |
| | | mimeType: |
| | | 'application/vnd.openxmlformats-officedocumnet.wordprocessingml.document' |
| | | }); |
| | | FileSaver.saveAs(out, zipName); |
| | | resolve(); |
| | | resolve(doc); |
| | | }); |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 准备docx文档,返回blob对象 |
| | | * @param {*} tempDocpath 模板docx文件路径 |
| | | * @param {*} data 数据对象 |
| | | * @param {*} zipName 压缩包名称 |
| | | * @param {*} imageSize 图片大小配置对象 |
| | | * @returns |
| | | */ |
| | | function prepareDocxBlob(tempDocpath, data, imageSize) { |
| | | return prepareDocxtemplater(tempDocpath, data, imageSize).then((doc) => { |
| | | try { |
| | | doc.render(); |
| | | } catch (error) { |
| | | console.log(error); |
| | | throw error; |
| | | } |
| | | const out = doc.getZip().generate({ |
| | | type: 'blob', |
| | | mimeType: |
| | | 'application/vnd.openxmlformats-officedocumnet.wordprocessingml.document' |
| | | }); |
| | | return out; |
| | | }); |
| | | } |
| | | |
| | | function prepareDocxStr(tempDocpath, data, imageSize) { |
| | | return prepareDocxtemplater(tempDocpath, data, imageSize).then((doc) => { |
| | | try { |
| | | return doc.render(); |
| | | } catch (error) { |
| | | console.log(error); |
| | | throw error; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | function preparePdf(tempDocpath, data, imageSize) { |
| | | return prepareDocxStr(tempDocpath, data, imageSize).then( (res) => { |
| | | // 将 Word 文档转换为 PDF 格式并保存到文件 |
| | | console.log(pdfMake); |
| | | |
| | | // const printer = new pdfMake.PdfPrinter(); |
| | | const docDefinition = { |
| | | content: [ |
| | | { |
| | | text: res.toString('utf8') |
| | | } |
| | | ] |
| | | }; |
| | | const pdfDoc = pdfMake.createPdf(docDefinition); |
| | | // const pdfBuffer = await pdfDoc.getBuffer(); |
| | | // return new Blob([pdfBuffer], { type: 'application/pdf' }); |
| | | return pdfDoc; |
| | | }); |
| | | } |
| | | |
| | | function exportDocx(tempDocpath, data, zipName, imageSize) { |
| | | return prepareDocxBlob(tempDocpath, data, zipName, imageSize).then((blob) => { |
| | | FileSaver.saveAs(blob, zipName); |
| | | }); |
| | | } |
| | | |
| | | function previewDocx(blob, ref) { |
| | | return renderAsync(blob, ref); |
| | | } |
| | | |
| | | function downloadDocx(blob, zipName) { |
| | | FileSaver.saveAs(blob, zipName); |
| | | } |
| | | |
| | | function print( |
| | | ref, |
| | | title, |
| | | style, |
| | | type, |
| | | jsonData, |
| | | borderHeadStyle, |
| | | gridStyle, |
| | | css |
| | | ) { |
| | | printJS({ |
| | | printable: ref, |
| | | header: title || null, |
| | | type: type || 'html', |
| | | headerStyle: |
| | | 'font-size:6px;font-weight:600;text-align:center;padding:15px 0 10px 0;', //标题设置 |
| | | properties: jsonData || [], //json数据元 |
| | | gridHeaderStyle: |
| | | borderHeadStyle || |
| | | 'font-size:6px;font-weight:400;height:40px;line-height:40px;border: 1px solid #ccc;padding:3px 5px 3px 5px;text-align:center;', //json格式表头样式 |
| | | gridStyle: |
| | | gridStyle || |
| | | 'font-size:1px;font-weight:200;border: 1px solid #ccc;padding:3px 5px 3px 5px;text-align:center;', //json各式表哥央视 |
| | | scanStyles: false, //不适用默认样式 |
| | | repeatTableHeader: false, //打印json表头只显示在第一页 |
| | | style: style || '@page{size:auto;margin: 0cm 1cm 0cm 1cm;}', //去除页眉页脚 |
| | | css: css || null //css url |
| | | }); |
| | | } |
| | | export { |
| | | exportDocx, |
| | | prepareDocxBlob, |
| | | preparePdf, |
| | | previewDocx, |
| | | downloadDocx, |
| | | print |
| | | }; |