|
1 | | -/** @preserve jsPDF 0.9.0rc2 ( 2013-07-26T14:30 commit ID afdc433c2444775787f39c73c8f2533a41c6c724 ) |
| 1 | +/** @preserve jsPDF 0.9.0rc2 ( 2013-08-05T14:35 commit ID e3967b71f963e7a55c2e0573e996fdaa3f15c132 ) |
2 | 2 | Copyright (c) 2010-2012 James Hall, james@snapshotmedia.co.uk, https://github.com/MrRio/jsPDF |
3 | 3 | Copyright (c) 2012 Willow Systems Corporation, willow-systems.com |
4 | 4 | MIT license. |
@@ -3629,6 +3629,378 @@ API.events.push([ |
3629 | 3629 | } |
3630 | 3630 | ]) // end of adding event handler |
3631 | 3631 |
|
| 3632 | +})(jsPDF.API); |
| 3633 | +/** ==================================================================== |
| 3634 | + * jsPDF Cell plugin |
| 3635 | + * Copyright (c) 2013 Youssef Beddad, youssef.beddad@gmail.com |
| 3636 | + * |
| 3637 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 3638 | + * a copy of this software and associated documentation files (the |
| 3639 | + * "Software"), to deal in the Software without restriction, including |
| 3640 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 3641 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 3642 | + * permit persons to whom the Software is furnished to do so, subject to |
| 3643 | + * the following conditions: |
| 3644 | + * |
| 3645 | + * The above copyright notice and this permission notice shall be |
| 3646 | + * included in all copies or substantial portions of the Software. |
| 3647 | + * |
| 3648 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 3649 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 3650 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 3651 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 3652 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 3653 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 3654 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 3655 | + * ==================================================================== |
| 3656 | + */ |
| 3657 | + |
| 3658 | +(function (jsPDFAPI) { |
| 3659 | + 'use strict'; |
| 3660 | + /*jslint browser:true */ |
| 3661 | + /*global document: false, jsPDF */ |
| 3662 | + |
| 3663 | + var maxLn = 0, |
| 3664 | + lnP = 0, |
| 3665 | + fontName, |
| 3666 | + fontSize, |
| 3667 | + fontStyle, |
| 3668 | + lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined }, |
| 3669 | + pages = 1, |
| 3670 | + newPage = false, |
| 3671 | + setLastCellPosition = function (x, y, w, h, ln) { |
| 3672 | + lastCellPos = { x: x, y: y, w: w, h: h, ln: ln }; |
| 3673 | + }, |
| 3674 | + getLastCellPosition = function () { |
| 3675 | + return lastCellPos; |
| 3676 | + }, |
| 3677 | + setMaxLn = function (x) { |
| 3678 | + maxLn = x; |
| 3679 | + }, |
| 3680 | + getMaxLn = function () { |
| 3681 | + return maxLn; |
| 3682 | + }, |
| 3683 | + setLnP = function (x) { |
| 3684 | + lnP = x; |
| 3685 | + }, |
| 3686 | + getLnP = function (x) { |
| 3687 | + return lnP; |
| 3688 | + }; |
| 3689 | + |
| 3690 | + jsPDFAPI.getTextDimensions = function (txt) { |
| 3691 | + fontName = this.internal.getFont().fontName; |
| 3692 | + fontSize = this.internal.getFontSize(); |
| 3693 | + fontStyle = this.internal.getFont().fontStyle; |
| 3694 | + |
| 3695 | + // 1 pixel = 0.264583 mm and 1 mm = 72/25.4 point |
| 3696 | + var px2pt = 0.264583 * 72 / 25.4, |
| 3697 | + dimensions, |
| 3698 | + text; |
| 3699 | + |
| 3700 | + text = document.createElement('font'); |
| 3701 | + text.id = "jsPDFCell"; |
| 3702 | + text.style.fontStyle = fontStyle; |
| 3703 | + text.style.fontName = fontName; |
| 3704 | + text.style.fontSize = fontSize + 'pt'; |
| 3705 | + text.innerText = txt; |
| 3706 | + |
| 3707 | + document.body.appendChild(text); |
| 3708 | + |
| 3709 | + dimensions = { w: (text.offsetWidth + 1) * px2pt, h: (text.offsetHeight + 1) * px2pt}; |
| 3710 | + |
| 3711 | + document.body.removeChild(text); |
| 3712 | + |
| 3713 | + return dimensions; |
| 3714 | + }; |
| 3715 | + |
| 3716 | + jsPDFAPI.cellAddPage = function () { |
| 3717 | + this.addPage(); |
| 3718 | + setLastCellPosition(undefined, undefined, undefined, undefined, undefined); |
| 3719 | + newPage = true; |
| 3720 | + pages += 1; |
| 3721 | + setLnP(1); |
| 3722 | + }; |
| 3723 | + |
| 3724 | + jsPDFAPI.cellInitialize = function () { |
| 3725 | + maxLn = 0; |
| 3726 | + lastCellPos = { x: undefined, y: undefined, w: undefined, h: undefined, ln: undefined }; |
| 3727 | + pages = 1; |
| 3728 | + newPage = false; |
| 3729 | + setLnP(0); |
| 3730 | + }; |
| 3731 | + |
| 3732 | + jsPDFAPI.cell = function (x, y, w, h, txt, ln) { |
| 3733 | + this.lnMod = this.lnMod === undefined ? 0 : this.lnMod; |
| 3734 | + if (this.printingHeaderRow !== true && this.lnMod !== 0) { |
| 3735 | + ln = ln + this.lnMod; |
| 3736 | + } |
| 3737 | + |
| 3738 | + if ((((ln * h) + y + (h * 2)) / pages) >= this.internal.pageSize.height && pages === 1 && !newPage) { |
| 3739 | + this.cellAddPage(); |
| 3740 | + |
| 3741 | + if (this.printHeaders && this.tableHeaderRow) { |
| 3742 | + this.printHeaderRow(ln); |
| 3743 | + this.lnMod += 1; |
| 3744 | + ln += 1; |
| 3745 | + } |
| 3746 | + if (getMaxLn() === 0) { |
| 3747 | + setMaxLn(Math.round((this.internal.pageSize.height - (h * 2)) / h)); |
| 3748 | + } |
| 3749 | + } else if (newPage && getLastCellPosition().ln !== ln && getLnP() === getMaxLn()) { |
| 3750 | + this.cellAddPage(); |
| 3751 | + |
| 3752 | + if (this.printHeaders && this.tableHeaderRow) { |
| 3753 | + this.printHeaderRow(ln); |
| 3754 | + this.lnMod += 1; |
| 3755 | + ln += 1; |
| 3756 | + } |
| 3757 | + } |
| 3758 | + |
| 3759 | + var curCell = getLastCellPosition(), |
| 3760 | + dim = this.getTextDimensions(txt), |
| 3761 | + isNewLn = 1; |
| 3762 | + if (curCell.x !== undefined && curCell.ln === ln) { |
| 3763 | + x = curCell.x + curCell.w; |
| 3764 | + } |
| 3765 | + if (curCell.y !== undefined && curCell.y === y) { |
| 3766 | + y = curCell.y; |
| 3767 | + } |
| 3768 | + if (curCell.h !== undefined && curCell.h === h) { |
| 3769 | + h = curCell.h; |
| 3770 | + } |
| 3771 | + if (curCell.ln !== undefined && curCell.ln === ln) { |
| 3772 | + ln = curCell.ln; |
| 3773 | + isNewLn = 0; |
| 3774 | + } |
| 3775 | + if (newPage) { |
| 3776 | + y = h * (getLnP() + isNewLn); |
| 3777 | + } else { |
| 3778 | + y = (y + (h * Math.abs(getMaxLn() * pages - ln - getMaxLn()))); |
| 3779 | + } |
| 3780 | + this.rect(x, y, w, h); |
| 3781 | + this.text(txt, x + 3, y + h - 3); |
| 3782 | + setLnP(getLnP() + isNewLn); |
| 3783 | + setLastCellPosition(x, y, w, h, ln); |
| 3784 | + return this; |
| 3785 | + }; |
| 3786 | + |
| 3787 | + /** |
| 3788 | + * Return an array containing all of the owned keys of an Object |
| 3789 | + * @type {Function} |
| 3790 | + * @return {String[]} of Object keys |
| 3791 | + */ |
| 3792 | + jsPDFAPI.getKeys = (typeof Object.keys === 'function') |
| 3793 | + ? function (object) { |
| 3794 | + if (!object) { |
| 3795 | + return []; |
| 3796 | + } |
| 3797 | + return Object.keys(object); |
| 3798 | + } |
| 3799 | + : function (object) { |
| 3800 | + var keys = [], |
| 3801 | + property; |
| 3802 | + |
| 3803 | + for (property in object) { |
| 3804 | + if (object.hasOwnProperty(property)) { |
| 3805 | + keys.push(property); |
| 3806 | + } |
| 3807 | + } |
| 3808 | + |
| 3809 | + return keys; |
| 3810 | + }; |
| 3811 | + |
| 3812 | + /** |
| 3813 | + * Return the maximum value from an array |
| 3814 | + * @param array |
| 3815 | + * @param comparisonFn |
| 3816 | + * @returns {*} |
| 3817 | + */ |
| 3818 | + jsPDFAPI.arrayMax = function (array, comparisonFn) { |
| 3819 | + var max = array[0], |
| 3820 | + i, |
| 3821 | + ln, |
| 3822 | + item; |
| 3823 | + |
| 3824 | + for (i = 0, ln = array.length; i < ln; i += 1) { |
| 3825 | + item = array[i]; |
| 3826 | + |
| 3827 | + if (comparisonFn) { |
| 3828 | + if (comparisonFn(max, item) === -1) { |
| 3829 | + max = item; |
| 3830 | + } |
| 3831 | + } else { |
| 3832 | + if (item > max) { |
| 3833 | + max = item; |
| 3834 | + } |
| 3835 | + } |
| 3836 | + } |
| 3837 | + |
| 3838 | + return max; |
| 3839 | + }; |
| 3840 | + |
| 3841 | + /** |
| 3842 | + * Create a table from a set of data. |
| 3843 | + * @param {Object[]} data As array of objects containing key-value pairs |
| 3844 | + * @param {String[]} [headers] Omit or null to auto-generate headers at a performance cost |
| 3845 | + * @param {Object} [config.printHeaders] True to print column headers at the top of every page |
| 3846 | + * @param {Object} [config.autoSize] True to dynamically set the column widths to match the widest cell value |
| 3847 | + * @param {Object} [config.autoStretch] True to force the table to fit the width of the page |
| 3848 | + */ |
| 3849 | + jsPDFAPI.table = function (data, headers, config) { |
| 3850 | + |
| 3851 | + var headerNames = [], |
| 3852 | + headerPrompts = [], |
| 3853 | + header, |
| 3854 | + autoSize, |
| 3855 | + printHeaders, |
| 3856 | + autoStretch, |
| 3857 | + i, |
| 3858 | + ln, |
| 3859 | + columnMatrix = {}, |
| 3860 | + columnWidths = {}, |
| 3861 | + columnData, |
| 3862 | + column, |
| 3863 | + columnMinWidths = [], |
| 3864 | + j, |
| 3865 | + tableHeaderConfigs = [], |
| 3866 | + model, |
| 3867 | + jln, |
| 3868 | + func; |
| 3869 | + |
| 3870 | + /** |
| 3871 | + * @property {Number} lnMod |
| 3872 | + * Keep track of the current line number modifier used when creating cells |
| 3873 | + */ |
| 3874 | + this.lnMod = 0; |
| 3875 | + |
| 3876 | + if (config) { |
| 3877 | + autoSize = config.autoSize || false; |
| 3878 | + printHeaders = this.printHeaders = config.printHeaders || true; |
| 3879 | + autoStretch = config.autoStretch || true; |
| 3880 | + } |
| 3881 | + |
| 3882 | + if (!data) { |
| 3883 | + throw 'No data for PDF table'; |
| 3884 | + } |
| 3885 | + |
| 3886 | + // Set headers |
| 3887 | + if (headers === undefined || (headers === null)) { |
| 3888 | + |
| 3889 | + // No headers defined so we derive from data |
| 3890 | + headerNames = this.getKeys(data[0]); |
| 3891 | + |
| 3892 | + } else if (headers[0] && (typeof headers[0] !== 'string')) { |
| 3893 | + |
| 3894 | + // Split header configs into names and prompts |
| 3895 | + for (i = 0, ln = headers.length; i < ln; i += 1) { |
| 3896 | + header = headers[i]; |
| 3897 | + headerNames.push(header.name); |
| 3898 | + headerPrompts.push(header.prompt); |
| 3899 | + } |
| 3900 | + |
| 3901 | + } else { |
| 3902 | + headerNames = headers; |
| 3903 | + } |
| 3904 | + |
| 3905 | + if (config.autoSize) { |
| 3906 | + |
| 3907 | + // Create Columns Matrix |
| 3908 | + |
| 3909 | + func = function (rec) { |
| 3910 | + return rec[header]; |
| 3911 | + }; |
| 3912 | + |
| 3913 | + for (i = 0, ln = headerNames.length; i < ln; i += 1) { |
| 3914 | + header = headerNames[i]; |
| 3915 | + |
| 3916 | + columnMatrix[header] = data.map( |
| 3917 | + func |
| 3918 | + ); |
| 3919 | + |
| 3920 | + // get header width |
| 3921 | + columnMinWidths.push(this.getTextDimensions(headerPrompts[i] || header).w); |
| 3922 | + |
| 3923 | + column = columnMatrix[header]; |
| 3924 | + |
| 3925 | + // get cell widths |
| 3926 | + for (j = 0, ln = column.length; j < ln; j += 1) { |
| 3927 | + columnData = column[j]; |
| 3928 | + |
| 3929 | + columnMinWidths.push(this.getTextDimensions(columnData).w); |
| 3930 | + } |
| 3931 | + |
| 3932 | + // get final column width |
| 3933 | + columnWidths[header] = jsPDFAPI.arrayMax(columnMinWidths); |
| 3934 | + } |
| 3935 | + } |
| 3936 | + |
| 3937 | + // -- Construct the table |
| 3938 | + |
| 3939 | + if (config.printHeaders) { |
| 3940 | + |
| 3941 | + // Construct the header row |
| 3942 | + for (i = 0, ln = headerNames.length; i < ln; i += 1) { |
| 3943 | + header = headerNames[i]; |
| 3944 | + tableHeaderConfigs.push([10, 10, columnWidths[header], 25, String(headerPrompts.length ? headerPrompts[i] : header)]); |
| 3945 | + } |
| 3946 | + |
| 3947 | + // Store the table header config |
| 3948 | + this.setTableHeaderRow(tableHeaderConfigs); |
| 3949 | + |
| 3950 | + // Print the header for the start of the table |
| 3951 | + this.printHeaderRow(1); |
| 3952 | + } |
| 3953 | + |
| 3954 | + // Construct the data rows |
| 3955 | + for (i = 0, ln = data.length; i < ln; i += 1) { |
| 3956 | + model = data[i]; |
| 3957 | + |
| 3958 | + for (j = 0, jln = headerNames.length; j < jln; j += 1) { |
| 3959 | + header = headerNames[j]; |
| 3960 | + this.cell(10, 10, columnWidths[header], 25, String(model[header]), i + 2); |
| 3961 | + } |
| 3962 | + } |
| 3963 | + |
| 3964 | + return this; |
| 3965 | + }; |
| 3966 | + |
| 3967 | + /** |
| 3968 | + * Store the config for outputting a table header |
| 3969 | + * @param {Object[]} config |
| 3970 | + * An array of cell configs that would define a header row: Each config matches the config used by jsPDFAPI.cell |
| 3971 | + * except the ln parameter is excluded |
| 3972 | + */ |
| 3973 | + jsPDFAPI.setTableHeaderRow = function (config) { |
| 3974 | + this.tableHeaderRow = config; |
| 3975 | + }; |
| 3976 | + |
| 3977 | + /** |
| 3978 | + * Output the store header row |
| 3979 | + * @param lineNumber The line number to output the header at |
| 3980 | + */ |
| 3981 | + jsPDFAPI.printHeaderRow = function (lineNumber) { |
| 3982 | + if (!this.tableHeaderRow) { |
| 3983 | + throw 'Property tableHeaderRow does not exist.'; |
| 3984 | + } |
| 3985 | + |
| 3986 | + var tableHeaderCell, |
| 3987 | + tmpArray, |
| 3988 | + i, |
| 3989 | + ln; |
| 3990 | + |
| 3991 | + this.printingHeaderRow = true; |
| 3992 | + |
| 3993 | + for (i = 0, ln = this.tableHeaderRow.length; i < ln; i += 1) { |
| 3994 | + |
| 3995 | + tableHeaderCell = this.tableHeaderRow[i]; |
| 3996 | + tmpArray = [].concat(tableHeaderCell); |
| 3997 | + |
| 3998 | + this.cell.apply(this, tmpArray.concat(lineNumber)); |
| 3999 | + } |
| 4000 | + |
| 4001 | + this.printingHeaderRow = false; |
| 4002 | + }; |
| 4003 | + |
3632 | 4004 | })(jsPDF.API); |
3633 | 4005 | /** ==================================================================== |
3634 | 4006 | * jsPDF total_pages plugin |
@@ -3666,9 +4038,9 @@ jsPDFAPI.putTotalPages = function(pageExpression) { |
3666 | 4038 | this.internal.pages[n][i] = this.internal.pages[n][i].replace(replaceExpression, this.internal.getNumberOfPages()); |
3667 | 4039 | } |
3668 | 4040 | return this; |
3669 | | -} |
| 4041 | +}; |
3670 | 4042 |
|
3671 | | -})(jsPDF.API) |
| 4043 | +})(jsPDF.API); |
3672 | 4044 | /* BlobBuilder.js |
3673 | 4045 | * A BlobBuilder implementation. |
3674 | 4046 | * 2012-04-21 |
|
0 commit comments