diff --git a/examples/tables.js b/examples/tables.js index 02210c2a2..097daabca 100644 --- a/examples/tables.js +++ b/examples/tables.js @@ -161,6 +161,38 @@ var docDefinition = { ] } }, + { text: 'Table alignment', pageBreak: 'before', style: 'subheader' }, + 'via tableAlignment property', + { + style: 'tableExample', + tableAlignment: 'left', + table: { + body: [ + ['Column 1', 'Column 2', 'Column 3'], + ['One value goes here', 'Another one here', 'OK?'] + ] + } + }, + { + style: 'tableExample', + tableAlignment: 'center', + table: { + body: [ + ['Column 1', 'Column 2', 'Column 3'], + ['One value goes here', 'Another one here', 'OK?'] + ] + } + }, + { + style: 'tableExample', + tableAlignment: 'right', + table: { + body: [ + ['Column 1', 'Column 2', 'Column 3'], + ['One value goes here', 'Another one here', 'OK?'] + ] + } + }, { text: 'Headers', pageBreak: 'before', style: 'subheader' }, 'You can declare how many rows should be treated as a header. Headers are automatically repeated on the following pages', { text: ['It is also possible to set keepWithHeaderRows to make sure there will be no page-break between the header and these rows. Take a look at the document-definition and play with it. If you set it to one, the following table will automatically start on the next page, since there\'s not enough space for the first row to be rendered here'], color: 'gray', italics: true }, @@ -300,9 +332,9 @@ var docDefinition = { body: [ ['Sample value 1', 'Sample value 2', 'Sample value 3'], [ - {text: 'Sample value 1',fillOpacity:0.15,fillColor:'blue'}, - {text: 'Sample value 2',fillOpacity:0.60,fillColor:'blue'}, - {text: 'Sample value 3',fillOpacity:0.85,fillColor:'blue'}, + { text: 'Sample value 1', fillOpacity: 0.15, fillColor: 'blue' }, + { text: 'Sample value 2', fillOpacity: 0.60, fillColor: 'blue' }, + { text: 'Sample value 3', fillOpacity: 0.85, fillColor: 'blue' }, ], [ {text: 'Sample value 1', fillOpacity: 0.15, fillColor: ['stripe45d', 'blue']}, @@ -325,9 +357,9 @@ var docDefinition = { body: [ ['Sample value 1', 'Sample value 2', 'Sample value 3'], [ - {text: 'Sample value 1',fillOpacity:0.15}, - {text: 'Sample value 2',fillOpacity:0.60}, - {text: 'Sample value 3',fillOpacity:0.85}, + { text: 'Sample value 1', fillOpacity: 0.15 }, + { text: 'Sample value 2', fillOpacity: 0.60 }, + { text: 'Sample value 3', fillOpacity: 0.85 }, ], ['Sample value 1', 'Sample value 2', 'Sample value 3'] ] @@ -351,7 +383,7 @@ var docDefinition = { layout: { fillColor: 'blue', fillOpacity: function (rowIndex, node, columnIndex) { - return (rowIndex/8+columnIndex/3); + return (rowIndex / 8 + columnIndex / 3); } } }, diff --git a/src/DocPreprocessor.js b/src/DocPreprocessor.js index 6002555b3..0135de60d 100644 --- a/src/DocPreprocessor.js +++ b/src/DocPreprocessor.js @@ -115,6 +115,30 @@ class DocPreprocessor { } } + // table alignment on page + switch (node.tableAlignment) { + case 'right': + node.width = 'auto'; + node = { + columns: [ + { width: '*', text: '' }, + node + ] + }; + break; + + case 'center': + node.width = 'auto'; + node = { + columns: [ + { width: '*', text: '' }, + node, + { width: '*', text: '' } + ] + }; + break; + } + return node; } diff --git a/tests/integration/tables.spec.js b/tests/integration/tables.spec.js index 7a2553af3..62f2988d4 100644 --- a/tests/integration/tables.spec.js +++ b/tests/integration/tables.spec.js @@ -288,4 +288,51 @@ describe('Integration test: tables', function () { assert.deepEqual(getColumnText(lines, { cell: 1 }), 'Row 2'); }); + + it('renders a simple table with right alignment', function () { + var dd = { + content: + { + tableAlignment: 'right', + table: { + body: [ + ['Column 1', 'Column 2'], + ['Value 1', 'Value 2'] + ] + }, + } + }; + + var pages = testHelper.renderPages('A6', dd); + var lines = getCells(pages, { pageNumber: 0 }); + + const availableWidth = sizes.A6[0] - testHelper.MARGINS.right; + const tableWidth = TABLE_PADDING_X * 3 + TABLE_BORDER_STRENGTH * 2 + lines[0].item.maxWidth * 2; + const startXAligned = availableWidth - tableWidth; + + assert.equal(lines[0].item.x, startXAligned); + }); + + it('renders a simple table with center alignment', function () { + var dd = { + content: { + tableAlignment: 'center', + table: { + body: [ + ['Column 1', 'Column 2'], + ['Value 1', 'Value 2'] + ] + } + } + }; + + var pages = testHelper.renderPages('A6', dd); + var lines = getCells(pages, { pageNumber: 0 }); + + const tableWidth = TABLE_PADDING_X * 2 + TABLE_BORDER_STRENGTH + lines[0].item.maxWidth * 2; + const startXAligned = (sizes.A6[0] - tableWidth) / 2; + + assert.equal(lines[0].item.x, startXAligned); + }); + });