1.0.6 • Published 3 years ago

docxload v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

docxload

NPM version

EN Doc

通过标签模板的配置来实现 .docx 文件的前端导出。

docxload 是基于 docx 包实现的 .docx 文件导出工具。docx 包拥有丰富的配置,能够实现绝大多数 docx 文档的样式需求。但其细致的配置也导致了配置代码相对复杂,当文档样式或内容过多时,其嵌套配置的方式会导致代码可读性降低,维护不方便。 docxload 简化了 docx 的配置,将一些基础配置封装,并以标签的形式暴露使用。docxload 通过标签的组合来完成配置,使配置工作更快,代码更易读;并且它支持 docx 的大多数配置,充分利用了 docx 的能力,详细配置请见下文。

安装

$npm install --save docxload

使用

import docxload from 'docxload'

// 标签属性值为 String 类型时,用双引号表示值
// 标签属性值为 js 表达式时,用大括号表示值
// 一个属性中,可以有多个子属性配置,形式为:"key1: value1; key2: value2;",如 underline 属性
let template = `
  <page>
    <p>
      <span 
        underline="type: single; color: #000;"
        color="#000"
      >Hello, </span>
      <span font-size={30}>docxload</span>
    </p>
  </page>
`

docxload(template).then(() => {
  console.log('done')
}).catch(err => {
  console.log('failed', err)
})

更多配置示例,见测试用例

API

function docxload(template: string, option?: object | string): Promise

方法参数:

docxload 方法支持两个参数: template:标签模板;option:配置选项,为可选参数;option 为 string 类型时,可配置导出文件的文件名;option 为 object 类型时,有以下配置选项: | 配置字段 | 描述 | 字段类型 | 默认值 | | - | - | :-: | - | | fileName | 导出文件的文件名,默认后缀名是 .docx | String | data.docx | | immediate | 是否立即导出文件; 若为 false, 程序将生成文档的二进制文件,但不导出 | Boolean | true |

返回值:

docxload 方法执行后将返回一个 Promise 对象,该 Promise 对象的 resolve 方法会传递一个数组 blob, exportFile,用于扩展操作: | 数组成员 | 描述 | 值类型 | | - | - | - | | blob | 待导出文件的二进制对象 | Blob | | exportFile | 用于导出文件的方法 exportFile(blob, fileName)接收两个参数:blob为二进制对象,fileName为文件名 | Function |

使用示例:

let template = ...
function docxToPdf() { ... }

docxload(template, { immediate: false }).then(([blob, exportFile]) => {
  // 对 blob 对象进行处理,如转成 pdf 格式
  let pdfBlob = docxToPdf(blob)
  exportFile(pdfBlob, 'data.pdf')
}).catch(err => {
  console.log('failed', err)
})

标签类型

docxload 中的标签有两种类型: 一种是与 docx 包中的类相对应的标签,可支持对应类中的配置选项; 另一种是通过封装 docx 包中的一些配置而实现的标签。

标签描述docx 中对应的类是否可配置属性
\\文档中的一页-
\\标题-×
\\段落Paragraph
\\文本TextRun
\图片ImageRun
\\表格-
\\表格行-
\\单元格-
\换行-×
\\占位标签,无实义-×

标签需按以下层级规则进行嵌套,同级标签不可嵌套:

page > title, p, table > span, img, br

table > row > cell

注意标签中的第二级必须是 title、p、table 之一

template 标签可嵌套在任意标签层级;在使用 jsx语法编写模板时,有些环境要求模板必须有根标签,此时\可作为根标签。

标签属性

以下属性简化了 docx 包的配置,其他属性配置见 docx 包中对应类的配置,部分属性配置与 docx 文档不一样时,以本文档为准。

page

属性名描述参数类型可选值默认值
orientation页面方向Stringvertical - 垂直方向 horizontal - 水平方向vertical
header页眉,有多个子属性: alignment: 水平对齐;format: 字符串模板,该模板提供了模板变量,分别为:当前页码 $pageCurrent,总页数 $pageTotalarea: 规定允许显示表头的页面;支持 \<span> 标签中的所有属性Stringalignment:center, left, right, distribute, both, start, end;area:all - 所有页面,odd - 奇数页,even - 偶数页-
footer页脚,配置规则与页眉相同String--

p

属性名描述参数类型可选值默认值
alignment水平对齐Stringcenter, left, right, distribute, both, start, endleft
heading标题等级Stringtitle, heading_1, heading_2, heading_3, heading_4, heading_5, heading_6-
indent增加缩进,一个单位为一次Tab缩进Number--
spacing段落间距,有4个子属性:before: 段前间距,数值类型,单位:磅;after: 段后间距,数值类型,单位:磅;line-rule: 行距规则;line: 行距值,数值类型,单位由 line-rule 确定;Stringline-rule:multiple - 多倍行距,line 的单位为行atLeast - 最小行距,line 的单位为磅-

span

属性名描述参数类型可选值默认值
font-size文字大小Number-20
bold文字加粗Boolean-false
italics斜体文字Boolean-false
color文字颜色,颜色名称 或者 16进制颜色String--
highlight高亮文字,颜色名称String--
all-caps全部大写字母Boolean-false
small-caps小型大写字母Boolean-false
strike删除线Boolean-false
double-strike双删除线Boolean-false
super-script上标Boolean-false
sub-script下标Boolean-false
underline下划线 当为Boolean类型,且值为true时,使用默认下划线样式;当为String类型时,有两个子属性:type: 下划线类型;color: 下划线颜色String, Boolean下划线类型 type:single, word, double, thick, dotted, dottedheav, dash, dashedheav, dashlong, dashlongheav, dotdash,dashdotheavy, dotdotdas, dashdotdotheavy, wave, wavyheavy, wavydouble;false

img

属性名描述参数类型可选值默认值
width图片宽度Number-100
height图片高度Number-100
src图片资源,可以是网络地址,或是base64格式;当 src 值为网络地址时,docxload 将自动请求图片资源String--

table

属性名描述参数类型可选值默认值
width表格总宽度,单位%String-100%

row

属性名描述参数类型可选值默认值
height行高,单位cmString, Number-1cm

cell

属性名描述参数类型可选值默认值
width单元格宽度,单位%,百分比相对于表格总宽度String-默认平分单元格宽度
colspan合并列Number--
rowspan合并行Number--
align水平对齐Stringcenter, left, right, distribute, both, start, endcenter
vertical-align垂直对齐Stringcenter, bottom, topcenter
fontSize字体大小Number-20
border单元格边框,有三个子属性配置: style: 边框类型 size: 边框宽度 数值 color: 边框颜色 16进制颜色Stringstyle: single, dash_dot_stroked, dashed,dash_small_gap, dot_dash, dot_dot_dash, dotted, double, double_wave, inset, nil, none, outset, thick, thick_thin_large_gap, thick_thin_medium_gap, thick_thin_small_gap, thin_thick_large_gap, thin_thick_medium_gap, thin_thick_small_gap, thin_thick_thin_large_gap,thin_thick_thin_medium_gap, thin_thick_thin_small_gap, three_d_emboss, three_d_engrave, triple, wave-
border-top border-right border-bottom border-left单元格单边边框,参数与border属性相同String--
1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.1.5

3 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.0.2

3 years ago

0.0.0

3 years ago