2.0.5 • Published 6 months ago

table2canvas v2.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Draw table on canvas! Can be used both of node and browser.

Install

  • node (Must install canvas) npm install canvas table2canvas or yarn add canvas table2canvas

  • browser npm install table2canvas or yarn add table2canvas

Usage

  • node
import { Canvas } from 'canvas';
import Table2canvas from 'table2canvas';
import fs from 'fs';

const columns = [
    {
        title: 'name',
        children: [
            {
                title: 'first',
                dataIndex: 'first'
            },
            {
                title: 'last',
                dataIndex: 'last',
                render: (text, row, i) => {
                    if (i === 0) {
                        return { text, rowSpan: 3 }
                    }
                    if (i === 1) {
                        return { text, rowSpan: 0 }
                    }
                    if (i === 2) {
                        return { text, rowSpan: 0 }
                    }
                    return text;
                }
            }
        ]
    },
    { title: 'age', dataIndex: 'age', textAlign: 'center', textColor: 'blue' },
    { title: 'weight', dataIndex: 'weight', render: '{c}kg' },
    { title: 'address', dataIndex: 'address', width: 200 },
    {
        title: 'other-abcd',
        children: [
            {
                title: 'a',
                dataIndex: 'a',
                render: (text, row, i) => {
                    if (i === 2) {
                        return { text, colSpan: 2, rowSpan: 2 }
                    }
                    if (i === 3) {
                        return { text, colSpan: 0, rowSpan: 0 }
                    }
                    return text;
                }
            },
            {
                title: 'b',
                dataIndex: 'b',
                render: (text, row, i) => {
                    if (i === 2 || i === 3) {
                        return { text, colSpan: 0, rowSpan: 0 }
                    }
                    return text;
                }
            },
            {
                title: 'c+d',
                children: [
                    {
                        title: 'c',
                        dataIndex: 'c'
                    },
                    {
                        title: 'd',
                        dataIndex: 'd'
                    }
                ]
            }
        ]
    }
]

const dataSource: any[] = [
    { first: 'Jack', last: 'smith', age: 16, weight: 50, address: '1.somewhere\n2.somewhere', a: 'a1', b: 'b1', c: 'c1', d: 'd1' },
    { first: 'Jack', last: 'smith', age: 26, weight: 60, address: 'street9527123456789no.,it is a to long adress!', a: 'a2', b: 'b2', c: 'c2', d: 'd2' },
    { first: 'Jack', last: 'last', age: 36, weight: 70, address: 'where', a: 'merge-a+b\nline2\nline3', b: 'merge-a+b', c: 'c3', d: 'd3' },
    { first: 'Tom', last: 'last', age: 46, weight: 80, address: 'where', a: 'merge-a+b', b: 'merge-a+b', c: 'c4', d: 'd4' },
]

const table = new Table2canvas({
    canvas: new Canvas(2, 2),
    columns: columns,
    dataSource: dataSource,
    bgColor: '#fff',
    text: 'This is table title!',
})

const buffer = table.canvas.toBuffer();

fs.writeFileSync('demo.png', buffer);

result:demo.png basic usage

  • browser
import Table2canvas from 'table2canvas';

const columns = [...]

const dataSource = [...]

const table = new Table2canvas({
    canvas: document.createElement('canvas'),
    ...     
});

document.body.appendChild(table.canvas);

Options

TableOpt

PropertyDescriptiontypeDefaultRequired
canvasDraw table to whitch canvasCanvas | HTMLCanvasElement-yes
widthcanvas's widthnumber | 'auto''auto'no
heightcanvas's heightnumber | 'auto''auto'no
bgColorcanvas's background colorstring'transparent'no
paddingTable in canvas's paddingnumber | number[]10no
columnscolumns configIColumn[]-yes
dataSourcedataSource for tableRecord<string,any>--
styleTable's styleTableStyle...no
textTable's titlestring-no
textStyleTable's title style(color,fontSize,fontFamily,textAlign,lineHeight)TextStyle...no

IColumn

PropertyDescriptiontypeDefaultRequired
titleTable's td textstring-yes
dataIndexDisplay field of the data recordgstring-no
titleColortitle's colorstring'rgba(0,0,0,0.85)'no
titleFontWeighttitle's font weightstring'bold'no
titleFontSizetitle's font sizestringinherit TableStyle.fontSizeno
textAlignThe specify which way that column is aligned'left' | 'right' | 'center''left'no
textColorColor of column's bodystring'rgba(0,0,0,0.85)'no
textFontWeightfont weight of column's bodystring''no
textFontSizefont size of column's bodystringinherit TableStyle.fontSizeno
textOverflowtext overflow show type'auto' | 'ellipsis''auto'no
widthWidth of this columnnumberdefaultStyle.columnWidthno
childrenGroup table headIColumn[]-no
rendercustom render,if is string,{c} is rowdataIndexstring | Function-no

TableStyle

PropertyDescriptiontypeDefaultRequired
columnWidthdefault width of all columnsnumber150no
borderColorcolor of borderstring'#e8e8e8'no
textAlignThe specify which way that column is alignedstring'left'no
colorcolor of tablestring'rgba(0,0,0,0.85)'no
fontSizefontSize of tablestring'14px'no
fontFamilyfontFamily of tablestring'sans-serif'no
headerBgColorbackground color of header cellstring'rgba(0,0,0,0.02)'no
backgroundbackground color of tablestring-no
paddingpadding of table cellnumber | number[]16no
lineHeighttext lineHeightnumber22no
2.0.5

6 months ago

2.0.4

7 months ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago