123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>plugins/cell.js - Documentation</title>
- <script src="scripts/prettify/prettify.js"></script>
- <script src="scripts/prettify/lang-css.js"></script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link type="text/css" rel="stylesheet" href="styles/prettify.css">
- <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
- </head>
- <body>
- <input type="checkbox" id="nav-trigger" class="nav-trigger" />
- <label for="nav-trigger" class="navicon-button x">
- <div class="navicon"></div>
- </label>
- <label for="nav-trigger" class="overlay"></label>
- <nav>
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="jsPDF.html">jsPDF</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addFont">addFont</a></li><li><a href="global.html#addHTML">addHTML</a></li><li><a href="global.html#addMetadata">addMetadata</a></li><li><a href="global.html#addPage">addPage</a></li><li><a href="global.html#autoPrint">autoPrint</a></li><li><a href="global.html#CapJoinStyles">CapJoinStyles</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#ellipse">ellipse</a></li><li><a href="global.html#getFontList">getFontList</a></li><li><a href="global.html#lines">lines</a></li><li><a href="global.html#lstext">lstext</a></li><li><a href="global.html#output">output</a></li><li><a href="global.html#rect">rect</a></li><li><a href="global.html#roundedRect">roundedRect</a></li><li><a href="global.html#save">save</a></li><li><a href="global.html#setDisplayMode">setDisplayMode</a></li><li><a href="global.html#setDrawColor">setDrawColor</a></li><li><a href="global.html#setFillColor">setFillColor</a></li><li><a href="global.html#setFont">setFont</a></li><li><a href="global.html#setFontSize">setFontSize</a></li><li><a href="global.html#setFontStyle">setFontStyle</a></li><li><a href="global.html#setLineCap">setLineCap</a></li><li><a href="global.html#setLineJoin">setLineJoin</a></li><li><a href="global.html#setLineWidth">setLineWidth</a></li><li><a href="global.html#setPage">setPage</a></li><li><a href="global.html#setProperties">setProperties</a></li><li><a href="global.html#setTextColor">setTextColor</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#triangle">triangle</a></li></ul>
- </nav>
- <div id="main">
-
- <h1 class="page-title">plugins/cell.js</h1>
-
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>/** ====================================================================
- * jsPDF Cell plugin
- * Copyright (c) 2013 Youssef Beddad, youssef.beddad@gmail.com
- * 2013 Eduardo Menezes de Morais, eduardo.morais@usp.br
- * 2013 Lee Driscoll, https://github.com/lsdriscoll
- * 2014 Juan Pablo Gaviria, https://github.com/juanpgaviria
- * 2014 James Hall, james@parall.ax
- * 2014 Diego Casorran, https://github.com/diegocr
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- * ====================================================================
- */
- (function (jsPDFAPI) {
- 'use strict';
- /*jslint browser:true */
- /*global document: false, jsPDF */
- var fontName,
- fontSize,
- fontStyle,
- padding = 3,
- margin = 13,
- headerFunction,
- lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined },
- pages = 1,
- setLastCellPosition = function (x, y, w, h, ln) {
- lastCellPos = { 'x': x, 'y': y, 'w': w, 'h': h, 'ln': ln };
- },
- getLastCellPosition = function () {
- return lastCellPos;
- },
- NO_MARGINS = {left:0, top:0, bottom: 0};
- jsPDFAPI.setHeaderFunction = function (func) {
- headerFunction = func;
- };
- jsPDFAPI.getTextDimensions = function (txt) {
- fontName = this.internal.getFont().fontName;
- fontSize = this.table_font_size || this.internal.getFontSize();
- fontStyle = this.internal.getFont().fontStyle;
- // 1 pixel = 0.264583 mm and 1 mm = 72/25.4 point
- var px2pt = 0.264583 * 72 / 25.4,
- dimensions,
- text;
- text = document.createElement('font');
- text.id = "jsPDFCell";
- try {
- text.style.fontStyle = fontStyle;
- } catch(e) {
- text.style.fontWeight = fontStyle;
- }
- text.style.fontName = fontName;
- text.style.fontSize = fontSize + 'pt';
- try {
- text.textContent = txt;
- } catch(e) {
- text.innerText = txt;
- }
- document.body.appendChild(text);
- dimensions = { w: (text.offsetWidth + 1) * px2pt, h: (text.offsetHeight + 1) * px2pt};
- document.body.removeChild(text);
- return dimensions;
- };
- jsPDFAPI.cellAddPage = function () {
- var margins = this.margins || NO_MARGINS;
- this.addPage();
- setLastCellPosition(margins.left, margins.top, undefined, undefined);
- //setLastCellPosition(undefined, undefined, undefined, undefined, undefined);
- pages += 1;
- };
- jsPDFAPI.cellInitialize = function () {
- lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined };
- pages = 1;
- };
- jsPDFAPI.cell = function (x, y, w, h, txt, ln, align) {
- var curCell = getLastCellPosition();
- var pgAdded = false;
- // If this is not the first cell, we must change its position
- if (curCell.ln !== undefined) {
- if (curCell.ln === ln) {
- //Same line
- x = curCell.x + curCell.w;
- y = curCell.y;
- } else {
- //New line
- var margins = this.margins || NO_MARGINS;
- if ((curCell.y + curCell.h + h + margin) >= this.internal.pageSize.height - margins.bottom) {
- this.cellAddPage();
- pgAdded = true;
- if (this.printHeaders && this.tableHeaderRow) {
- this.printHeaderRow(ln, true);
- }
- }
- //We ignore the passed y: the lines may have different heights
- y = (getLastCellPosition().y + getLastCellPosition().h);
- if (pgAdded) y = margin + 10;
- }
- }
- if (txt[0] !== undefined) {
- if (this.printingHeaderRow) {
- this.rect(x, y, w, h, 'FD');
- } else {
- this.rect(x, y, w, h);
- }
- if (align === 'right') {
- if (!(txt instanceof Array)) {
- txt = [txt];
- }
- for (var i = 0; i < txt.length; i++) {
- var currentLine = txt[i];
- var textSize = this.getStringUnitWidth(currentLine) * this.internal.getFontSize();
- this.text(currentLine, x + w - textSize - padding, y + this.internal.getLineHeight()*(i+1));
- }
- } else {
- this.text(txt, x + padding, y + this.internal.getLineHeight());
- }
- }
- setLastCellPosition(x, y, w, h, ln);
- return this;
- };
- /**
- * Return the maximum value from an array
- * @param array
- * @param comparisonFn
- * @returns {*}
- */
- jsPDFAPI.arrayMax = function (array, comparisonFn) {
- var max = array[0],
- i,
- ln,
- item;
- for (i = 0, ln = array.length; i < ln; i += 1) {
- item = array[i];
- if (comparisonFn) {
- if (comparisonFn(max, item) === -1) {
- max = item;
- }
- } else {
- if (item > max) {
- max = item;
- }
- }
- }
- return max;
- };
- /**
- * Create a table from a set of data.
- * @param {Integer} [x] : left-position for top-left corner of table
- * @param {Integer} [y] top-position for top-left corner of table
- * @param {Object[]} [data] As array of objects containing key-value pairs corresponding to a row of data.
- * @param {String[]} [headers] Omit or null to auto-generate headers at a performance cost
- * @param {Object} [config.printHeaders] True to print column headers at the top of every page
- * @param {Object} [config.autoSize] True to dynamically set the column widths to match the widest cell value
- * @param {Object} [config.margins] margin values for left, top, bottom, and width
- * @param {Object} [config.fontSize] Integer fontSize to use (optional)
- */
- jsPDFAPI.table = function (x,y, data, headers, config) {
- if (!data) {
- throw 'No data for PDF table';
- }
- var headerNames = [],
- headerPrompts = [],
- header,
- i,
- ln,
- cln,
- columnMatrix = {},
- columnWidths = {},
- columnData,
- column,
- columnMinWidths = [],
- j,
- tableHeaderConfigs = [],
- model,
- jln,
- func,
- //set up defaults. If a value is provided in config, defaults will be overwritten:
- autoSize = false,
- printHeaders = true,
- fontSize = 12,
- margins = NO_MARGINS;
- margins.width = this.internal.pageSize.width;
- if (config) {
- //override config defaults if the user has specified non-default behavior:
- if(config.autoSize === true) {
- autoSize = true;
- }
- if(config.printHeaders === false) {
- printHeaders = false;
- }
- if(config.fontSize){
- fontSize = config.fontSize;
- }
- if (config.css && typeof(config.css['font-size']) !== "undefined") {
- fontSize = config.css['font-size'] * 16;
- }
- if(config.margins){
- margins = config.margins;
- }
- }
- /**
- * @property {Number} lnMod
- * Keep track of the current line number modifier used when creating cells
- */
- this.lnMod = 0;
- lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined },
- pages = 1;
- this.printHeaders = printHeaders;
- this.margins = margins;
- this.setFontSize(fontSize);
- this.table_font_size = fontSize;
- // Set header values
- if (headers === undefined || (headers === null)) {
- // No headers defined so we derive from data
- headerNames = Object.keys(data[0]);
- } else if (headers[0] && (typeof headers[0] !== 'string')) {
- var px2pt = 0.264583 * 72 / 25.4;
- // Split header configs into names and prompts
- for (i = 0, ln = headers.length; i < ln; i += 1) {
- header = headers[i];
- headerNames.push(header.name);
- headerPrompts.push(header.prompt);
- columnWidths[header.name] = header.width *px2pt;
- }
- } else {
- headerNames = headers;
- }
- if (autoSize) {
- // Create a matrix of columns e.g., {column_title: [row1_Record, row2_Record]}
- func = function (rec) {
- return rec[header];
- };
- for (i = 0, ln = headerNames.length; i < ln; i += 1) {
- header = headerNames[i];
- columnMatrix[header] = data.map(
- func
- );
- // get header width
- columnMinWidths.push(this.getTextDimensions(headerPrompts[i] || header).w);
- column = columnMatrix[header];
- // get cell widths
- for (j = 0, cln = column.length; j < cln; j += 1) {
- columnData = column[j];
- columnMinWidths.push(this.getTextDimensions(columnData).w);
- }
- // get final column width
- columnWidths[header] = jsPDFAPI.arrayMax(columnMinWidths);
-
- //have to reset
- columnMinWidths = [];
- }
- }
- // -- Construct the table
- if (printHeaders) {
- var lineHeight = this.calculateLineHeight(headerNames, columnWidths, headerPrompts.length?headerPrompts:headerNames);
- // Construct the header row
- for (i = 0, ln = headerNames.length; i < ln; i += 1) {
- header = headerNames[i];
- tableHeaderConfigs.push([x, y, columnWidths[header], lineHeight, String(headerPrompts.length ? headerPrompts[i] : header)]);
- }
- // Store the table header config
- this.setTableHeaderRow(tableHeaderConfigs);
- // Print the header for the start of the table
- this.printHeaderRow(1, false);
- }
- // Construct the data rows
- for (i = 0, ln = data.length; i < ln; i += 1) {
- var lineHeight;
- model = data[i];
- lineHeight = this.calculateLineHeight(headerNames, columnWidths, model);
- for (j = 0, jln = headerNames.length; j < jln; j += 1) {
- header = headerNames[j];
- this.cell(x, y, columnWidths[header], lineHeight, model[header], i + 2, header.align);
- }
- }
- this.lastCellPos = lastCellPos;
- this.table_x = x;
- this.table_y = y;
- return this;
- };
- /**
- * Calculate the height for containing the highest column
- * @param {String[]} headerNames is the header, used as keys to the data
- * @param {Integer[]} columnWidths is size of each column
- * @param {Object[]} model is the line of data we want to calculate the height of
- */
- jsPDFAPI.calculateLineHeight = function (headerNames, columnWidths, model) {
- var header, lineHeight = 0;
- for (var j = 0; j < headerNames.length; j++) {
- header = headerNames[j];
- model[header] = this.splitTextToSize(String(model[header]), columnWidths[header] - padding);
- var h = this.internal.getLineHeight() * model[header].length + padding;
- if (h > lineHeight)
- lineHeight = h;
- }
- return lineHeight;
- };
- /**
- * Store the config for outputting a table header
- * @param {Object[]} config
- * An array of cell configs that would define a header row: Each config matches the config used by jsPDFAPI.cell
- * except the ln parameter is excluded
- */
- jsPDFAPI.setTableHeaderRow = function (config) {
- this.tableHeaderRow = config;
- };
- /**
- * Output the store header row
- * @param lineNumber The line number to output the header at
- */
- jsPDFAPI.printHeaderRow = function (lineNumber, new_page) {
- if (!this.tableHeaderRow) {
- throw 'Property tableHeaderRow does not exist.';
- }
- var tableHeaderCell,
- tmpArray,
- i,
- ln;
- this.printingHeaderRow = true;
- if (headerFunction !== undefined) {
- var position = headerFunction(this, pages);
- setLastCellPosition(position[0], position[1], position[2], position[3], -1);
- }
- this.setFontStyle('bold');
- var tempHeaderConf = [];
- for (i = 0, ln = this.tableHeaderRow.length; i < ln; i += 1) {
- this.setFillColor(200,200,200);
- tableHeaderCell = this.tableHeaderRow[i];
- if (new_page) {
- this.margins.top = margin;
- tableHeaderCell[1] = this.margins && this.margins.top || 0;
- tempHeaderConf.push(tableHeaderCell);
- }
- tmpArray = [].concat(tableHeaderCell);
- this.cell.apply(this, tmpArray.concat(lineNumber));
- }
- if (tempHeaderConf.length > 0){
- this.setTableHeaderRow(tempHeaderConf);
- }
- this.setFontStyle('normal');
- this.printingHeaderRow = false;
- };
- })(jsPDF.API);
- </code></pre>
- </article>
- </section>
- </div>
- <br class="clear">
- <footer>
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Sun Oct 09 2016 11:08:27 GMT+0100 (BST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
- </footer>
- <script>prettyPrint();</script>
- <script src="scripts/linenumber.js"></script>
- </body>
- </html>
|