Comparing version 0.1.14 to 0.1.17
{ | ||
"name": "pdfmake", | ||
"version": "0.1.14", | ||
"version": "0.1.17", | ||
"homepage": "https://bpampuch.github.io/pdfmake", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -55,5 +55,9 @@ module.exports = function(grunt) { | ||
reporter: '<%= (grunt.option("cc") ? "html-cov" : "spec") %>', | ||
}, | ||
src: ['tests/**/*.js'], | ||
} | ||
} | ||
}, | ||
options: { | ||
files: 'tests', | ||
recursive: true, | ||
'check-leaks': true | ||
} | ||
}, | ||
@@ -77,8 +81,10 @@ | ||
build: { | ||
options: { | ||
standalone: 'pdfMake', | ||
alias: './src/browser-extensions/virtual-fs.js:fs' | ||
}, | ||
files: { | ||
'build/pdfmake.js': ['./src/browser-extensions/pdfMake.js'] | ||
}, | ||
options: { | ||
require: ['./src/browser-extensions/virtual-fs.js:fs', './src/browser-extensions/pdfmake.js:pdfMake'], | ||
browserifyOptions: { | ||
standalone: 'pdfMake', | ||
} | ||
} | ||
@@ -129,3 +135,9 @@ } | ||
grunt.registerTask('buildFonts', [ 'dump_dir' ]); | ||
grunt.registerTask('fixVfsFonts', 'Adds semicolon to the end of vfs_fonts.js', function () { | ||
var file = grunt.file.read('build/vfs_fonts.js'); | ||
file += ";"; | ||
grunt.file.write('build/vfs_fonts.js', file); | ||
}); | ||
grunt.registerTask('buildFonts', [ 'dump_dir', 'fixVfsFonts' ]); | ||
grunt.registerTask('build', [ 'replace:fixPdfKit', 'browserify', 'uglify', 'buildFonts' ]); | ||
@@ -132,0 +144,0 @@ |
{ | ||
"name": "pdfmake", | ||
"version": "0.1.14", | ||
"version": "0.1.17", | ||
"description": "Client/server side PDF printing in pure JavaScript", | ||
@@ -10,15 +10,18 @@ "main": "src/printer.js", | ||
"dependencies": { | ||
"pdfkit": "~0.7.0" | ||
"pdfkit": "~0.7.0", | ||
"lodash": "~3.1.0", | ||
"lodash-node": "~3.1.0" | ||
}, | ||
"devDependencies": { | ||
"grunt": "~0.4.2", | ||
"grunt-cli": "~0.1.11", | ||
"grunt-jsdoc": "~0.5.1", | ||
"grunt-contrib-jshint": "~0.8.0", | ||
"grunt": "~0.4.5", | ||
"grunt-cli": "~0.1.13", | ||
"grunt-jsdoc": "~0.5.8", | ||
"grunt-contrib-jshint": "~0.11.0", | ||
"grunt-mocha-cov": "~0.2.1", | ||
"grunt-text-replace": "~0.3.11", | ||
"grunt-browserify": "~1.3.1", | ||
"grunt-contrib-uglify": "~0.4.0", | ||
"grunt-text-replace": "~0.4.0", | ||
"grunt-browserify": "~3.5.0", | ||
"grunt-contrib-uglify": "~0.8.0", | ||
"grunt-dump-dir": "~0.1.2", | ||
"grunt-contrib-concat": "~0.3.0" | ||
"grunt-contrib-concat": "~0.5.1", | ||
"sinon": "~1.12.2" | ||
}, | ||
@@ -25,0 +28,0 @@ "scripts": { |
@@ -32,4 +32,2 @@ pdfmake [![Build Status](https://travis-ci.org/bpampuch/pdfmake.png?branch=master)](https://travis-ci.org/bpampuch/pdfmake) [![NPM version](https://badge.fury.io/js/pdfmake.png)](http://badge.fury.io/js/pdfmake) [![Bower version](https://badge.fury.io/bo/pdfmake.png)](http://badge.fury.io/bo/pdfmake) | ||
*warning! links to the documentation do NOT work yet* | ||
This document will walk you through the basics of pdfmake and will show you how to create PDF files in the browser. If you're interested in server-side printing check the examples folder. | ||
@@ -40,3 +38,3 @@ | ||
* **pdfmake.min.js**, | ||
* **vfs_fonts.js** - default font definition (it contains Roboto, you can however [use custom fonts instead](CustomFonts)) | ||
* **vfs_fonts.js** - default font definition (it contains Roboto, you can however [use custom fonts instead](https://github.com/bpampuch/pdfmake/wiki/Custom-Fonts---client-side)) | ||
@@ -166,2 +164,7 @@ ```html | ||
text: 'Third column' | ||
}, | ||
{ | ||
// % width | ||
width: '20%', | ||
text: 'Fourth column' | ||
} | ||
@@ -439,3 +442,16 @@ ], | ||
To change page orientation within a document, add a page break with the new page orientation. | ||
```js | ||
{ | ||
pageOrientation: 'portrait', | ||
content: [ | ||
{text: 'Text on Portrait'}, | ||
{text: 'Text on Landscape', pageOrientation: 'landscape', pageBreak: 'before'}, | ||
{text: 'Text on Landscape 2', pageOrientation: 'portrait', pageBreak: 'after'}, | ||
{text: 'Text on Portrait 2'}, | ||
] | ||
} | ||
``` | ||
## Coming soon | ||
@@ -460,2 +476,1 @@ Hmmm... let me know what you need ;) | ||
big thanks to @yelouafi for making this library even better | ||
@@ -10,3 +10,4 @@ /* jslint node: true */ | ||
starMaxMax = 0, | ||
fixedColumns = []; | ||
fixedColumns = [], | ||
initial_availableWidth = availableWidth; | ||
@@ -28,2 +29,6 @@ columns.forEach(function(column) { | ||
fixedColumns.forEach(function(col) { | ||
// width specified as % | ||
if (typeof col.width === 'string' && /\d+%/.test(col.width) ) { | ||
col.width = parseFloat(col.width)*initial_availableWidth/100; | ||
} | ||
if (col.width < (col._minWidth) && col.elasticWidth) { | ||
@@ -30,0 +35,0 @@ col._calcWidth = col._minWidth; |
@@ -82,28 +82,66 @@ /* jslint node: true */ | ||
function getNodeMargin() { | ||
var margin = node.margin; | ||
if (!margin && node.style) { | ||
var styleArray = (node.style instanceof Array) ? node.style : [ node.style ]; | ||
function processSingleMargins(node, currentMargin){ | ||
if (node.marginLeft || node.marginTop || node.marginRight || node.marginBottom) { | ||
return [ | ||
node.marginLeft || currentMargin[0] || 0, | ||
node.marginTop || currentMargin[1] || 0, | ||
node.marginRight || currentMargin[2] || 0, | ||
node.marginBottom || currentMargin[3] || 0 | ||
]; | ||
} | ||
return currentMargin; | ||
} | ||
for(var i = styleArray.length - 1; i >= 0; i--) { | ||
function flattenStyleArray(styleArray){ | ||
var flattenedStyles = {}; | ||
for (var i = styleArray.length - 1; i >= 0; i--) { | ||
var styleName = styleArray[i]; | ||
var style = self.styleStack.styleDictionary[styleName]; | ||
if (style && style.margin) { | ||
margin = style.margin; | ||
break; | ||
for(var key in style){ | ||
if(style.hasOwnProperty(key)){ | ||
flattenedStyles[key] = style[key]; | ||
} | ||
} | ||
} | ||
return flattenedStyles; | ||
} | ||
if (!margin) return null; | ||
function convertMargin(margin) { | ||
if (typeof margin === 'number' || margin instanceof Number) { | ||
margin = [ margin, margin, margin, margin ]; | ||
} else if (margin instanceof Array) { | ||
if (margin.length === 2) { | ||
margin = [ margin[0], margin[1], margin[0], margin[1] ]; | ||
} | ||
} | ||
return margin; | ||
} | ||
if (typeof margin === 'number' || margin instanceof Number) { | ||
margin = [ margin, margin, margin, margin ]; | ||
} else if (margin instanceof Array) { | ||
if (margin.length === 2) { | ||
margin = [ margin[0], margin[1], margin[0], margin[1] ]; | ||
var margin = [undefined, undefined, undefined, undefined]; | ||
if(node.style) { | ||
var styleArray = (node.style instanceof Array) ? node.style : [node.style]; | ||
var flattenedStyleArray = flattenStyleArray(styleArray); | ||
if(flattenedStyleArray) { | ||
margin = processSingleMargins(flattenedStyleArray, margin); | ||
} | ||
if(flattenedStyleArray.margin){ | ||
margin = convertMargin(flattenedStyleArray.margin); | ||
} | ||
} | ||
margin = processSingleMargins(node, margin); | ||
return margin; | ||
if(node.margin){ | ||
margin = convertMargin(node.margin); | ||
} | ||
if(margin[0] === undefined && margin[1] === undefined && margin[2] === undefined && margin[3] === undefined) { | ||
return null; | ||
} else { | ||
return margin; | ||
} | ||
} | ||
@@ -184,7 +222,6 @@ }; | ||
var radius = gapSize.fontSize / 6; | ||
marker = { | ||
canvas: [ { | ||
x: radius, | ||
y: gapSize.height + gapSize.decender - gapSize.fontSize / 3,//0,// gapSize.fontSize * 2 / 3, | ||
y: (gapSize.height / gapSize.lineHeight) + gapSize.decender - gapSize.fontSize / 3,//0,// gapSize.fontSize * 2 / 3, | ||
r1: radius, | ||
@@ -191,0 +228,0 @@ r2: radius, |
@@ -13,3 +13,2 @@ /* jslint node: true */ | ||
this.pageSize = pageSize; | ||
this.pageMargins = pageMargins; | ||
@@ -25,8 +24,6 @@ | ||
this.endingCell = null; | ||
this.defaultPage = { items: [] }; | ||
this.tracker = new TraversalTracker(); | ||
this.tracker = new TraversalTracker(); | ||
this.addPage(); | ||
this.addPage(pageSize); | ||
} | ||
@@ -119,14 +116,77 @@ | ||
DocumentContext.prototype.moveToPageTop = function() { | ||
DocumentContext.prototype.initializePage = function() { | ||
this.y = this.pageMargins.top; | ||
this.availableHeight = this.pageSize.height - this.pageMargins.top - this.pageMargins.bottom; | ||
this.availableHeight = this.getCurrentPage().pageSize.height - this.pageMargins.top - this.pageMargins.bottom; | ||
this.pageSnapshot().availableWidth = this.getCurrentPage().pageSize.width - this.pageMargins.left - this.pageMargins.right; | ||
}; | ||
DocumentContext.prototype.addPage = function() { | ||
var page = this.getDefaultPage(); | ||
this.pages.push(page); | ||
DocumentContext.prototype.pageSnapshot = function(){ | ||
if(this.snapshots[0]){ | ||
return this.snapshots[0]; | ||
} else { | ||
return this; | ||
} | ||
}; | ||
function pageOrientation(pageOrientationString, currentPageOrientation){ | ||
if(pageOrientationString === undefined) { | ||
return currentPageOrientation; | ||
} else if(pageOrientationString === 'landscape'){ | ||
return 'landscape'; | ||
} else { | ||
return 'portrait'; | ||
} | ||
} | ||
var getPageSize = function (currentPage, newPageOrientation) { | ||
newPageOrientation = pageOrientation(newPageOrientation, currentPage.pageSize.orientation); | ||
if(newPageOrientation !== currentPage.pageSize.orientation) { | ||
return { | ||
orientation: newPageOrientation, | ||
width: currentPage.pageSize.height, | ||
height: currentPage.pageSize.width | ||
}; | ||
} else { | ||
return { | ||
orientation: currentPage.pageSize.orientation, | ||
width: currentPage.pageSize.width, | ||
height: currentPage.pageSize.height | ||
}; | ||
} | ||
}; | ||
DocumentContext.prototype.moveToNextPage = function(pageOrientation) { | ||
var nextPageIndex = this.page + 1; | ||
var prevPage = this.page; | ||
var prevY = this.y; | ||
var createNewPage = nextPageIndex >= this.pages.length; | ||
if (createNewPage) { | ||
this.addPage(getPageSize(this.getCurrentPage(), pageOrientation)); | ||
} else { | ||
this.page = nextPageIndex; | ||
this.initializePage(); | ||
} | ||
return { | ||
newPageCreated: createNewPage, | ||
prevPage: prevPage, | ||
prevY: prevY, | ||
y: this.y | ||
}; | ||
}; | ||
DocumentContext.prototype.addPage = function(pageSize) { | ||
var page = { items: [], pageSize: pageSize }; | ||
this.pages.push(page); | ||
this.page = this.pages.length - 1; | ||
this.moveToPageTop(); | ||
this.initializePage(); | ||
this.tracker.emit('pageAdded'); | ||
this.tracker.emit('pageAdded'); | ||
@@ -142,9 +202,15 @@ return page; | ||
DocumentContext.prototype.setDefaultPage = function(defaultPage) { | ||
// copy the items without deep-copying the object (which is not possible due to circular structures) | ||
this.defaultPage = { items: (defaultPage || this.pages[this.page]).items.slice() }; | ||
}; | ||
DocumentContext.prototype.getCurrentPosition = function() { | ||
var pageSize = this.getCurrentPage().pageSize; | ||
var innerHeight = pageSize.height - this.pageMargins.top - this.pageMargins.bottom; | ||
var innerWidth = pageSize.width - this.pageMargins.left - this.pageMargins.right; | ||
DocumentContext.prototype.getDefaultPage = function() { | ||
return { items: this.defaultPage.items.slice() }; | ||
return { | ||
pageNumber: this.page + 1, | ||
pageOrientation: pageSize.orientation, | ||
left: this.x, | ||
top: this.y, | ||
verticalRatio: ((this.y - this.pageMargins.top) / innerHeight), | ||
horizontalRatio: ((this.x - this.pageMargins.left) / innerWidth) | ||
}; | ||
}; | ||
@@ -151,0 +217,0 @@ |
@@ -16,3 +16,3 @@ /* jslint node: true */ | ||
this.contextStack = []; | ||
this.tracker = tracker || { emit: function() { } }; | ||
this.tracker = tracker; | ||
} | ||
@@ -31,3 +31,4 @@ | ||
var context = this.context; | ||
var page = context.getCurrentPage(); | ||
var page = context.getCurrentPage(), | ||
position = this.getCurrentPositionOnPage(); | ||
@@ -51,3 +52,3 @@ if (context.availableHeight < height || !page) { | ||
return true; | ||
return position; | ||
}; | ||
@@ -91,3 +92,4 @@ | ||
var context = this.context; | ||
var page = context.getCurrentPage(); | ||
var page = context.getCurrentPage(), | ||
position = this.getCurrentPositionOnPage(); | ||
@@ -110,3 +112,3 @@ if (context.availableHeight < image._height || !page) { | ||
return true; | ||
return position; | ||
}; | ||
@@ -116,3 +118,4 @@ | ||
var context = this.context; | ||
var page = context.getCurrentPage(); | ||
var page = context.getCurrentPage(), | ||
position = this.getCurrentPositionOnPage(); | ||
@@ -125,5 +128,5 @@ if (context.availableHeight < qr._height || !page) { | ||
qr.y = context.y; | ||
this.alignImage(qr); | ||
for (var i=0, l=qr._canvas.length; i < l; i++) { | ||
@@ -138,3 +141,3 @@ var vector = qr._canvas[i]; | ||
return true; | ||
return position; | ||
}; | ||
@@ -162,3 +165,4 @@ | ||
var context = this.context; | ||
var page = context.getCurrentPage(); | ||
var page = context.getCurrentPage(), | ||
position = this.getCurrentPositionOnPage(); | ||
@@ -171,3 +175,3 @@ if (page) { | ||
}, index); | ||
return true; | ||
return position; | ||
} | ||
@@ -207,3 +211,3 @@ }; | ||
break; | ||
case 'vector': | ||
@@ -218,3 +222,3 @@ var v = pack(item.item); | ||
break; | ||
case 'image': | ||
@@ -248,3 +252,3 @@ var img = pack(item.item); | ||
if (contextOrWidth === undefined) { | ||
height = this.context.pageSize.height - this.context.pageMargins.top - this.context.pageMargins.bottom; | ||
height = this.context.getCurrentPage().height - this.context.pageMargins.top - this.context.pageMargins.bottom; | ||
contextOrWidth = this.context.availableWidth; | ||
@@ -265,3 +269,7 @@ } | ||
ElementWriter.prototype.getCurrentPositionOnPage = function(){ | ||
return (this.contextStack[0] || this.context).getCurrentPosition(); | ||
}; | ||
module.exports = ElementWriter; |
var pdfKit = require('pdfkit'); | ||
var PDFImage = require('../node_modules/pdfkit/js/image'); | ||
var PDFImage = require('pdfkit/js/image'); | ||
@@ -4,0 +4,0 @@ function ImageMeasure(pdfDoc, imageDictionary) { |
/* jslint node: true */ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var TraversalTracker = require('./traversalTracker'); | ||
@@ -18,2 +19,8 @@ var DocMeasure = require('./docMeasure'); | ||
function addAll(target, otherArray){ | ||
_.each(otherArray, function(item){ | ||
target.push(item); | ||
}); | ||
} | ||
/** | ||
@@ -48,5 +55,74 @@ * Creates an instance of LayoutBuilder - layout engine which turns document-definition-object | ||
*/ | ||
LayoutBuilder.prototype.layoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark) { | ||
this.docMeasure = new DocMeasure(fontProvider, styleDictionary, defaultStyle, this.imageMeasure, this.tableLayouts, images); | ||
LayoutBuilder.prototype.layoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) { | ||
function addPageBreaksIfNecessary(linearNodeList, pages) { | ||
linearNodeList = _.reject(linearNodeList, function(node){ | ||
return _.isEmpty(node.positions); | ||
}); | ||
_.each(linearNodeList, function(node) { | ||
var nodeInfo = _.pick(node, ['id', 'headlineLevel', 'text', 'ul', 'ol', 'table', 'image', 'qr', 'canvas', 'columns', 'style', 'pageOrientation']); | ||
nodeInfo.startPosition = _.first(node.positions); | ||
nodeInfo.pageNumbers = _.chain(node.positions).map('pageNumber').uniq().value(); | ||
nodeInfo.pages = pages.length; | ||
nodeInfo.stack = _.isArray(node.stack); | ||
node.nodeInfo = nodeInfo; | ||
}); | ||
return _.any(linearNodeList, function (node, index, followingNodeList) { | ||
if (node.pageBreak !== 'before') { | ||
var pageNumber = _.first(node.nodeInfo.pageNumbers); | ||
var followingNodesOnPage = _.chain(followingNodeList).drop(index + 1).filter(function (node0) { | ||
return _.contains(node0.nodeInfo.pageNumbers, pageNumber); | ||
}).value(); | ||
var nodesOnNextPage = _.chain(followingNodeList).drop(index + 1).filter(function (node0) { | ||
return _.contains(node0.nodeInfo.pageNumbers, pageNumber + 1); | ||
}).value(); | ||
var previousNodesOnPage = _.chain(followingNodeList).take(index).filter(function (node0) { | ||
return _.contains(node0.nodeInfo.pageNumbers, pageNumber); | ||
}).value(); | ||
if (pageBreakBeforeFct(node.nodeInfo, | ||
_.map(followingNodesOnPage, 'nodeInfo'), | ||
_.map(nodesOnNextPage, 'nodeInfo'), | ||
_.map(previousNodesOnPage, 'nodeInfo'))) { | ||
node.pageBreak = 'before'; | ||
return true; | ||
} | ||
} | ||
}); | ||
} | ||
if(!isFunction(pageBreakBeforeFct)){ | ||
pageBreakBeforeFct = function(){ | ||
return false; | ||
}; | ||
} | ||
this.docMeasure = new DocMeasure(fontProvider, styleDictionary, defaultStyle, this.imageMeasure, this.tableLayouts, images); | ||
function resetXYs(result) { | ||
_.each(result.linearNodeList, function (node) { | ||
node.resetXY(); | ||
}); | ||
} | ||
var result = this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark); | ||
while(addPageBreaksIfNecessary(result.linearNodeList, result.pages)){ | ||
resetXYs(result); | ||
result = this.tryLayoutDocument(docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark); | ||
} | ||
return result.pages; | ||
}; | ||
LayoutBuilder.prototype.tryLayoutDocument = function (docStructure, fontProvider, styleDictionary, defaultStyle, background, header, footer, images, watermark, pageBreakBeforeFct) { | ||
this.linearNodeList = []; | ||
docStructure = this.docMeasure.measureDocument(docStructure); | ||
@@ -59,5 +135,5 @@ | ||
this.writer.context().tracker.startTracking('pageAdded', function() { | ||
_this.addBackground(background); | ||
_this.addBackground(background); | ||
}); | ||
this.addBackground(background); | ||
@@ -70,5 +146,6 @@ this.processNode(docStructure); | ||
return this.writer.context().pages; | ||
return {pages: this.writer.context().pages, linearNodeList: this.linearNodeList}; | ||
}; | ||
LayoutBuilder.prototype.addBackground = function(background) { | ||
@@ -80,3 +157,4 @@ var backgroundGetter = isFunction(background) ? background : function() { return background; }; | ||
if (pageBackground) { | ||
this.writer.beginUnbreakableBlock(this.pageSize.width, this.pageSize.height); | ||
var pageSize = this.writer.context().getCurrentPage().pageSize; | ||
this.writer.beginUnbreakableBlock(pageSize.width, pageSize.height); | ||
this.processNode(this.docMeasure.measureDocument(pageBackground)); | ||
@@ -97,3 +175,3 @@ this.writer.commitUnbreakableBlock(0, 0); | ||
this.writer.commitUnbreakableBlock(x, y); | ||
for(var i = 1, l = pages.length; i < l; i++) { | ||
@@ -105,14 +183,15 @@ this.writer.context().page = i; | ||
LayoutBuilder.prototype.addDynamicRepeatable = function(nodeGetter, x, y, width, height) { | ||
LayoutBuilder.prototype.addDynamicRepeatable = function(nodeGetter, sizeFunction) { | ||
var pages = this.writer.context().pages; | ||
for(var i = 0, l = pages.length; i < l; i++) { | ||
this.writer.context().page = i; | ||
var node = nodeGetter(i + 1, l); | ||
for(var pageIndex = 0, l = pages.length; pageIndex < l; pageIndex++) { | ||
this.writer.context().page = pageIndex; | ||
var node = nodeGetter(pageIndex + 1, l); | ||
if (node) { | ||
this.writer.beginUnbreakableBlock(width, height); | ||
var sizes = sizeFunction(this.writer.context().getCurrentPage().pageSize, this.pageMargins); | ||
this.writer.beginUnbreakableBlock(sizes.width, sizes.height); | ||
this.processNode(this.docMeasure.measureDocument(node)); | ||
this.writer.commitUnbreakableBlock(x, y); | ||
this.writer.commitUnbreakableBlock(sizes.x, sizes.y); | ||
} | ||
@@ -123,12 +202,30 @@ } | ||
LayoutBuilder.prototype.addHeadersAndFooters = function(header, footer) { | ||
var headerSizeFct = function(pageSize, pageMargins){ | ||
return { | ||
x: 0, | ||
y: 0, | ||
width: pageSize.width, | ||
height: pageMargins.top | ||
}; | ||
}; | ||
var footerSizeFct = function (pageSize, pageMargins) { | ||
return { | ||
x: 0, | ||
y: pageSize.height - pageMargins.bottom, | ||
width: pageSize.width, | ||
height: pageMargins.bottom | ||
}; | ||
}; | ||
if(isFunction(header)) { | ||
this.addDynamicRepeatable(header, 0, 0, this.pageSize.width, this.pageMargins.top); | ||
this.addDynamicRepeatable(header, headerSizeFct); | ||
} else if(header) { | ||
this.addStaticRepeatable(header, 0, 0, this.pageSize.width, this.pageMargins.top); | ||
this.addStaticRepeatable(header, headerSizeFct); | ||
} | ||
if(isFunction(footer)) { | ||
this.addDynamicRepeatable(footer, 0, this.pageSize.height - this.pageMargins.bottom, this.pageSize.width, this.pageMargins.bottom); | ||
this.addDynamicRepeatable(footer, footerSizeFct); | ||
} else if(footer) { | ||
this.addStaticRepeatable(footer, 0, this.pageSize.height - this.pageMargins.bottom, this.pageSize.width, this.pageMargins.bottom); | ||
this.addStaticRepeatable(footer, headerSizeFct); | ||
} | ||
@@ -188,14 +285,26 @@ }; | ||
function decorateNode(node){ | ||
var x = node.x, y = node.y; | ||
node.positions = []; | ||
node.resetXY = function(){ | ||
node.x = x; | ||
node.y = y; | ||
}; | ||
} | ||
LayoutBuilder.prototype.processNode = function(node) { | ||
var self = this; | ||
this.linearNodeList.push(node); | ||
decorateNode(node); | ||
applyMargins(function() { | ||
if (node.stack) { | ||
self.processVerticalContainer(node.stack); | ||
self.processVerticalContainer(node); | ||
} else if (node.columns) { | ||
self.processColumns(node); | ||
} else if (node.ul) { | ||
self.processList(false, node.ul, node._gapSize); | ||
self.processList(false, node); | ||
} else if (node.ol) { | ||
self.processList(true, node.ol, node._gapSize); | ||
self.processList(true, node); | ||
} else if (node.table) { | ||
@@ -220,3 +329,3 @@ self.processTable(node); | ||
if (node.pageBreak === 'before') { | ||
self.writer.moveToNextPage(); | ||
self.writer.moveToNextPage(node.pageOrientation); | ||
} | ||
@@ -237,3 +346,3 @@ | ||
if (node.pageBreak === 'after') { | ||
self.writer.moveToNextPage(); | ||
self.writer.moveToNextPage(node.pageOrientation); | ||
} | ||
@@ -244,6 +353,7 @@ } | ||
// vertical container | ||
LayoutBuilder.prototype.processVerticalContainer = function(items) { | ||
LayoutBuilder.prototype.processVerticalContainer = function(node) { | ||
var self = this; | ||
items.forEach(function(item) { | ||
node.stack.forEach(function(item) { | ||
self.processNode(item); | ||
addAll(node.positions, item.positions); | ||
@@ -263,3 +373,4 @@ //TODO: paragraph gap | ||
ColumnCalculator.buildColumnWidths(columns, availableWidth); | ||
this.processRow(columns, columns, gaps); | ||
var result = this.processRow(columns, columns, gaps); | ||
addAll(columnNode.positions, result.positions); | ||
@@ -283,3 +394,3 @@ | ||
var self = this; | ||
var pageBreaks = []; | ||
var pageBreaks = [], positions = []; | ||
@@ -305,2 +416,3 @@ this.tracker.auto('pageChanged', storePageBreakData, function() { | ||
self.processNode(column); | ||
addAll(positions, column.positions); | ||
} else if (column._columnEndingContext) { | ||
@@ -315,3 +427,3 @@ // row-span ending | ||
return pageBreaks; | ||
return {pageBreaks: pageBreaks, positions: positions}; | ||
@@ -354,4 +466,6 @@ function storePageBreakData(data) { | ||
// lists | ||
LayoutBuilder.prototype.processList = function(orderedList, items, gapSize) { | ||
var self = this; | ||
LayoutBuilder.prototype.processList = function(orderedList, node) { | ||
var self = this, | ||
items = orderedList ? node.ol : node.ul, | ||
gapSize = node._gapSize; | ||
@@ -365,2 +479,3 @@ this.writer.context().addMargin(gapSize.width); | ||
self.processNode(item); | ||
addAll(node.positions, item.positions); | ||
}); | ||
@@ -403,5 +518,6 @@ }); | ||
var pageBreaks = this.processRow(tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i); | ||
var result = this.processRow(tableNode.table.body[i], tableNode.table.widths, tableNode._offsets.offsets, tableNode.table.body, i); | ||
addAll(tableNode.positions, result.positions); | ||
processor.endRow(i, this.writer, pageBreaks); | ||
processor.endRow(i, this.writer, result.pageBreaks); | ||
} | ||
@@ -417,3 +533,4 @@ | ||
while (line) { | ||
this.writer.addLine(line); | ||
var positions = this.writer.addLine(line); | ||
node.positions.push(positions); | ||
line = this.buildNextLine(node); | ||
@@ -438,3 +555,4 @@ } | ||
LayoutBuilder.prototype.processImage = function(node) { | ||
this.writer.addImage(node); | ||
var position = this.writer.addImage(node); | ||
node.positions.push(position); | ||
}; | ||
@@ -453,3 +571,4 @@ | ||
node.canvas.forEach(function(vector) { | ||
this.writer.addVector(vector); | ||
var position = this.writer.addVector(vector); | ||
node.positions.push(position); | ||
}, this); | ||
@@ -461,3 +580,4 @@ | ||
LayoutBuilder.prototype.processQr = function(node) { | ||
this.writer.addQr(node); | ||
var position = this.writer.addQr(node); | ||
node.positions.push(position); | ||
}; | ||
@@ -464,0 +584,0 @@ |
@@ -22,25 +22,31 @@ /* jslint node: true */ | ||
function fitOnPage(self, addFct){ | ||
var position = addFct(self); | ||
if (!position) { | ||
self.moveToNextPage(); | ||
position = addFct(self); | ||
} | ||
return position; | ||
} | ||
PageElementWriter.prototype.addLine = function(line, dontUpdateContextPosition, index) { | ||
if (!this.writer.addLine(line, dontUpdateContextPosition, index)) { | ||
this.moveToNextPage(); | ||
this.writer.addLine(line, dontUpdateContextPosition, index); | ||
} | ||
return fitOnPage(this, function(self){ | ||
return self.writer.addLine(line, dontUpdateContextPosition, index); | ||
}); | ||
}; | ||
PageElementWriter.prototype.addImage = function(image, index) { | ||
if(!this.writer.addImage(image, index)) { | ||
this.moveToNextPage(); | ||
this.writer.addImage(image, index); | ||
} | ||
return fitOnPage(this, function(self){ | ||
return self.writer.addImage(image, index); | ||
}); | ||
}; | ||
PageElementWriter.prototype.addQr = function(qr, index) { | ||
if(!this.writer.addQr(qr, index)) { | ||
this.moveToNextPage(); | ||
this.writer.addQr(qr, index); | ||
} | ||
return fitOnPage(this, function(self){ | ||
return self.writer.addQr(qr, index); | ||
}); | ||
}; | ||
PageElementWriter.prototype.addVector = function(vector, ignoreContextX, ignoreContextY, index) { | ||
this.writer.addVector(vector, ignoreContextX, ignoreContextY, index); | ||
return this.writer.addVector(vector, ignoreContextX, ignoreContextY, index); | ||
}; | ||
@@ -55,13 +61,7 @@ | ||
PageElementWriter.prototype.moveToNextPage = function() { | ||
var nextPageIndex = this.writer.context.page + 1; | ||
var prevPage = this.writer.context.page; | ||
var prevY = this.writer.context.y; | ||
if (nextPageIndex >= this.writer.context.pages.length) { | ||
// create new Page | ||
this.writer.context.addPage(); | ||
// add repeatable fragments | ||
PageElementWriter.prototype.moveToNextPage = function(pageOrientation) { | ||
var nextPage = this.writer.context.moveToNextPage(pageOrientation); | ||
if (nextPage.newPageCreated) { | ||
this.repeatables.forEach(function(rep) { | ||
@@ -71,5 +71,2 @@ this.writer.addFragment(rep, true); | ||
} else { | ||
this.writer.context.page = nextPageIndex; | ||
this.writer.context.moveToPageTop(); | ||
this.repeatables.forEach(function(rep) { | ||
@@ -81,5 +78,5 @@ this.writer.context.moveDown(rep.height); | ||
this.writer.tracker.emit('pageChanged', { | ||
prevPage: prevPage, | ||
prevY: prevY, | ||
y: this.writer.context.y | ||
prevPage: nextPage.prevPage, | ||
prevY: nextPage.prevY, | ||
y: nextPage.y | ||
}); | ||
@@ -111,5 +108,5 @@ }; | ||
if (forcedX !== undefined || forcedY !== undefined) { | ||
fragment.height = unbreakableContext.pageSize.height - unbreakableContext.pageMargins.top - unbreakableContext.pageMargins.bottom; | ||
fragment.height = unbreakableContext.getCurrentPage().pageSize.height - unbreakableContext.pageMargins.top - unbreakableContext.pageMargins.bottom; | ||
} else { | ||
fragment.height = this.writer.context.pageSize.height - this.writer.context.pageMargins.top - this.writer.context.pageMargins.bottom; | ||
fragment.height = this.writer.context.getCurrentPage().pageSize.height - this.writer.context.pageMargins.top - this.writer.context.pageMargins.bottom; | ||
for (var i = 0, l = this.repeatables.length; i < l; i++) { | ||
@@ -120,3 +117,3 @@ fragment.height -= this.repeatables[i].height; | ||
} else { | ||
fragment.height = unbreakableContext.y; | ||
fragment.height = unbreakableContext.y; | ||
} | ||
@@ -123,0 +120,0 @@ |
@@ -7,3 +7,3 @@ /* jslint node: true */ | ||
var PdfKit = require('pdfkit'); | ||
var PDFReference = require('../node_modules/pdfkit/js/reference'); | ||
var PDFReference = require('pdfkit/js/reference'); | ||
var sizes = require('./standardPageSizes'); | ||
@@ -79,4 +79,5 @@ var ImageMeasure = require('./imageMeasure'); | ||
if(docDefinition.pageOrientation === 'landscape') { | ||
pageSize = { width: pageSize.height, height: pageSize.width }; | ||
pageSize = { width: pageSize.height, height: pageSize.width}; | ||
} | ||
pageSize.orientation = docDefinition.pageOrientation === 'landscape' ? docDefinition.pageOrientation : 'portrait'; | ||
@@ -100,3 +101,3 @@ this.pdfKitDoc = new PdfKit({ size: [ pageSize.width, pageSize.height ], compress: false}); | ||
var pages = builder.layoutDocument(docDefinition.content, this.fontProvider, docDefinition.styles || {}, docDefinition.defaultStyle || { fontSize: 12, font: 'Roboto' }, docDefinition.background, docDefinition.header, docDefinition.footer, docDefinition.images, docDefinition.watermark); | ||
var pages = builder.layoutDocument(docDefinition.content, this.fontProvider, docDefinition.styles || {}, docDefinition.defaultStyle || { fontSize: 12, font: 'Roboto' }, docDefinition.background, docDefinition.header, docDefinition.footer, docDefinition.images, docDefinition.watermark, docDefinition.pageBreakBefore); | ||
@@ -206,6 +207,18 @@ renderPages(pages, this.fontProvider, this.pdfKitDoc); | ||
function updatePageOrientationInOptions(currentPage, pdfKitDoc) { | ||
var previousPageOrientation = pdfKitDoc.options.size[0] > pdfKitDoc.options.size[1] ? 'landscape' : 'portrait'; | ||
if(currentPage.pageSize.orientation !== previousPageOrientation) { | ||
var width = pdfKitDoc.options.size[0]; | ||
var height = pdfKitDoc.options.size[1]; | ||
pdfKitDoc.options.size = [height, width]; | ||
} | ||
} | ||
function renderPages(pages, fontProvider, pdfKitDoc) { | ||
for(var i = 0, l = pages.length; i < l; i++) { | ||
pdfKitDoc._pdfMakePages = pages; | ||
for (var i = 0; i < pages.length; i++) { | ||
if (i > 0) { | ||
pdfKitDoc.addPage(); | ||
updatePageOrientationInOptions(pages[i], pdfKitDoc); | ||
pdfKitDoc.addPage(pdfKitDoc.options); | ||
} | ||
@@ -417,3 +430,3 @@ | ||
var fontCache = (this.cache[familyName] = this.cache[familyName] || {}); | ||
fontCache[type] = this.pdfDoc.font(this.fonts[familyName][type])._font; | ||
fontCache[type] = this.pdfDoc.font(this.fonts[familyName][type], familyName + ' (' + type + ')')._font; | ||
return fontCache[type]; | ||
@@ -420,0 +433,0 @@ }; |
@@ -96,3 +96,4 @@ /* jslint node: true */ | ||
'decorationColor', | ||
'background' | ||
'background', | ||
'lineHeight' | ||
//'tableCellPadding' | ||
@@ -99,0 +100,0 @@ // 'cellBorder', |
@@ -21,2 +21,3 @@ var ColumnCalculator = require('./columnCalculator'); | ||
this.rowSpanData = prepareRowSpanData(); | ||
this.cleanUpRepeatables = false; | ||
@@ -142,3 +143,3 @@ this.headerRows = tableNode.table.headerRows || 0; | ||
lineWidth: width, | ||
lineColor: typeof this.layout.vlineColor === 'function' ? this.layout.vLineColor(vLineIndex, this.tableNode) : this.layout.vLineColor | ||
lineColor: typeof this.layout.vLineColor === 'function' ? this.layout.vLineColor(vLineIndex, this.tableNode) : this.layout.vLineColor | ||
}, false, true); | ||
@@ -148,7 +149,9 @@ }; | ||
TableProcessor.prototype.endTable = function(writer) { | ||
writer.popFromRepeatables(); | ||
if (this.cleanUpRepeatables) { | ||
writer.popFromRepeatables(); | ||
} | ||
}; | ||
TableProcessor.prototype.endRow = function(rowIndex, writer, pageBreaks) { | ||
var i; | ||
var l, i; | ||
var self = this; | ||
@@ -270,2 +273,3 @@ writer.tracker.stopTracking('pageChanged', this.rowCallback); | ||
writer.pushToRepeatables(this.headerRepeatable); | ||
this.cleanUpRepeatables = true; | ||
this.headerRepeatable = null; | ||
@@ -272,0 +276,0 @@ } |
@@ -76,2 +76,3 @@ /* jslint node: true */ | ||
var italics = getStyleProperty({}, styleContextStack, 'italics', false); | ||
var lineHeight = getStyleProperty({}, styleContextStack, 'lineHeight', 1); | ||
@@ -82,4 +83,5 @@ var font = this.fontProvider.provideFont(fontName, bold, italics); | ||
width: font.widthOfString(removeDiacritics(text), fontSize), | ||
height: font.lineHeight(fontSize), | ||
height: font.lineHeight(fontSize) * lineHeight, | ||
fontSize: fontSize, | ||
lineHeight: lineHeight, | ||
ascender: font.ascender / 1000 * fontSize, | ||
@@ -215,2 +217,3 @@ decender: font.decender / 1000 * fontSize | ||
var background = getStyleProperty(item, styleContextStack, 'background', null); | ||
var lineHeight = getStyleProperty(item, styleContextStack, 'lineHeight', 1); | ||
@@ -221,3 +224,3 @@ var font = fontProvider.provideFont(fontName, bold, italics); | ||
item.width = font.widthOfString(removeDiacritics(item.text), fontSize); | ||
item.height = font.lineHeight(fontSize); | ||
item.height = font.lineHeight(fontSize) * lineHeight; | ||
@@ -224,0 +227,0 @@ var leadingSpaces = item.text.match(LEADING); |
Sorry, the diff of this file is not supported yet
149950
27
3880
472
3
11
+ Addedlodash@~3.1.0
+ Addedlodash-node@~3.1.0
+ Addedlodash@3.1.0(transitive)
+ Addedlodash-node@3.1.0(transitive)