|
| 1 | +var pdfmake = require('../js/index'); // only during development, otherwise use the following line |
| 2 | +//var pdfmake = require('pdfmake'); |
| 3 | + |
| 4 | +var Roboto = require('../fonts/Roboto'); |
| 5 | +pdfmake.addFonts(Roboto); |
| 6 | + |
| 7 | +pdfmake.setUrlAccessPolicy((url) => { |
| 8 | + // this can be used to restrict allowed domains |
| 9 | + return url.startsWith('https://'); |
| 10 | +}); |
| 11 | + |
| 12 | +var loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. '; |
| 13 | + |
| 14 | +var docDefinition = { |
| 15 | + pageMargins: function(currentPage, pageCount, pageSize) { |
| 16 | + return { |
| 17 | + left: (currentPage % 2 === 0) ? 80 : 40, |
| 18 | + top: 40, |
| 19 | + right: (currentPage % 2 === 0) ? 40 : 80, |
| 20 | + bottom: 40 |
| 21 | + }; |
| 22 | + }, |
| 23 | + content: [ |
| 24 | + { text: loremIpsum.repeat(42) }, |
| 25 | + '', |
| 26 | + 'Table:', |
| 27 | + { |
| 28 | + table: { |
| 29 | + body: [ |
| 30 | + [{ text: 'Header 1', style: 'tableHeader' }, { text: 'Header 2', style: 'tableHeader' }, { text: 'Header 3', style: 'tableHeader' }], |
| 31 | + [ |
| 32 | + loremIpsum.repeat(4), |
| 33 | + loremIpsum.repeat(4), |
| 34 | + loremIpsum.repeat(4), |
| 35 | + ] |
| 36 | + ] |
| 37 | + } |
| 38 | + }, |
| 39 | + '', |
| 40 | + 'Table width headerRows:', |
| 41 | + { |
| 42 | + table: { |
| 43 | + headerRows: 1, |
| 44 | + body: [ |
| 45 | + [{ text: 'Header 1', style: 'tableHeader' }, { text: 'Header 2', style: 'tableHeader' }, { text: 'Header 3', style: 'tableHeader' }], |
| 46 | + [ |
| 47 | + loremIpsum.repeat(6), |
| 48 | + loremIpsum.repeat(6), |
| 49 | + loremIpsum.repeat(6), |
| 50 | + ] |
| 51 | + ] |
| 52 | + } |
| 53 | + } |
| 54 | + ] |
| 55 | +}; |
| 56 | + |
| 57 | +var now = new Date(); |
| 58 | + |
| 59 | +var pdf = pdfmake.createPdf(docDefinition); |
| 60 | +pdf.write('pdfs/dynamicPageMargins.pdf').then(() => { |
| 61 | + console.log(new Date() - now); |
| 62 | +}, err => { |
| 63 | + console.error(err); |
| 64 | +}); |
0 commit comments