1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370 |
- /**
- * @preserve tableExport.jquery.plugin
- *
- * Version 1.28.0
- *
- * Copyright (c) 2015-2023 hhurz,
- * https://github.com/hhurz/tableExport.jquery.plugin
- *
- * Based on https://github.com/kayalshri/tableExport.jquery.plugin
- *
- * Licensed under the MIT License
- **/
- 'use strict';
- (function ($) {
- $.fn.tableExport = function (options) {
- let docData;
- const defaults = {
- csvEnclosure: '"',
- csvSeparator: ',',
- csvUseBOM: true,
- date: {
- html: 'dd/mm/yyyy' // Date format used in html source. Supported placeholders: dd, mm, yy, yyyy and a arbitrary single separator character
- },
- displayTableName: false, // Deprecated
- escape: false, // Deprecated
- exportHiddenCells: false, // true = speed up export of large tables with hidden cells (hidden cells will be exported !)
- fileName: 'tableExport',
- htmlContent: false,
- htmlHyperlink: 'content', // Export the 'content' or the 'href' link of <a> tags unless onCellHtmlHyperlink is not defined
- ignoreColumn: [],
- ignoreRow: [],
- jsonScope: 'all', // One of 'head', 'data', 'all'
- jspdf: { // jsPDF / jsPDF-AutoTable related options
- orientation: 'p',
- unit: 'pt',
- format: 'a4', // One of jsPDF page formats or 'bestfit' for automatic paper format selection
- margins: {left: 20, right: 10, top: 10, bottom: 10},
- onDocCreated: null,
- autotable: {
- styles: {
- cellPadding: 2,
- rowHeight: 12,
- fontSize: 8,
- fillColor: 255, // Color value or 'inherit' to use css background-color from html table
- textColor: 50, // Color value or 'inherit' to use css color from html table
- fontStyle: 'normal', // 'normal', 'bold', 'italic', 'bolditalic' or 'inherit' to use css font-weight and font-style from html table
- overflow: 'ellipsize', // 'visible', 'hidden', 'ellipsize' or 'linebreak'
- halign: 'inherit', // 'left', 'center', 'right' or 'inherit' to use css horizontal cell alignment from html table
- valign: 'middle' // 'top', 'middle', or 'bottom'
- },
- headerStyles: {
- fillColor: [52, 73, 94],
- textColor: 255,
- fontStyle: 'bold',
- halign: 'inherit', // 'left', 'center', 'right' or 'inherit' to use css horizontal header cell alignment from html table
- valign: 'middle' // 'top', 'middle', or 'bottom'
- },
- alternateRowStyles: {
- fillColor: 245
- },
- tableExport: {
- doc: null, // jsPDF doc object. If set, an already created doc object will be used to export to
- onAfterAutotable: null,
- onBeforeAutotable: null,
- onAutotableText: null,
- onTable: null,
- outputImages: true
- }
- }
- },
- mso: { // MS Excel and MS Word related options
- fileFormat: 'xlshtml', // 'xlshtml' = Excel 2000 html format
- // 'xmlss' = XML Spreadsheet 2003 file format (XMLSS)
- // 'xlsx' = Excel 2007 Office Open XML format
- onMsoNumberFormat: null, // Excel 2000 html format only. See readme.md for more information about msonumberformat
- pageFormat: 'a4', // Page format used for page orientation
- pageOrientation: 'portrait', // portrait, landscape (xlshtml format only)
- rtl: false, // true = Set worksheet option 'DisplayRightToLeft'
- styles: [], // E.g. ['border-bottom', 'border-top', 'border-left', 'border-right']
- worksheetName: '',
- xlsx: { // Specific Excel 2007 XML format settings:
- formatId: { // XLSX format (id) used to format excel cells. See readme.md: data-tableexport-xlsxformatid
- date: 14, // The default format id, or a format string (e.g. 'm/d/yy'), or a function(cell, row, col)
- numbers: 2, // The default Format id, or a format string (e.g. '\"T\"\ #0.00'), or a function(cell, row, col)
- currency: 164 // This id is used by "data-tableexport-xlsxformatid" to allow you to export a cell in currency format (see below)
- },
- format: {
- currency: '$#,##0.00;[Red]-$#,##0.00' // The format string to be used for the export for the currency format
- // Euro format: '#,##0.00 €;[Red](#,##0.00) €'
- },
- onHyperlink: null // function($cell, row, col, href, content, hyperlink): Return what to export for hyperlinks
- }
- },
- numbers: {
- html: {
- decimalMark: '.', // Decimal mark in html source
- thousandsSeparator: ',' // Thousands separator in html source
- },
- output: { // Set 'output: false' to keep number format of html source in resulting output
- decimalMark: '.', // Decimal mark in resulting output
- thousandsSeparator: ',' // Thousands separator in resulting output
- }
- },
- onAfterSaveToFile: null, // function(data, fileName)
- onBeforeSaveToFile: null, // saveIt = function(data, fileName, type, charset, encoding): Return false to abort save process
- onCellData: null, // Text to export = function($cell, row, col, href, cellText, cellType)
- onCellHtmlData: null, // Text to export = function($cell, row, col, htmlContent)
- onCellHtmlHyperlink: null, // Text to export = function($cell, row, col, href, cellText)
- onIgnoreRow: null, // ignoreRow = function($tr, row): Return true to prevent export of the row
- onTableExportBegin: null, // function() - called when export starts
- onTableExportEnd: null, // function() - called when export ends
- outputMode: 'file', // 'file', 'string', 'base64' or 'window' (experimental)
- pdfmake: {
- enabled: false, // true: Use pdfmake as pdf producer instead of jspdf and jspdf-autotable
- docDefinition: {
- pageSize: 'A4', // 4A0,2A0,A{0-10},B{0-10},C{0-10},RA{0-4},SRA{0-4},EXECUTIVE,FOLIO,LEGAL,LETTER,TABLOID
- pageOrientation: 'portrait', // 'portrait' or 'landscape'
- styles: {
- header: {
- background: '#34495E',
- color: '#FFFFFF',
- bold: true,
- alignment: 'center',
- fillColor: '#34495E'
- },
- alternateRow: {
- fillColor: '#f5f5f5'
- }
- },
- defaultStyle: {
- color: '#000000',
- fontSize: 8,
- font: 'Roboto' // Default font is 'Roboto' which needs vfs_fonts.js to be included
- } // To export arabic characters include mirza_fonts.js _instead_ of vfs_fonts.js
- }, // For a chinese font include either gbsn00lp_fonts.js or ZCOOLXiaoWei_fonts.js _instead_ of vfs_fonts.js
- fonts: {}
- },
- preserve: {
- leadingWS: false, // preserve leading white spaces
- trailingWS: false // preserve trailing white spaces
- },
- preventInjection: true, // Prepend a single quote to cell strings that start with =,+,- or @ to prevent formula injection
- sql: {
- tableEnclosure: '`', // If table name or column names contain any characters except letters, numbers, and
- columnEnclosure: '`' // underscores, usually the name must be delimited by enclosing it in back quotes (`)
- },
- tbodySelector: 'tr',
- tfootSelector: 'tr', // Set empty ('') to prevent export of tfoot rows
- theadSelector: 'tr',
- tableName: 'Table',
- type: 'excel' // Export format: 'csv', 'tsv', 'txt', 'sql', 'json', 'xml', 'excel', 'doc', 'png' or 'pdf'
- };
- const pageFormats = { // Size in pt of various paper formats. Adopted from jsPDF.
- 'a0': [2383.94, 3370.39], 'a1': [1683.78, 2383.94], 'a2': [1190.55, 1683.78],
- 'a3': [841.89, 1190.55], 'a4': [595.28, 841.89], 'a5': [419.53, 595.28],
- 'a6': [297.64, 419.53], 'a7': [209.76, 297.64], 'a8': [147.40, 209.76],
- 'a9': [104.88, 147.40], 'a10': [73.70, 104.88],
- 'b0': [2834.65, 4008.19], 'b1': [2004.09, 2834.65], 'b2': [1417.32, 2004.09],
- 'b3': [1000.63, 1417.32], 'b4': [708.66, 1000.63], 'b5': [498.90, 708.66],
- 'b6': [354.33, 498.90], 'b7': [249.45, 354.33], 'b8': [175.75, 249.45],
- 'b9': [124.72, 175.75], 'b10': [87.87, 124.72],
- 'c0': [2599.37, 3676.54],
- 'c1': [1836.85, 2599.37], 'c2': [1298.27, 1836.85], 'c3': [918.43, 1298.27],
- 'c4': [649.13, 918.43], 'c5': [459.21, 649.13], 'c6': [323.15, 459.21],
- 'c7': [229.61, 323.15], 'c8': [161.57, 229.61], 'c9': [113.39, 161.57],
- 'c10': [79.37, 113.39],
- 'dl': [311.81, 623.62],
- 'letter': [612, 792], 'government-letter': [576, 756], 'legal': [612, 1008],
- 'junior-legal': [576, 360], 'ledger': [1224, 792], 'tabloid': [792, 1224],
- 'credit-card': [153, 243]
- };
- const jsPdfThemes = { // Styles for the themes
- 'striped': {
- table: {
- fillColor: 255,
- textColor: 80,
- fontStyle: 'normal',
- fillStyle: 'F'
- },
- header: {
- textColor: 255,
- fillColor: [41, 128, 185],
- rowHeight: 23,
- fontStyle: 'bold'
- },
- body: {},
- alternateRow: {fillColor: 245}
- },
- 'grid': {
- table: {
- fillColor: 255,
- textColor: 80,
- fontStyle: 'normal',
- lineWidth: 0.1,
- fillStyle: 'DF'
- },
- header: {
- textColor: 255,
- fillColor: [26, 188, 156],
- rowHeight: 23,
- fillStyle: 'F',
- fontStyle: 'bold'
- },
- body: {},
- alternateRow: {}
- },
- 'plain': {header: {fontStyle: 'bold'}}
- };
- let jsPdfDefaultStyles = { // Base style for all themes
- cellPadding: 5,
- fontSize: 10,
- fontName: "helvetica", // helvetica, times, courier, malgun
- lineColor: 200,
- lineWidth: 0.1,
- fontStyle: 'normal', // normal, bold, italic, bolditalic
- overflow: 'ellipsize', // visible, hidden, ellipsize or linebreak
- fillColor: 255,
- textColor: 20,
- halign: 'left', // left, center, right
- valign: 'top', // top, middle, bottom
- fillStyle: 'F', // 'S', 'F' or 'DF' (stroke, fill or fill then stroke)
- rowHeight: 20,
- columnWidth: 'auto'
- };
- const FONT_ROW_RATIO = 1.15;
- const el = this;
- let DownloadEvt = null;
- let $head_rows = [];
- let $rows = [];
- let rowIndex = 0;
- let trData = '';
- let colNames = [];
- let ranges = [];
- let blob;
- let $hiddenTableElements = [];
- let checkCellVisibility = false;
- $.extend(true, defaults, options);
- // Adopt deprecated options
- if (defaults.type === 'xlsx') {
- defaults.mso.fileFormat = defaults.type;
- defaults.type = 'excel';
- }
- if (typeof defaults.excelFileFormat !== 'undefined' && typeof defaults.mso.fileFormat === 'undefined')
- defaults.mso.fileFormat = defaults.excelFileFormat;
- if (typeof defaults.excelPageFormat !== 'undefined' && typeof defaults.mso.pageFormat === 'undefined')
- defaults.mso.pageFormat = defaults.excelPageFormat;
- if (typeof defaults.excelPageOrientation !== 'undefined' && typeof defaults.mso.pageOrientation === 'undefined')
- defaults.mso.pageOrientation = defaults.excelPageOrientation;
- if (typeof defaults.excelRTL !== 'undefined' && typeof defaults.mso.rtl === 'undefined')
- defaults.mso.rtl = defaults.excelRTL;
- if (typeof defaults.excelstyles !== 'undefined' && typeof defaults.mso.styles === 'undefined')
- defaults.mso.styles = defaults.excelstyles;
- if (typeof defaults.onMsoNumberFormat !== 'undefined' && typeof defaults.mso.onMsoNumberFormat === 'undefined')
- defaults.mso.onMsoNumberFormat = defaults.onMsoNumberFormat;
- if (typeof defaults.worksheetName !== 'undefined' && typeof defaults.mso.worksheetName === 'undefined')
- defaults.mso.worksheetName = defaults.worksheetName;
- if (typeof defaults.mso.xslx !== 'undefined' && typeof defaults.mso.xlsx === 'undefined')
- defaults.mso.xlsx = defaults.mso.xslx;
- // Check values of some options
- defaults.mso.pageOrientation = (defaults.mso.pageOrientation.substr(0, 1) === 'l') ? 'landscape' : 'portrait';
- defaults.date.html = defaults.date.html || '';
- if (defaults.date.html.length) {
- const patt = [];
- patt['dd'] = '(3[01]|[12][0-9]|0?[1-9])';
- patt['mm'] = '(1[012]|0?[1-9])';
- patt['yyyy'] = '((?:1[6-9]|2[0-2])\\d{2})';
- patt['yy'] = '(\\d{2})';
- const separator = defaults.date.html.match(/[^a-zA-Z0-9]/)[0];
- const formatItems = defaults.date.html.toLowerCase().split(separator);
- defaults.date.regex = '^\\s*';
- defaults.date.regex += patt[formatItems[0]];
- defaults.date.regex += '(.)'; // separator group
- defaults.date.regex += patt[formatItems[1]];
- defaults.date.regex += '\\2'; // identical separator group
- defaults.date.regex += patt[formatItems[2]];
- defaults.date.regex += '\\s*$';
- // e.g. '^\\s*(3[01]|[12][0-9]|0?[1-9])(.)(1[012]|0?[1-9])\\2((?:1[6-9]|2[0-2])\\d{2})\\s*$'
- defaults.date.pattern = new RegExp(defaults.date.regex, 'g');
- let f = formatItems.indexOf('dd') + 1;
- defaults.date.match_d = f + (f > 1 ? 1 : 0);
- f = formatItems.indexOf('mm') + 1;
- defaults.date.match_m = f + (f > 1 ? 1 : 0);
- f = (formatItems.indexOf('yyyy') >= 0 ? formatItems.indexOf('yyyy') : formatItems.indexOf('yy')) + 1;
- defaults.date.match_y = f + (f > 1 ? 1 : 0);
- }
- colNames = GetColumnNames(el);
- if (typeof defaults.onTableExportBegin === 'function')
- defaults.onTableExportBegin();
- if (defaults.type === 'csv' || defaults.type === 'tsv' || defaults.type === 'txt') {
- let csvData = '';
- let rowLength = 0;
- ranges = [];
- rowIndex = 0;
- const csvString = function (cell, rowIndex, colIndex) {
- let result = '';
- if (cell !== null) {
- const dataString = parseString(cell, rowIndex, colIndex);
- const csvValue = (dataString === null || dataString === '') ? '' : dataString.toString();
- if (defaults.type === 'tsv') {
- if (dataString instanceof Date)
- dataString.toLocaleString();
- // According to http://www.iana.org/assignments/media-types/text/tab-separated-values
- // are fields that contain tabs not allowable in tsv encoding
- result = replaceAll(csvValue, '\t', ' ');
- } else {
- // Takes a string and encapsulates it (by default in double-quotes) if it
- // contains the csv field separator, spaces, or linebreaks.
- if (dataString instanceof Date)
- result = defaults.csvEnclosure + dataString.toLocaleString() + defaults.csvEnclosure;
- else {
- result = preventInjection(csvValue);
- result = replaceAll(result, defaults.csvEnclosure, defaults.csvEnclosure + defaults.csvEnclosure);
- if (result.indexOf(defaults.csvSeparator) >= 0 || /[\r\n ]/g.test(result))
- result = defaults.csvEnclosure + result + defaults.csvEnclosure;
- }
- }
- }
- return result;
- };
- const CollectCsvData = function ($rows, rowselector, length) {
- $rows.each(function () {
- trData = '';
- ForEachVisibleCell(this, rowselector, rowIndex, length + $rows.length,
- function (cell, row, col) {
- trData += csvString(cell, row, col) + (defaults.type === 'tsv' ? '\t' : defaults.csvSeparator);
- });
- trData = $.trim(trData).substring(0, trData.length - 1);
- if (trData.length > 0) {
- if (csvData.length > 0)
- csvData += '\n';
- csvData += trData;
- }
- rowIndex++;
- });
- return $rows.length;
- };
- rowLength += CollectCsvData($(el).find('thead').first().find(defaults.theadSelector), 'th,td', rowLength);
- findTableElements($(el), 'tbody').each(function () {
- rowLength += CollectCsvData(findTableElements($(this), defaults.tbodySelector), 'td,th', rowLength);
- });
- if (defaults.tfootSelector.length)
- CollectCsvData($(el).find('tfoot').first().find(defaults.tfootSelector), 'td,th', rowLength);
- csvData += '\n';
- //output
- if (defaults.outputMode === 'string')
- return csvData;
- if (defaults.outputMode === 'base64')
- return base64encode(csvData);
- if (defaults.outputMode === 'window') {
- downloadFile(false, 'data:text/' + (defaults.type === 'csv' ? 'csv' : 'plain') + ';charset=utf-8,', csvData);
- return;
- }
- saveToFile(csvData,
- defaults.fileName + '.' + defaults.type,
- 'text/' + (defaults.type === 'csv' ? 'csv' : 'plain'),
- 'utf-8',
- '',
- (defaults.type === 'csv' && defaults.csvUseBOM));
- } else if (defaults.type === 'sql') {
- // Header
- rowIndex = 0;
- ranges = [];
- let tdData = 'INSERT INTO ' + defaults.sql.tableEnclosure + defaults.tableName + defaults.sql.tableEnclosure + ' (';
- $head_rows = collectHeadRows($(el));
- $($head_rows).each(function () {
- ForEachVisibleCell(this, 'th,td', rowIndex, $head_rows.length,
- function (cell, row, col) {
- let colName = parseString(cell, row, col) || '';
- if (colName.indexOf(defaults.sql.columnEnclosure) > -1)
- colName = replaceAll(colName.toString(), defaults.sql.columnEnclosure, defaults.sql.columnEnclosure + defaults.sql.columnEnclosure);
- tdData += defaults.sql.columnEnclosure + colName + defaults.sql.columnEnclosure + ',';
- });
- rowIndex++;
- tdData = $.trim(tdData).substring(0, tdData.length - 1);
- });
- tdData += ') VALUES ';
- // Data
- $rows = collectRows($(el));
- $($rows).each(function () {
- trData = '';
- ForEachVisibleCell(this, 'td,th', rowIndex, $head_rows.length + $rows.length,
- function (cell, row, col) {
- let dataString = parseString(cell, row, col) || '';
- if (dataString.indexOf('\'') > -1)
- dataString = replaceAll(dataString.toString(), '\'', '\'\'');
- trData += '\'' + dataString + '\',';
- });
- if (trData.length > 3) {
- tdData += '(' + trData;
- tdData = $.trim(tdData).substring(0, tdData.length - 1);
- tdData += '),';
- }
- rowIndex++;
- });
- tdData = $.trim(tdData).substring(0, tdData.length - 1);
- tdData += ';';
- // Output
- if (defaults.outputMode === 'string')
- return tdData;
- if (defaults.outputMode === 'base64')
- return base64encode(tdData);
- saveToFile(tdData, defaults.fileName + '.sql', 'application/sql', 'utf-8', '', false);
- } else if (defaults.type === 'json') {
- const jsonHeaderArray = [];
- ranges = [];
- $head_rows = collectHeadRows($(el));
- $($head_rows).each(function () {
- const jsonArrayTd = [];
- ForEachVisibleCell(this, 'th,td', rowIndex, $head_rows.length,
- function (cell, row, col) {
- jsonArrayTd.push(parseString(cell, row, col));
- });
- jsonHeaderArray.push(jsonArrayTd);
- });
- // Data
- const jsonArray = [];
- $rows = collectRows($(el));
- $($rows).each(function () {
- const jsonObjectTd = {};
- let colIndex = 0;
- ForEachVisibleCell(this, 'td,th', rowIndex, $head_rows.length + $rows.length,
- function (cell, row, col) {
- if (jsonHeaderArray.length) {
- jsonObjectTd[jsonHeaderArray[jsonHeaderArray.length - 1][colIndex]] = parseString(cell, row, col);
- } else {
- jsonObjectTd[colIndex] = parseString(cell, row, col);
- }
- colIndex++;
- });
- if ($.isEmptyObject(jsonObjectTd) === false)
- jsonArray.push(jsonObjectTd);
- rowIndex++;
- });
- let save_data;
- if (defaults.jsonScope === 'head')
- save_data = JSON.stringify(jsonHeaderArray);
- else if (defaults.jsonScope === 'data')
- save_data = JSON.stringify(jsonArray);
- else // all
- save_data = JSON.stringify({header: jsonHeaderArray, data: jsonArray});
- if (defaults.outputMode === 'string')
- return save_data;
- if (defaults.outputMode === 'base64')
- return base64encode(save_data);
- saveToFile(save_data, defaults.fileName + '.json', 'application/json', 'utf-8', 'base64', false);
- } else if (defaults.type === 'xml') {
- rowIndex = 0;
- ranges = [];
- let xml = '<?xml version="1.0" encoding="utf-8"?>';
- xml += '<tabledata><fields>';
- // Header
- $head_rows = collectHeadRows($(el));
- $($head_rows).each(function () {
- ForEachVisibleCell(this, 'th,td', rowIndex, $head_rows.length,
- function (cell, row, col) {
- xml += '<field>' + parseString(cell, row, col) + '</field>';
- });
- rowIndex++;
- });
- xml += '</fields><data>';
- // Data
- let rowCount = 1;
- $rows = collectRows($(el));
- $($rows).each(function () {
- let colCount = 1;
- trData = '';
- ForEachVisibleCell(this, 'td,th', rowIndex, $head_rows.length + $rows.length,
- function (cell, row, col) {
- trData += '<column-' + colCount + '>' + parseString(cell, row, col) + '</column-' + colCount + '>';
- colCount++;
- });
- if (trData.length > 0 && trData !== '<column-1></column-1>') {
- xml += '<row id="' + rowCount + '">' + trData + '</row>';
- rowCount++;
- }
- rowIndex++;
- });
- xml += '</data></tabledata>';
- // Output
- if (defaults.outputMode === 'string')
- return xml;
- if (defaults.outputMode === 'base64')
- return base64encode(xml);
- saveToFile(xml, defaults.fileName + '.xml', 'application/xml', 'utf-8', 'base64', false);
- } else if (defaults.type === 'excel' && defaults.mso.fileFormat === 'xmlss') {
- const sheetData = [];
- const docNames = [];
- $(el).filter(function () {
- return isVisible($(this));
- }).each(function () {
- const $table = $(this);
- let ssName = '';
- if (typeof defaults.mso.worksheetName === 'string' && defaults.mso.worksheetName.length)
- ssName = defaults.mso.worksheetName + ' ' + (docNames.length + 1);
- else if (typeof defaults.mso.worksheetName[docNames.length] !== 'undefined')
- ssName = defaults.mso.worksheetName[docNames.length];
- if (!ssName.length)
- ssName = $table.find('caption').text() || '';
- if (!ssName.length)
- ssName = 'Table ' + (docNames.length + 1);
- ssName = $.trim(ssName.replace(/[\\\/[\]*:?'"]/g, '').substring(0, 31));
- docNames.push($('<div />').text(ssName).html());
- if (defaults.exportHiddenCells === false) {
- $hiddenTableElements = $table.find('tr, th, td').filter(':hidden');
- checkCellVisibility = $hiddenTableElements.length > 0;
- }
- rowIndex = 0;
- colNames = GetColumnNames(this);
- docData = '<Table>\r';
- function CollectXmlssData ($rows, rowselector, length) {
- const spans = [];
- $($rows).each(function () {
- let ssIndex = 0;
- let nCols = 0;
- trData = '';
- ForEachVisibleCell(this, 'td,th', rowIndex, length + $rows.length,
- function (cell, row, col) {
- if (cell !== null) {
- let style = '';
- let data = parseString(cell, row, col);
- let type = 'String';
- if (jQuery.isNumeric(data) !== false) {
- type = 'Number';
- } else {
- const number = parsePercent(data);
- if (number !== false) {
- data = number;
- type = 'Number';
- style += ' ss:StyleID="pct1"';
- }
- }
- if (type !== 'Number')
- data = data.replace(/\n/g, '<br>');
- let colspan = getColspan(cell);
- let rowspan = getRowspan(cell);
- // Skip spans
- $.each(spans, function () {
- const range = this;
- if (rowIndex >= range.s.r && rowIndex <= range.e.r && nCols >= range.s.c && nCols <= range.e.c) {
- for (let i = 0; i <= range.e.c - range.s.c; ++i) {
- nCols++;
- ssIndex++;
- }
- }
- });
- // Handle Row Span
- if (rowspan || colspan) {
- rowspan = rowspan || 1;
- colspan = colspan || 1;
- spans.push({
- s: {r: rowIndex, c: nCols},
- e: {r: rowIndex + rowspan - 1, c: nCols + colspan - 1}
- });
- }
- // Handle Colspan
- if (colspan > 1) {
- style += ' ss:MergeAcross="' + (colspan - 1) + '"';
- nCols += (colspan - 1);
- }
- if (rowspan > 1) {
- style += ' ss:MergeDown="' + (rowspan - 1) + '" ss:StyleID="rsp1"';
- }
- if (ssIndex > 0) {
- style += ' ss:Index="' + (nCols + 1) + '"';
- ssIndex = 0;
- }
- trData += '<Cell' + style + '><Data ss:Type="' + type + '">' +
- $('<div />').text(data).html() +
- '</Data></Cell>\r';
- nCols++;
- }
- });
- if (trData.length > 0)
- docData += '<Row ss:AutoFitHeight="0">\r' + trData + '</Row>\r';
- rowIndex++;
- });
- return $rows.length;
- }
- const rowLength = CollectXmlssData(collectHeadRows($table), 'th,td', 0);
- CollectXmlssData(collectRows($table), 'td,th', rowLength);
- docData += '</Table>\r';
- sheetData.push(docData);
- });
- const count = {};
- const firstOccurrences = {};
- let item, itemCount;
- for (let n = 0, c = docNames.length; n < c; n++) {
- item = docNames[n];
- itemCount = count[item];
- itemCount = count[item] = (itemCount == null ? 1 : itemCount + 1);
- if (itemCount === 2)
- docNames[firstOccurrences[item]] = docNames[firstOccurrences[item]].substring(0, 29) + '-1';
- if (count[item] > 1)
- docNames[n] = docNames[n].substring(0, 29) + '-' + count[item];
- else
- firstOccurrences[item] = n;
- }
- const CreationDate = new Date().toISOString();
- let xmlssDocFile = '<?xml version="1.0" encoding="UTF-8"?>\r' +
- '<?mso-application progid="Excel.Sheet"?>\r' +
- '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"\r' +
- ' xmlns:o="urn:schemas-microsoft-com:office:office"\r' +
- ' xmlns:x="urn:schemas-microsoft-com:office:excel"\r' +
- ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"\r' +
- ' xmlns:html="http://www.w3.org/TR/REC-html40">\r' +
- '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">\r' +
- ' <Created>' + CreationDate + '</Created>\r' +
- '</DocumentProperties>\r' +
- '<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">\r' +
- ' <AllowPNG/>\r' +
- '</OfficeDocumentSettings>\r' +
- '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">\r' +
- ' <WindowHeight>9000</WindowHeight>\r' +
- ' <WindowWidth>13860</WindowWidth>\r' +
- ' <WindowTopX>0</WindowTopX>\r' +
- ' <WindowTopY>0</WindowTopY>\r' +
- ' <ProtectStructure>False</ProtectStructure>\r' +
- ' <ProtectWindows>False</ProtectWindows>\r' +
- '</ExcelWorkbook>\r' +
- '<Styles>\r' +
- ' <Style ss:ID="Default" ss:Name="Normal">\r' +
- ' <Alignment ss:Vertical="Bottom"/>\r' +
- ' <Borders/>\r' +
- ' <Font/>\r' +
- ' <Interior/>\r' +
- ' <NumberFormat/>\r' +
- ' <Protection/>\r' +
- ' </Style>\r' +
- ' <Style ss:ID="rsp1">\r' +
- ' <Alignment ss:Vertical="Center"/>\r' +
- ' </Style>\r' +
- ' <Style ss:ID="pct1">\r' +
- ' <NumberFormat ss:Format="Percent"/>\r' +
- ' </Style>\r' +
- '</Styles>\r';
- for (let j = 0; j < sheetData.length; j++) {
- xmlssDocFile += '<Worksheet ss:Name="' + docNames[j] + '" ss:RightToLeft="' + (defaults.mso.rtl ? '1' : '0') + '">\r' +
- sheetData[j];
- if (defaults.mso.rtl) {
- xmlssDocFile += '<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">\r' +
- '<DisplayRightToLeft/>\r' +
- '</WorksheetOptions>\r';
- } else
- xmlssDocFile += '<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"/>\r';
- xmlssDocFile += '</Worksheet>\r';
- }
- xmlssDocFile += '</Workbook>\r';
- if (defaults.outputMode === 'string')
- return xmlssDocFile;
- if (defaults.outputMode === 'base64')
- return base64encode(xmlssDocFile);
- saveToFile(xmlssDocFile, defaults.fileName + '.xml', 'application/xml', 'utf-8', 'base64', false);
- } else if (defaults.type === 'excel' && defaults.mso.fileFormat === 'xlsx') {
- const sheetNames = [];
- const workbook = XLSX.utils.book_new();
- // Multiple worksheets and .xlsx file extension #202
- $(el).filter(function () {
- return isVisible($(this));
- }).each(function () {
- const $table = $(this);
- const ws = xlsxTableToSheet(this);
- let sheetName = '';
- if (typeof defaults.mso.worksheetName === 'string' && defaults.mso.worksheetName.length)
- sheetName = defaults.mso.worksheetName + ' ' + (sheetNames.length + 1);
- else if (typeof defaults.mso.worksheetName[sheetNames.length] !== 'undefined')
- sheetName = defaults.mso.worksheetName[sheetNames.length];
- if (!sheetName.length)
- sheetName = $table.find('caption').text() || '';
- if (!sheetName.length)
- sheetName = 'Table ' + (sheetNames.length + 1);
- sheetName = $.trim(sheetName.replace(/[\\\/[\]*:?'"]/g, '').substring(0, 31));
- sheetNames.push(sheetName);
- XLSX.utils.book_append_sheet(workbook, ws, sheetName);
- });
- // add worksheet to workbook
- const wbData = XLSX.write(workbook, {type: 'binary', bookType: defaults.mso.fileFormat, bookSST: false});
- saveToFile(xlsxWorkbookToArrayBuffer(wbData),
- defaults.fileName + '.' + defaults.mso.fileFormat,
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- 'UTF-8', '', false);
- } else if (defaults.type === 'excel' || defaults.type === 'xls' || defaults.type === 'word' || defaults.type === 'doc') {
- const MSDocType = (defaults.type === 'excel' || defaults.type === 'xls') ? 'excel' : 'word';
- const MSDocExt = (MSDocType === 'excel') ? 'xls' : 'doc';
- const MSDocSchema = 'xmlns:x="urn:schemas-microsoft-com:office:' + MSDocType + '"';
- docData = '';
- let docName = '';
- $(el).filter(function () {
- return isVisible($(this));
- }).each(function () {
- const $table = $(this);
- if (docName === '') {
- docName = defaults.mso.worksheetName || $table.find('caption').text() || 'Table';
- docName = $.trim(docName.replace(/[\\\/[\]*:?'"]/g, '').substring(0, 31));
- }
- if (defaults.exportHiddenCells === false) {
- $hiddenTableElements = $table.find('tr, th, td').filter(':hidden');
- checkCellVisibility = $hiddenTableElements.length > 0;
- }
- rowIndex = 0;
- ranges = [];
- colNames = GetColumnNames(this);
- // Header
- docData += '<table><thead>';
- $head_rows = collectHeadRows($table);
- $($head_rows).each(function () {
- const $row = $(this);
- const rowStyles = document.defaultView.getComputedStyle($row[0], null);
- trData = '';
- ForEachVisibleCell(this, 'th,td', rowIndex, $head_rows.length,
- function (cell, row, col) {
- if (cell !== null) {
- let thStyle = '';
- trData += '<th';
- if (defaults.mso.styles.length) {
- const cellStyles = document.defaultView.getComputedStyle(cell, null);
- for (let cssStyle in defaults.mso.styles) {
- const stylePropertyName = defaults.mso.styles[cssStyle];
- let thCss = getStyle(cellStyles, stylePropertyName);
- if (thCss === '')
- thCss = getStyle(rowStyles, stylePropertyName);
- if (thCss !== '' && thCss !== '0px none rgb(0, 0, 0)' && thCss !== 'rgba(0, 0, 0, 0)') {
- thStyle += (thStyle === '') ? 'style="' : ';';
- thStyle += stylePropertyName + ':' + thCss;
- }
- }
- }
- if (thStyle !== '')
- trData += ' ' + thStyle + '"';
- const tdColspan = getColspan(cell);
- if (tdColspan > 0)
- trData += ' colspan="' + tdColspan + '"';
- const tdRowspan = getRowspan(cell);
- if (tdRowspan > 0)
- trData += ' rowspan="' + tdRowspan + '"';
- trData += '>' + parseString(cell, row, col) + '</th>';
- }
- });
- if (trData.length > 0)
- docData += '<tr>' + trData + '</tr>';
- rowIndex++;
- });
- docData += '</thead><tbody>';
- // Data
- $rows = collectRows($table);
- $($rows).each(function () {
- const $row = $(this);
- let cellStyles = null;
- let rowStyles = null;
- trData = '';
- ForEachVisibleCell(this, 'td,th', rowIndex, $head_rows.length + $rows.length,
- function (cell, row, col) {
- if (cell !== null) {
- let tdValue = parseString(cell, row, col);
- let tdStyle = '';
- let tdCss = $(cell).attr('data-tableexport-msonumberformat');
- if (typeof tdCss === 'undefined' && typeof defaults.mso.onMsoNumberFormat === 'function')
- tdCss = defaults.mso.onMsoNumberFormat(cell, row, col);
- if (typeof tdCss !== 'undefined' && tdCss !== '')
- tdStyle = 'style="mso-number-format:\'' + tdCss + '\'';
- if (defaults.mso.styles.length) {
- cellStyles = document.defaultView.getComputedStyle(cell, null);
- rowStyles = null;
- for (let cssStyle in defaults.mso.styles) {
- const stylePropertyName = defaults.mso.styles[cssStyle];
- tdCss = getStyle(cellStyles, stylePropertyName);
- if (tdCss === '') {
- if (rowStyles === null)
- rowStyles = document.defaultView.getComputedStyle($row[0], null);
- tdCss = getStyle(rowStyles, stylePropertyName);
- }
- if (tdCss !== '' && tdCss !== '0px none rgb(0, 0, 0)' && tdCss !== 'rgba(0, 0, 0, 0)') {
- tdStyle += (tdStyle === '') ? 'style="' : ';';
- tdStyle += stylePropertyName + ':' + tdCss;
- }
- }
- }
- trData += '<td';
- if (tdStyle !== '')
- trData += ' ' + tdStyle + '"';
- const tdColspan = getColspan(cell);
- if (tdColspan > 0)
- trData += ' colspan="' + tdColspan + '"';
- const tdRowspan = getRowspan(cell);
- if (tdRowspan > 0)
- trData += ' rowspan="' + tdRowspan + '"';
- if (typeof tdValue === 'string' && tdValue !== '') {
- tdValue = preventInjection(tdValue);
- tdValue = tdValue.replace(/\n/g, '<br>');
- }
- trData += '>' + tdValue + '</td>';
- }
- });
- if (trData.length > 0)
- docData += '<tr>' + trData + '</tr>';
- rowIndex++;
- });
- if (defaults.displayTableName)
- docData += '<tr><td></td></tr><tr><td></td></tr><tr><td>' + parseString($('<p>' + defaults.tableName + '</p>')) + '</td></tr>';
- docData += '</tbody></table>';
- });
- //noinspection XmlUnusedNamespaceDeclaration
- let docFile = '<html xmlns:o="urn:schemas-microsoft-com:office:office" ' + MSDocSchema + ' xmlns="http://www.w3.org/TR/REC-html40">';
- docFile += '<head>';
- docFile += '<meta http-equiv="content-type" content="application/vnd.ms-' + MSDocType + '; charset=UTF-8">';
- if (MSDocType === 'excel') {
- docFile += '<!--[if gte mso 9]>';
- docFile += '<xml>';
- docFile += '<x:ExcelWorkbook>';
- docFile += '<x:ExcelWorksheets>';
- docFile += '<x:ExcelWorksheet>';
- docFile += '<x:Name>';
- docFile += docName;
- docFile += '</x:Name>';
- docFile += '<x:WorksheetOptions>';
- docFile += '<x:DisplayGridlines/>';
- if (defaults.mso.rtl)
- docFile += '<x:DisplayRightToLeft/>';
- docFile += '</x:WorksheetOptions>';
- docFile += '</x:ExcelWorksheet>';
- docFile += '</x:ExcelWorksheets>';
- docFile += '</x:ExcelWorkbook>';
- docFile += '</xml>';
- docFile += '<![endif]-->';
- }
- docFile += '<style>';
- docFile += '@page { size:' + defaults.mso.pageOrientation + '; mso-page-orientation:' + defaults.mso.pageOrientation + '; }';
- docFile += '@page Section1 {size:' + pageFormats[defaults.mso.pageFormat][0] + 'pt ' + pageFormats[defaults.mso.pageFormat][1] + 'pt';
- docFile += '; margin:1.0in 1.25in 1.0in 1.25in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}';
- docFile += 'div.Section1 {page:Section1;}';
- docFile += '@page Section2 {size:' + pageFormats[defaults.mso.pageFormat][1] + 'pt ' + pageFormats[defaults.mso.pageFormat][0] + 'pt';
- docFile += ';mso-page-orientation:' + defaults.mso.pageOrientation + ';margin:1.25in 1.0in 1.25in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}';
- docFile += 'div.Section2 {page:Section2;}';
- docFile += 'br {mso-data-placement:same-cell;}';
- docFile += '</style>';
- docFile += '</head>';
- docFile += '<body>';
- docFile += '<div class="Section' + ((defaults.mso.pageOrientation === 'landscape') ? '2' : '1') + '">';
- docFile += docData;
- docFile += '</div>';
- docFile += '</body>';
- docFile += '</html>';
- if (defaults.outputMode === 'string')
- return docFile;
- if (defaults.outputMode === 'base64')
- return base64encode(docFile);
- saveToFile(docFile, defaults.fileName + '.' + MSDocExt, 'application/vnd.ms-' + MSDocType, '', 'base64', false);
- } else if (defaults.type === 'png') {
- html2canvas($(el)[0]).then(
- function (canvas) {
- const image = canvas.toDataURL();
- const byteString = atob(image.substring(22)); // remove data stuff
- const buffer = new ArrayBuffer(byteString.length);
- const intArray = new Uint8Array(buffer);
- for (let i = 0; i < byteString.length; i++)
- intArray[i] = byteString.charCodeAt(i);
- if (defaults.outputMode === 'string')
- return byteString;
- if (defaults.outputMode === 'base64')
- return base64encode(image);
- if (defaults.outputMode === 'window') {
- window.open(image);
- return;
- }
- saveToFile(buffer, defaults.fileName + '.png', 'image/png', '', '', false);
- });
- } else if (defaults.type === 'pdf') {
- if (defaults.pdfmake.enabled === true) {
- // pdf output using pdfmake
- // https://github.com/bpampuch/pdfmake
- const docDefinition = {
- content: []
- };
- $.extend(true, docDefinition, defaults.pdfmake.docDefinition);
- ranges = [];
- $(el).filter(function () {
- return isVisible($(this));
- }).each(function () {
- const $table = $(this);
- const widths = [];
- const body = [];
- rowIndex = 0;
- /**
- * @return {number}
- */
- const CollectPdfmakeData = function ($rows, colselector, length) {
- let rLength = 0;
- $($rows).each(function () {
- const r = [];
- ForEachVisibleCell(this, colselector, rowIndex, length,
- function (cell, row, col) {
- let cellContent;
- if (typeof cell !== 'undefined' && cell !== null) {
- const colspan = getColspan(cell);
- const rowspan = getRowspan(cell);
- cellContent = {text: parseString(cell, row, col) || ' '};
- if (colspan > 1 || rowspan > 1) {
- cellContent['colSpan'] = colspan || 1;
- cellContent['rowSpan'] = rowspan || 1;
- }
- } else
- cellContent = {text: ' '};
- if (colselector.indexOf('th') >= 0)
- cellContent['style'] = 'header';
- r.push(cellContent);
- });
- if (r.length)
- body.push(r);
- if (rLength < r.length)
- rLength = r.length;
- rowIndex++;
- });
- return rLength;
- };
- $head_rows = collectHeadRows($table);
- let colcount = CollectPdfmakeData($head_rows, 'th,td', $head_rows.length);
- for (let i = widths.length; i < colcount; i++)
- widths.push('*');
- // Data
- $rows = collectRows($table);
- colcount = CollectPdfmakeData($rows, 'td', $head_rows.length + $rows.length);
- for (let i = widths.length; i < colcount; i++)
- widths.push('*');
- docDefinition.content.push({ table: {
- headerRows: $head_rows.length ? $head_rows.length : null,
- widths: widths,
- body: body
- },
- layout: {
- layout: 'noBorders',
- hLineStyle: function (i, node) { return 0; },
- vLineWidth: function (i, node) { return 0; },
- hLineColor: function (i, node) { return i < node.table.headerRows ?
- defaults.pdfmake.docDefinition.styles.header.background :
- defaults.pdfmake.docDefinition.styles.alternateRow.fillColor; },
- vLineColor: function (i, node) { return i < node.table.headerRows ?
- defaults.pdfmake.docDefinition.styles.header.background :
- defaults.pdfmake.docDefinition.styles.alternateRow.fillColor; },
- fillColor: function (rowIndex, node, columnIndex) { return (rowIndex % 2 === 0) ?
- defaults.pdfmake.docDefinition.styles.alternateRow.fillColor :
- null; }
- },
- pageBreak: docDefinition.content.length ? "before" : undefined
- });
- }); // ...for each table
- if (typeof pdfMake !== 'undefined' && typeof pdfMake.createPdf !== 'undefined') {
- pdfMake.fonts = {
- Roboto: {
- normal: 'Roboto-Regular.ttf',
- bold: 'Roboto-Medium.ttf',
- italics: 'Roboto-Italic.ttf',
- bolditalics: 'Roboto-MediumItalic.ttf'
- }
- };
- // pdfmake >= 0.2.0 - replace pdfMake.vfs with pdfMake.virtualfs
- if (pdfMake.vfs.hasOwnProperty ('Mirza-Regular.ttf')) {
- docDefinition.defaultStyle.font = 'Mirza';
- $.extend(true, pdfMake.fonts, {Mirza: {normal: 'Mirza-Regular.ttf',
- bold: 'Mirza-Bold.ttf',
- italics: 'Mirza-Medium.ttf',
- bolditalics: 'Mirza-SemiBold.ttf'
- }});
- }
- else if (pdfMake.vfs.hasOwnProperty ('gbsn00lp.ttf')) {
- docDefinition.defaultStyle.font = 'gbsn00lp';
- $.extend(true, pdfMake.fonts, {gbsn00lp: {normal: 'gbsn00lp.ttf',
- bold: 'gbsn00lp.ttf',
- italics: 'gbsn00lp.ttf',
- bolditalics: 'gbsn00lp.ttf'
- }});
- }
- else if (pdfMake.vfs.hasOwnProperty ('ZCOOLXiaoWei-Regular.ttf')) {
- docDefinition.defaultStyle.font = 'ZCOOLXiaoWei';
- $.extend(true, pdfMake.fonts, {ZCOOLXiaoWei: {normal: 'ZCOOLXiaoWei-Regular.ttf',
- bold: 'ZCOOLXiaoWei-Regular.ttf',
- italics: 'ZCOOLXiaoWei-Regular.ttf',
- bolditalics: 'ZCOOLXiaoWei-Regular.ttf'
- }});
- }
- $.extend(true, pdfMake.fonts, defaults.pdfmake.fonts);
- // pdfmake <= 0.1.71
- pdfMake.createPdf(docDefinition).getBuffer(function (buffer) {
- saveToFile(buffer, defaults.fileName + '.pdf', 'application/pdf', '', '', false);
- });
- // pdfmake >= 0.2.0 - replace above code with:
- //pdfMake.createPdf(docDefinition).download(defaults.fileName);
- }
- } else if (defaults.jspdf.autotable === false) {
- // pdf output using jsPDF's core html support
- let doc = new jspdf.jsPDF({orientation: defaults.jspdf.orientation,
- unit: defaults.jspdf.unit,
- format: defaults.jspdf.format});
- doc.html(el[0], {
- callback: function () {
- jsPdfOutput(doc, false);
- },
- html2canvas: {scale: ((doc.internal.pageSize.width - defaults.jspdf.margins.left * 2) / el[0].scrollWidth)},
- x: defaults.jspdf.margins.left,
- y: defaults.jspdf.margins.top
- /*
- margin: [
- defaults.jspdf.margins.left,
- defaults.jspdf.margins.top,
- getPropertyUnitValue($(el).first().get(0), 'width', 'mm'),
- getPropertyUnitValue($(el).first().get(0), 'height', 'mm')
- ]
- */
- });
- } else {
- // pdf output using jsPDF AutoTable plugin
- // https://github.com/simonbengtsson/jsPDF-AutoTable
- let teOptions = defaults.jspdf.autotable.tableExport;
- // When setting jspdf.format to 'bestfit' tableExport tries to choose
- // the minimum required paper format and orientation in which the table
- // (or tables in multitable mode) completely fits without column adjustment
- if (typeof defaults.jspdf.format === 'string' && defaults.jspdf.format.toLowerCase() === 'bestfit') {
- let rk = '', ro = '';
- let mw = 0;
- $(el).each(function () {
- if (isVisible($(this))) {
- const w = getPropertyUnitValue($(this).get(0), 'width', 'pt');
- if (w > mw) {
- if (w > pageFormats.a0[0]) {
- rk = 'a0';
- ro = 'l';
- }
- for (let key in pageFormats) {
- if (pageFormats.hasOwnProperty(key)) {
- if (pageFormats[key][1] > w) {
- rk = key;
- ro = 'l';
- if (pageFormats[key][0] > w)
- ro = 'p';
- }
- }
- }
- mw = w;
- }
- }
- });
- defaults.jspdf.format = (rk === '' ? 'a4' : rk);
- defaults.jspdf.orientation = (ro === '' ? 'w' : ro);
- }
- // The jsPDF doc object is stored in defaults.jspdf.autotable.tableExport,
- // thus it can be accessed from any callback function
- if (teOptions.doc == null) {
- teOptions.doc = new jspdf.jsPDF(defaults.jspdf.orientation,
- defaults.jspdf.unit,
- defaults.jspdf.format);
- teOptions.wScaleFactor = 1;
- teOptions.hScaleFactor = 1;
- if (typeof defaults.jspdf.onDocCreated === 'function')
- defaults.jspdf.onDocCreated(teOptions.doc);
- }
- jsPdfDefaultStyles.fontName = teOptions.doc.getFont().fontName;
- if (teOptions.outputImages === true)
- teOptions.images = {};
- if (typeof teOptions.images !== 'undefined') {
- $(el).filter(function () {
- return isVisible($(this));
- }).each(function () {
- let rowCount = 0;
- ranges = [];
- if (defaults.exportHiddenCells === false) {
- $hiddenTableElements = $(this).find('tr, th, td').filter(':hidden');
- checkCellVisibility = $hiddenTableElements.length > 0;
- }
- $head_rows = collectHeadRows($(this));
- $rows = collectRows($(this));
- $($rows).each(function () {
- ForEachVisibleCell(this, 'td,th', $head_rows.length + rowCount, $head_rows.length + $rows.length,
- function (cell) {
- collectImages(cell, $(cell).children(), teOptions);
- });
- rowCount++;
- });
- });
- $head_rows = [];
- $rows = [];
- }
- loadImages(teOptions, function () {
- $(el).filter(function () {
- return isVisible($(this));
- }).each(function () {
- let colKey;
- rowIndex = 0;
- ranges = [];
- if (defaults.exportHiddenCells === false) {
- $hiddenTableElements = $(this).find('tr, th, td').filter(':hidden');
- checkCellVisibility = $hiddenTableElements.length > 0;
- }
- colNames = GetColumnNames(this);
- teOptions.columns = [];
- teOptions.rows = [];
- teOptions.teCells = {};
- // onTable: optional callback function for every matching table that can be used
- // to modify the tableExport options or to skip the output of a particular table
- // if the table selector targets multiple tables
- if (typeof teOptions.onTable === 'function')
- if (teOptions.onTable($(this), defaults) === false)
- return true; // continue to next iteration step (table)
- // each table works with an own copy of AutoTable options
- defaults.jspdf.autotable.tableExport = null; // avoid deep recursion error
- const atOptions = $.extend(true, {}, defaults.jspdf.autotable);
- defaults.jspdf.autotable.tableExport = teOptions;
- atOptions.margin = {};
- $.extend(true, atOptions.margin, defaults.jspdf.margins);
- atOptions.tableExport = teOptions;
- if (typeof atOptions.createdHeaderCell !== 'function') {
- // apply some original css styles to pdf header cells
- atOptions.createdHeaderCell = function (cell, data) {
- if (typeof teOptions.columns [data.column.dataKey] !== 'undefined') {
- const col = teOptions.columns [data.column.dataKey];
- if (typeof col.rect !== 'undefined') {
- let rh;
- cell.contentWidth = col.rect.width;
- if (typeof teOptions.heightRatio === 'undefined' || teOptions.heightRatio === 0) {
- if (data.row.raw [data.column.dataKey].rowspan)
- rh = data.row.raw [data.column.dataKey].rect.height / data.row.raw [data.column.dataKey].rowspan;
- else
- rh = data.row.raw [data.column.dataKey].rect.height;
- teOptions.heightRatio = cell.styles.rowHeight / rh;
- }
- rh = data.row.raw [data.column.dataKey].rect.height * teOptions.heightRatio;
- if (rh > cell.styles.rowHeight)
- cell.styles.rowHeight = rh;
- }
- cell.styles.halign = (atOptions.headerStyles.halign === 'inherit') ? 'center' : atOptions.headerStyles.halign;
- cell.styles.valign = atOptions.headerStyles.valign;
- if (typeof col.style !== 'undefined' && col.style.hidden !== true) {
- if (atOptions.headerStyles.halign === 'inherit')
- cell.styles.halign = col.style.align;
- if (atOptions.styles.fillColor === 'inherit')
- cell.styles.fillColor = col.style.bcolor;
- if (atOptions.styles.textColor === 'inherit')
- cell.styles.textColor = col.style.color;
- if (atOptions.styles.fontStyle === 'inherit')
- cell.styles.fontStyle = col.style.fstyle;
- }
- }
- };
- }
- if (typeof atOptions.createdCell !== 'function') {
- // apply some original css styles to pdf table cells
- atOptions.createdCell = function (cell, data) {
- const tecell = teOptions.teCells [data.row.index + ':' + data.column.dataKey];
- cell.styles.halign = (atOptions.styles.halign === 'inherit') ? 'center' : atOptions.styles.halign;
- cell.styles.valign = atOptions.styles.valign;
- if (typeof tecell !== 'undefined' && typeof tecell.style !== 'undefined' && tecell.style.hidden !== true) {
- if (atOptions.styles.halign === 'inherit')
- cell.styles.halign = tecell.style.align;
- if (atOptions.styles.fillColor === 'inherit')
- cell.styles.fillColor = tecell.style.bcolor;
- if (atOptions.styles.textColor === 'inherit')
- cell.styles.textColor = tecell.style.color;
- if (atOptions.styles.fontStyle === 'inherit')
- cell.styles.fontStyle = tecell.style.fstyle;
- }
- };
- }
- if (typeof atOptions.drawHeaderCell !== 'function') {
- atOptions.drawHeaderCell = function (cell, data) {
- const colopt = teOptions.columns [data.column.dataKey];
- if ((colopt.style.hasOwnProperty('hidden') !== true || colopt.style.hidden !== true) &&
- colopt.rowIndex >= 0)
- return prepareAutoTableText(cell, data, colopt);
- else
- return false; // cell is hidden
- };
- }
- if (typeof atOptions.drawCell !== 'function') {
- atOptions.drawCell = function (cell, data) {
- const teCell = teOptions.teCells [data.row.index + ':' + data.column.dataKey];
- const draw2canvas = (typeof teCell !== 'undefined' && teCell.isCanvas);
- if (draw2canvas !== true) {
- if (prepareAutoTableText(cell, data, teCell)) {
- teOptions.doc.rect(cell.x, cell.y, cell.width, cell.height, cell.styles.fillStyle);
- if (typeof teCell !== 'undefined' &&
- (typeof teCell.hasUserDefText === 'undefined' || teCell.hasUserDefText !== true) &&
- typeof teCell.elements !== 'undefined' && teCell.elements.length) {
- const hScale = cell.height / teCell.rect.height;
- if (hScale > teOptions.hScaleFactor)
- teOptions.hScaleFactor = hScale;
- teOptions.wScaleFactor = cell.width / teCell.rect.width;
- const ySave = cell.textPos.y;
- drawAutotableElements(cell, teCell.elements, teOptions);
- cell.textPos.y = ySave;
- drawAutotableText(cell, teCell.elements, teOptions);
- } else
- drawAutotableText(cell, {}, teOptions);
- }
- } else {
- const container = teCell.elements[0];
- const imgId = $(container).attr('data-tableexport-canvas');
- const r = container.getBoundingClientRect();
- cell.width = r.width * teOptions.wScaleFactor;
- cell.height = r.height * teOptions.hScaleFactor;
- data.row.height = cell.height;
- jsPdfDrawImage(cell, container, imgId, teOptions);
- }
- return false;
- };
- }
- // collect header and data rows
- teOptions.headerrows = [];
- $head_rows = collectHeadRows($(this));
- $($head_rows).each(function () {
- colKey = 0;
- teOptions.headerrows[rowIndex] = [];
- ForEachVisibleCell(this, 'th,td', rowIndex, $head_rows.length,
- function (cell, row, col) {
- const obj = getCellStyles(cell);
- obj.title = parseString(cell, row, col);
- obj.key = colKey++;
- obj.rowIndex = rowIndex;
- teOptions.headerrows[rowIndex].push(obj);
- });
- rowIndex++;
- });
- if (rowIndex > 0) {
- // iterate through last row
- let lastrow = rowIndex - 1;
- while (lastrow >= 0) {
- $.each(teOptions.headerrows[lastrow], function () {
- let obj = this;
- if (lastrow > 0 && this.rect === null)
- obj = teOptions.headerrows[lastrow - 1][this.key];
- if (obj !== null && obj.rowIndex >= 0 &&
- (obj.style.hasOwnProperty('hidden') !== true || obj.style.hidden !== true))
- teOptions.columns.push(obj);
- });
- lastrow = (teOptions.columns.length > 0) ? -1 : lastrow - 1;
- }
- }
- let rowCount = 0;
- $rows = [];
- $rows = collectRows($(this));
- $($rows).each(function () {
- const rowData = [];
- colKey = 0;
- ForEachVisibleCell(this, 'td,th', rowIndex, $head_rows.length + $rows.length,
- function (cell, row, col) {
- let obj;
- if (typeof teOptions.columns[colKey] === 'undefined') {
- // jsPDF-Autotable needs columns. Thus define hidden ones for tables without thead
- obj = {
- title: '',
- key: colKey,
- style: {
- hidden: true
- }
- };
- teOptions.columns.push(obj);
- }
- rowData.push(parseString(cell, row, col));
- if (typeof cell !== 'undefined' && cell !== null) {
- obj = getCellStyles(cell);
- obj.isCanvas = cell.hasAttribute('data-tableexport-canvas');
- obj.elements = obj.isCanvas ? $(cell) : $(cell).children();
- if(typeof $(cell).data('teUserDefText') !== 'undefined')
- obj.hasUserDefText = true;
- teOptions.teCells [rowCount + ':' + colKey++] = obj;
- } else {
- obj = $.extend(true, {}, teOptions.teCells [rowCount + ':' + (colKey - 1)]);
- obj.colspan = -1;
- teOptions.teCells [rowCount + ':' + colKey++] = obj;
- }
- });
- if (rowData.length) {
- teOptions.rows.push(rowData);
- rowCount++;
- }
- rowIndex++;
- });
- // onBeforeAutotable: optional callback function before calling
- // jsPDF AutoTable that can be used to modify the AutoTable options
- if (typeof teOptions.onBeforeAutotable === 'function')
- teOptions.onBeforeAutotable($(this), teOptions.columns, teOptions.rows, atOptions);
- jsPdfAutoTable(atOptions.tableExport.doc, teOptions.columns, teOptions.rows, atOptions);
- // onAfterAutotable: optional callback function after returning
- // from jsPDF AutoTable that can be used to modify the AutoTable options
- if (typeof teOptions.onAfterAutotable === 'function')
- teOptions.onAfterAutotable($(this), atOptions);
- // set the start position for the next table (in case there is one)
- defaults.jspdf.autotable.startY = jsPdfAutoTableEndPosY() + atOptions.margin.top;
- });
- jsPdfOutput(teOptions.doc, (typeof teOptions.images !== 'undefined' && jQuery.isEmptyObject(teOptions.images) === false));
- if (typeof teOptions.headerrows !== 'undefined')
- teOptions.headerrows.length = 0;
- if (typeof teOptions.columns !== 'undefined')
- teOptions.columns.length = 0;
- if (typeof teOptions.rows !== 'undefined')
- teOptions.rows.length = 0;
- delete teOptions.doc;
- teOptions.doc = null;
- });
- }
- }
- function collectHeadRows ($table) {
- const result = [];
- findTableElements($table, 'thead').each(function () {
- result.push.apply(result, findTableElements($(this), defaults.theadSelector).toArray());
- });
- return result;
- }
- function collectRows ($table) {
- const result = [];
- findTableElements($table, 'tbody').each(function () {
- result.push.apply(result, findTableElements($(this), defaults.tbodySelector).toArray());
- });
- if (defaults.tfootSelector.length) {
- findTableElements($table, 'tfoot').each(function () {
- result.push.apply(result, findTableElements($(this), defaults.tfootSelector).toArray());
- });
- }
- return result;
- }
- function findTableElements ($parent, selector) {
- const parentSelector = $parent[0].tagName;
- const parentLevel = $parent.parents(parentSelector).length;
- return $parent.find(selector).filter(function () {
- return parentLevel === $(this).closest(parentSelector).parents(parentSelector).length;
- });
- }
- function GetColumnNames (table) {
- const result = [];
- let maxCols = 0;
- let row = 0;
- let col = 0;
- $(table).find('thead').first().find('th').each(function (index, el) {
- const hasDataField = $(el).attr('data-field') !== undefined;
- if (typeof el.parentNode.rowIndex !== 'undefined' && row !== el.parentNode.rowIndex) {
- row = el.parentNode.rowIndex;
- col = 0;
- maxCols = 0;
- }
- const colSpan = getColspan(el);
- maxCols += (colSpan ? colSpan : 1);
- while (col < maxCols) {
- result[col] = (hasDataField ? $(el).attr('data-field') : col.toString());
- col++;
- }
- });
- return result;
- }
- function isVisible ($element) {
- let isRow = typeof $element[0].rowIndex !== 'undefined';
- const isCell = isRow === false && typeof $element[0].cellIndex !== 'undefined';
- const isElementVisible = (isCell || isRow) ? isTableElementVisible($element) : $element.is(':visible');
- let tableexportDisplay = $element.attr('data-tableexport-display');
- if (isCell && tableexportDisplay !== 'none' && tableexportDisplay !== 'always') {
- $element = $($element[0].parentNode);
- isRow = typeof $element[0].rowIndex !== 'undefined';
- tableexportDisplay = $element.attr('data-tableexport-display');
- }
- if (isRow && tableexportDisplay !== 'none' && tableexportDisplay !== 'always') {
- tableexportDisplay = $element.closest('table').attr('data-tableexport-display');
- }
- return tableexportDisplay !== 'none' && (isElementVisible === true || tableexportDisplay === 'always');
- }
- function isTableElementVisible ($element) {
- let hiddenEls = [];
- if (checkCellVisibility) {
- hiddenEls = $hiddenTableElements.filter(function () {
- let found = false;
- if (this.nodeType === $element[0].nodeType) {
- if (typeof this.rowIndex !== 'undefined' && this.rowIndex === $element[0].rowIndex)
- found = true;
- else if (typeof this.cellIndex !== 'undefined' && this.cellIndex === $element[0].cellIndex &&
- typeof this.parentNode.rowIndex !== 'undefined' &&
- typeof $element[0].parentNode.rowIndex !== 'undefined' &&
- this.parentNode.rowIndex === $element[0].parentNode.rowIndex)
- found = true;
- }
- return found;
- });
- }
- return (checkCellVisibility === false || hiddenEls.length === 0);
- }
- function isColumnIgnored ($cell, rowLength, colIndex) {
- let result = false;
- if (isVisible($cell)) {
- if (defaults.ignoreColumn.length > 0) {
- if ($.inArray(colIndex, defaults.ignoreColumn) !== -1 ||
- $.inArray(colIndex - rowLength, defaults.ignoreColumn) !== -1 ||
- (colNames.length > colIndex && typeof colNames[colIndex] !== 'undefined' &&
- $.inArray(colNames[colIndex], defaults.ignoreColumn) !== -1))
- result = true;
- }
- } else
- result = true;
- return result;
- }
- function ForEachVisibleCell (tableRow, selector, rowIndex, rowCount, cellcallback) {
- if (typeof (cellcallback) === 'function') {
- let ignoreRow = false;
- if (typeof defaults.onIgnoreRow === 'function')
- ignoreRow = defaults.onIgnoreRow($(tableRow), rowIndex);
- if (ignoreRow === false &&
- (defaults.ignoreRow.length === 0 ||
- ($.inArray(rowIndex, defaults.ignoreRow) === -1 &&
- $.inArray(rowIndex - rowCount, defaults.ignoreRow) === -1)) &&
- isVisible($(tableRow))) {
- const $cells = findTableElements($(tableRow), selector);
- let cellsCount = $cells.length;
- let colCount = 0;
- let colIndex = 0;
- $cells.each(function () {
- const $cell = $(this);
- let colspan = getColspan(this);
- let rowspan = getRowspan(this);
- let c;
- // Skip ranges
- $.each(ranges, function () {
- const range = this;
- if (rowIndex > range.s.r && rowIndex <= range.e.r && colCount >= range.s.c && colCount <= range.e.c) {
- for (c = 0; c <= range.e.c - range.s.c; ++c) {
- cellsCount++;
- colIndex++;
- cellcallback(null, rowIndex, colCount++);
- }
- }
- });
- // Handle span's
- if (rowspan || colspan) {
- rowspan = rowspan || 1;
- colspan = colspan || 1;
- ranges.push({
- s: {r: rowIndex, c: colCount},
- e: {r: rowIndex + rowspan - 1, c: colCount + colspan - 1}
- });
- }
- if (isColumnIgnored($cell, cellsCount, colIndex++) === false) {
- // Handle value
- cellcallback(this, rowIndex, colCount++);
- }
- // Handle colspan
- if (colspan > 1) {
- for (c = 0; c < colspan - 1; ++c) {
- colIndex++;
- cellcallback(null, rowIndex, colCount++);
- }
- }
- });
- // Skip ranges
- $.each(ranges, function () {
- const range = this;
- if (rowIndex >= range.s.r && rowIndex <= range.e.r && colCount >= range.s.c && colCount <= range.e.c) {
- for (let c = 0; c <= range.e.c - range.s.c; ++c) {
- cellcallback(null, rowIndex, colCount++);
- }
- }
- });
- }
- }
- }
- function jsPdfDrawImage (cell, container, imgId, teOptions) {
- if (typeof teOptions.images !== 'undefined') {
- const image = teOptions.images[imgId];
- if (typeof image !== 'undefined') {
- const r = container.getBoundingClientRect();
- const arCell = cell.width / cell.height;
- const arImg = r.width / r.height;
- let imgWidth = cell.width;
- let imgHeight = cell.height;
- const px2pt = 0.264583 * 72 / 25.4;
- let uy = 0;
- if (arImg <= arCell) {
- imgHeight = Math.min(cell.height, r.height);
- imgWidth = r.width * imgHeight / r.height;
- } else if (arImg > arCell) {
- imgWidth = Math.min(cell.width, r.width);
- imgHeight = r.height * imgWidth / r.width;
- }
- imgWidth *= px2pt;
- imgHeight *= px2pt;
- if (imgHeight < cell.height)
- uy = (cell.height - imgHeight) / 2;
- try {
- teOptions.doc.addImage(image.src, cell.textPos.x, cell.y + uy, imgWidth, imgHeight);
- } catch (e) {
- // TODO: IE -> convert png to jpeg
- }
- cell.textPos.x += imgWidth;
- }
- }
- }
- function jsPdfOutput (doc, hasimages) {
- if (defaults.outputMode === 'string')
- return doc.output();
- if (defaults.outputMode === 'base64')
- return base64encode(doc.output());
- if (defaults.outputMode === 'window') {
- window.URL = window.URL || window.webkitURL;
- window.open(window.URL.createObjectURL(doc.output('blob')));
- return;
- }
- const fileName = defaults.fileName + '.pdf';
- try {
- const blob = doc.output('blob')
- saveAs(blob, fileName);
- if (typeof defaults.onAfterSaveToFile === 'function')
- defaults.onAfterSaveToFile(blob, fileName);
- } catch (e) {
- downloadFile(fileName,
- 'data:application/pdf' + (hasimages ? '' : ';base64') + ',',
- hasimages ? doc.output('blob') : doc.output());
- }
- }
- function prepareAutoTableText (cell, data, cellopt) {
- let cs = 0
- if (typeof cellopt !== 'undefined')
- cs = cellopt.colspan;
- if (cs >= 0) {
- // colspan handling
- let cellWidth = cell.width
- let textPosX = cell.textPos.x
- const i = data.table.columns.indexOf(data.column)
- for (let c = 1; c < cs; c++) {
- const column = data.table.columns[i + c]
- cellWidth += column.width;
- }
- if (cs > 1) {
- if (cell.styles.halign === 'right')
- textPosX = cell.textPos.x + cellWidth - cell.width;
- else if (cell.styles.halign === 'center')
- textPosX = cell.textPos.x + (cellWidth - cell.width) / 2;
- }
- cell.width = cellWidth;
- cell.textPos.x = textPosX;
- if (typeof cellopt !== 'undefined' && cellopt.rowspan > 1)
- cell.height = cell.height * cellopt.rowspan;
- // fix jsPDF's calculation of text position
- if (cell.styles.valign === 'middle' || cell.styles.valign === 'bottom') {
- const splittedText = typeof cell.text === 'string' ? cell.text.split(/\r\n|\r|\n/g) : cell.text;
- const lineCount = splittedText.length || 1;
- if (lineCount > 2)
- cell.textPos.y -= ((2 - FONT_ROW_RATIO) / 2 * data.row.styles.fontSize) * (lineCount - 2) / 3;
- }
- return true;
- } else
- return false; // cell is hidden (colspan = -1), don't draw it
- }
- function collectImages (cell, elements, teOptions) {
- if (typeof cell !== 'undefined' && cell !== null) {
- if (cell.hasAttribute('data-tableexport-canvas')) {
- const imgId = new Date().getTime();
- $(cell).attr('data-tableexport-canvas', imgId);
- teOptions.images[imgId] = {
- url: '[data-tableexport-canvas="' + imgId + '"]',
- src: null
- };
- } else if (elements !== 'undefined' && elements != null) {
- elements.each(function () {
- if ($(this).is('img')) {
- const imgId = strHashCode(this.src);
- teOptions.images[imgId] = {
- url: this.src,
- src: this.src
- };
- }
- collectImages(cell, $(this).children(), teOptions);
- });
- }
- }
- }
- function loadImages (teOptions, callback) {
- let imageCount = 0;
- let pendingCount = 0;
- function done () {
- callback(imageCount);
- }
- function loadImage (image) {
- if (image.url) {
- if (!image.src) {
- const $imgContainer = $(image.url);
- if ($imgContainer.length) {
- imageCount = ++pendingCount;
- html2canvas($imgContainer[0]).then(function (canvas) {
- image.src = canvas.toDataURL('image/png');
- if (!--pendingCount)
- done();
- });
- }
- } else {
- const img = new Image();
- imageCount = ++pendingCount;
- img.crossOrigin = 'Anonymous';
- img.onerror = img.onload = function () {
- if (img.complete) {
- if (img.src.indexOf('data:image/') === 0) {
- img.width = image.width || img.width || 0;
- img.height = image.height || img.height || 0;
- }
- if (img.width + img.height) {
- const canvas = document.createElement('canvas');
- const ctx = canvas.getContext('2d');
- canvas.width = img.width;
- canvas.height = img.height;
- ctx.drawImage(img, 0, 0);
- image.src = canvas.toDataURL('image/png');
- }
- }
- if (!--pendingCount)
- done();
- };
- img.src = image.url;
- }
- }
- }
- if (typeof teOptions.images !== 'undefined') {
- for (let i in teOptions.images)
- if (teOptions.images.hasOwnProperty(i))
- loadImage(teOptions.images[i]);
- }
- return pendingCount || done();
- }
- function drawAutotableElements (cell, elements, teOptions) {
- elements.each(function () {
- if ($(this).is('div')) {
- const bColor = rgb2array(getStyle(this, 'background-color'), [255, 255, 255]);
- const lColor = rgb2array(getStyle(this, 'border-top-color'), [0, 0, 0]);
- const lWidth = getPropertyUnitValue(this, 'border-top-width', defaults.jspdf.unit);
- const r = this.getBoundingClientRect();
- const ux = this.offsetLeft * teOptions.wScaleFactor;
- const uy = this.offsetTop * teOptions.hScaleFactor;
- const uw = r.width * teOptions.wScaleFactor;
- const uh = r.height * teOptions.hScaleFactor;
- teOptions.doc.setDrawColor.apply(undefined, lColor);
- teOptions.doc.setFillColor.apply(undefined, bColor);
- teOptions.doc.setLineWidth(lWidth);
- teOptions.doc.rect(cell.x + ux, cell.y + uy, uw, uh, lWidth ? 'FD' : 'F');
- } else if ($(this).is('img')) {
- const imgId = strHashCode(this.src);
- jsPdfDrawImage(cell, this, imgId, teOptions);
- }
- drawAutotableElements(cell, $(this).children(), teOptions);
- });
- }
- function drawAutotableText (cell, texttags, teOptions) {
- if (typeof teOptions.onAutotableText === 'function') {
- teOptions.onAutotableText(teOptions.doc, cell, texttags);
- } else {
- let x = cell.textPos.x;
- let y = cell.textPos.y;
- const style = {halign: cell.styles.halign, valign: cell.styles.valign};
- if (texttags.length) {
- let tag = texttags[0];
- while (tag.previousSibling)
- tag = tag.previousSibling;
- let b = false, i = false;
- while (tag) {
- let txt = tag.innerText || tag.textContent || '';
- const leadingSpace = (txt.length && txt[0] === ' ') ? ' ' : '';
- const trailingSpace = (txt.length > 1 && txt[txt.length - 1] === ' ') ? ' ' : '';
- if (defaults.preserve.leadingWS !== true)
- txt = leadingSpace + trimLeft(txt);
- if (defaults.preserve.trailingWS !== true)
- txt = trimRight(txt) + trailingSpace;
- if ($(tag).is('br')) {
- x = cell.textPos.x;
- y += teOptions.doc.internal.getFontSize();
- }
- if ($(tag).is('b'))
- b = true;
- else if ($(tag).is('i'))
- i = true;
- if (b || i)
- teOptions.doc.setFont('undefined ', (b && i) ? 'bolditalic' : b ? 'bold' : 'italic');
- let w = teOptions.doc.getStringUnitWidth(txt) * teOptions.doc.internal.getFontSize();
- if (w) {
- if (cell.styles.overflow === 'linebreak' &&
- x > cell.textPos.x && (x + w) > (cell.textPos.x + cell.width)) {
- const chars = '.,!%*;:=-';
- if (chars.indexOf(txt.charAt(0)) >= 0) {
- const s = txt.charAt(0);
- w = teOptions.doc.getStringUnitWidth(s) * teOptions.doc.internal.getFontSize();
- if ((x + w) <= (cell.textPos.x + cell.width)) {
- jsPdfAutoTableText(s, x, y, style);
- txt = txt.substring(1, txt.length);
- }
- w = teOptions.doc.getStringUnitWidth(txt) * teOptions.doc.internal.getFontSize();
- }
- x = cell.textPos.x;
- y += teOptions.doc.internal.getFontSize();
- }
- if (cell.styles.overflow !== 'visible') {
- while (txt.length && (x + w) > (cell.textPos.x + cell.width)) {
- txt = txt.substring(0, txt.length - 1);
- w = teOptions.doc.getStringUnitWidth(txt) * teOptions.doc.internal.getFontSize();
- }
- }
- jsPdfAutoTableText(txt, x, y, style);
- x += w;
- }
- if (b || i) {
- if ($(tag).is('b'))
- b = false;
- else if ($(tag).is('i'))
- i = false;
- teOptions.doc.setFont('undefined ', (!b && !i) ? 'normal' : b ? 'bold' : 'italic');
- }
- tag = tag.nextSibling;
- }
- cell.textPos.x = x;
- cell.textPos.y = y;
- } else {
- jsPdfAutoTableText(cell.text, cell.textPos.x, cell.textPos.y, style);
- }
- }
- }
- function escapeRegExp (string) {
- return string == null ? '' : string.toString().replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
- }
- function replaceAll (string, find, replace) {
- return string == null ? '' : string.toString().replace(new RegExp(escapeRegExp(find), 'g'), replace);
- }
- function trimLeft (string) {
- return string == null ? '' : string.toString().replace(/^\s+/, '');
- }
- function trimRight (string) {
- return string == null ? '' : string.toString().replace(/\s+$/, '');
- }
- function parseDateUTC (s) {
- if (defaults.date.html.length === 0)
- return false;
- defaults.date.pattern.lastIndex = 0;
- const match = defaults.date.pattern.exec(s);
- if (match == null)
- return false;
- const y = +match[defaults.date.match_y];
- if (y < 0 || y > 8099) return false;
- const m = match[defaults.date.match_m] * 1;
- const d = match[defaults.date.match_d] * 1;
- if (!isFinite(d)) return false;
- const o = new Date(y, m - 1, d, 0, 0, 0);
- if (o.getFullYear() === y && o.getMonth() === (m - 1) && o.getDate() === d)
- return new Date(Date.UTC(y, m - 1, d, 0, 0, 0));
- else
- return false;
- }
- function parseNumber (value) {
- value = value || '0';
- if ('' !== defaults.numbers.html.thousandsSeparator)
- value = replaceAll(value, defaults.numbers.html.thousandsSeparator, '');
- if ('.' !== defaults.numbers.html.decimalMark)
- value = replaceAll(value, defaults.numbers.html.decimalMark, '.');
- return typeof value === 'number' || jQuery.isNumeric(value) !== false ? value : false;
- }
- function parsePercent (value) {
- if (value.indexOf('%') > -1) {
- value = parseNumber(value.replace(/%/g, ''));
- if (value !== false)
- value = value / 100;
- } else
- value = false;
- return value;
- }
- function parseString (cell, rowIndex, colIndex, cellInfo) {
- let result = '';
- let cellType = 'text';
- if (cell !== null) {
- const $cell = $(cell);
- let htmlData;
- $cell.removeData('teUserDefText');
- if ($cell[0].hasAttribute('data-tableexport-canvas')) {
- htmlData = '';
- } else if ($cell[0].hasAttribute('data-tableexport-value')) {
- htmlData = $cell.attr('data-tableexport-value');
- htmlData = htmlData ? htmlData + '' : '';
- $cell.data('teUserDefText', 1);
- } else {
- htmlData = $cell.html();
- if (typeof defaults.onCellHtmlData === 'function') {
- htmlData = defaults.onCellHtmlData($cell, rowIndex, colIndex, htmlData);
- $cell.data('teUserDefText', 1);
- }
- else if (htmlData !== '') {
- const html = $.parseHTML('<div>' + htmlData + '</div>', null, false);
- let inputIndex = 0;
- let selectIndex = 0;
- htmlData = '';
- $.each(html, function () {
- if ($(this).is('input')) {
- htmlData += $cell.find('input').eq(inputIndex++).val();
- }
- else if ($(this).is('select')) {
- htmlData += $cell.find('select option:selected').eq(selectIndex++).text();
- }
- else if ($(this).is('br')) {
- htmlData += '<br>';
- }
- else {
- if (typeof $(this).html() === 'undefined')
- htmlData += $(this).text();
- else if (jQuery().bootstrapTable === undefined ||
- ($(this).hasClass('fht-cell') === false && // BT 4
- $(this).hasClass('filterControl') === false &&
- $cell.parents('.detail-view').length === 0))
- htmlData += $(this).html();
- if ($(this).is('a')) {
- const href = $cell.find('a').attr('href') || '';
- if (typeof defaults.onCellHtmlHyperlink === 'function') {
- result += defaults.onCellHtmlHyperlink($cell, rowIndex, colIndex, href, htmlData);
- }
- else if (defaults.htmlHyperlink === 'href') {
- result += href;
- }
- else { // 'content'
- result += htmlData;
- }
- htmlData = '';
- }
- }
- });
- }
- }
- if (htmlData && htmlData !== '' && defaults.htmlContent === true) {
- result = $.trim(htmlData);
- } else if (htmlData && htmlData !== '') {
- const cellFormat = $cell.attr('data-tableexport-cellformat');
- if (cellFormat !== '') {
- let text = htmlData.replace(/\n/g, '\u2028').replace(/(<\s*br([^>]*)>)/gi, '\u2060');
- const obj = $('<div/>').html(text).contents();
- let number = false;
- text = '';
- $.each(obj.text().split('\u2028'), function (i, v) {
- if (i > 0)
- text += ' ';
- if (defaults.preserve.leadingWS !== true)
- v = trimLeft(v);
- text += (defaults.preserve.trailingWS !== true) ? trimRight(v) : v;
- });
- $.each(text.split('\u2060'), function (i, v) {
- if (i > 0)
- result += '\n';
- if (defaults.preserve.leadingWS !== true)
- v = trimLeft(v);
- if (defaults.preserve.trailingWS !== true)
- v = trimRight(v);
- result += v.replace(/\u00AD/g, ''); // remove soft hyphens
- });
- result = result.replace(/\u00A0/g, ' '); // replace nbsp's with spaces
- if (defaults.type === 'json' ||
- (defaults.type === 'excel' && defaults.mso.fileFormat === 'xmlss') ||
- defaults.numbers.output === false) {
- number = parseNumber(result);
- if (number !== false) {
- cellType = 'number';
- result = Number(number);
- }
- } else if (defaults.numbers.html.decimalMark !== defaults.numbers.output.decimalMark ||
- defaults.numbers.html.thousandsSeparator !== defaults.numbers.output.thousandsSeparator) {
- number = parseNumber(result);
- if (number !== false) {
- const frac = ('' + number.substr(number < 0 ? 1 : 0)).split('.');
- if (frac.length === 1)
- frac[1] = '';
- const mod = frac[0].length > 3 ? frac[0].length % 3 : 0;
- cellType = 'number';
- result = (number < 0 ? '-' : '') +
- (defaults.numbers.output.thousandsSeparator ? ((mod ? frac[0].substr(0, mod) + defaults.numbers.output.thousandsSeparator : '') + frac[0].substr(mod).replace(/(\d{3})(?=\d)/g, '$1' + defaults.numbers.output.thousandsSeparator)) : frac[0]) +
- (frac[1].length ? defaults.numbers.output.decimalMark + frac[1] : '');
- }
- }
- }
- else
- result = htmlData;
- }
- if (defaults.escape === true) {
- //noinspection JSDeprecatedSymbols
- result = escape(result);
- }
- if (typeof defaults.onCellData === 'function') {
- result = defaults.onCellData($cell, rowIndex, colIndex, result, cellType);
- $cell.data('teUserDefText', 1);
- }
- }
- if (cellInfo !== undefined)
- cellInfo.type = cellType;
- return result;
- }
- function preventInjection (str) {
- if (str.length > 0 && defaults.preventInjection === true) {
- const chars = '=+-@';
- if (chars.indexOf(str.charAt(0)) >= 0)
- return ('\'' + str);
- }
- return str;
- }
- //noinspection JSUnusedLocalSymbols
- function hyphenate (a, b, c) {
- return b + '-' + c.toLowerCase();
- }
- function rgb2array (rgb_string, default_result) {
- const re = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/;
- const bits = re.exec(rgb_string);
- let result = default_result;
- if (bits)
- result = [parseInt(bits[1]), parseInt(bits[2]), parseInt(bits[3])];
- return result;
- }
- function getCellStyles (cell) {
- let a = getStyle(cell, 'text-align');
- const fw = getStyle(cell, 'font-weight');
- const fs = getStyle(cell, 'font-style');
- let f = '';
- if (a === 'start')
- a = getStyle(cell, 'direction') === 'rtl' ? 'right' : 'left';
- if (fw >= 700)
- f = 'bold';
- if (fs === 'italic')
- f += fs;
- if (f === '')
- f = 'normal';
- const result = {
- style: {
- align: a,
- bcolor: rgb2array(getStyle(cell, 'background-color'), [255, 255, 255]),
- color: rgb2array(getStyle(cell, 'color'), [0, 0, 0]),
- fstyle: f
- },
- colspan: getColspan(cell),
- rowspan: getRowspan(cell)
- };
- if (cell !== null) {
- const r = cell.getBoundingClientRect();
- result.rect = {
- width: r.width,
- height: r.height
- };
- }
- return result;
- }
- function getColspan (cell) {
- let result = $(cell).attr('data-tableexport-colspan');
- if (typeof result === 'undefined' && $(cell).is('[colspan]'))
- result = $(cell).attr('colspan');
- return (parseInt(result) || 0);
- }
- function getRowspan (cell) {
- let result = $(cell).attr('data-tableexport-rowspan');
- if (typeof result === 'undefined' && $(cell).is('[rowspan]'))
- result = $(cell).attr('rowspan');
- return (parseInt(result) || 0);
- }
- // get computed style property
- function getStyle (target, prop) {
- try {
- if (window.getComputedStyle) { // gecko and webkit
- prop = prop.replace(/([a-z])([A-Z])/, hyphenate); // requires hyphenated, not camel
- if (typeof target === 'object' && target.nodeType !== undefined)
- return window.getComputedStyle(target, null).getPropertyValue(prop);
- if (typeof target === 'object' && target.length)
- return target.getPropertyValue(prop);
- return '';
- }
- if (target.currentStyle) { // ie
- return target.currentStyle[prop];
- }
- return target.style[prop];
- } catch (e) {
- }
- return '';
- }
- function getUnitValue (parent, value, unit) {
- const baseline = 100; // any number serves
- const temp = document.createElement('div'); // create temporary element
- temp.style.overflow = 'hidden'; // in case baseline is set too low
- temp.style.visibility = 'hidden'; // no need to show it
- parent.appendChild(temp); // insert it into the parent for em, ex and %
- temp.style.width = baseline + unit;
- const factor = baseline / temp.offsetWidth;
- parent.removeChild(temp); // clean up
- return (value * factor);
- }
- function getPropertyUnitValue (target, prop, unit) {
- const value = getStyle(target, prop); // get the computed style value
- let numeric = value.match(/\d+/); // get the numeric component
- if (numeric !== null) {
- numeric = numeric[0]; // get the string
- return getUnitValue(target.parentElement, numeric, unit);
- }
- return 0;
- }
- function xlsxWorkbookToArrayBuffer (s) {
- const buf = new ArrayBuffer(s.length);
- const view = new Uint8Array(buf);
- for (let i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
- return buf;
- }
- function xlsxTableToSheet (table) {
- let ssfId;
- const ws = ({});
- const rows = table.getElementsByTagName('tr');
- const sheetRows = Math.min(10000000, rows.length);
- const range = {s: {r: 0, c: 0}, e: {r: 0, c: 0}};
- let merges = [], midx = 0;
- let _R = 0, R = 0, _C = 0, C = 0, RS = 0, CS = 0;
- let elt;
- const ssfTable = XLSX.SSF.get_table();
- for (; _R < rows.length && R < sheetRows; ++_R) {
- const row = rows[_R];
- let ignoreRow = false;
- if (typeof defaults.onIgnoreRow === 'function')
- ignoreRow = defaults.onIgnoreRow($(row), _R);
- if (ignoreRow === true ||
- (defaults.ignoreRow.length !== 0 &&
- ($.inArray(_R, defaults.ignoreRow) !== -1 ||
- $.inArray(_R - rows.length, defaults.ignoreRow) !== -1)) ||
- isVisible($(row)) === false) {
- continue;
- }
- const elts = (row.children);
- let _CLength = 0;
- for (_C = 0; _C < elts.length; ++_C) {
- elt = elts[_C];
- CS = +getColspan(elt) || 1;
- _CLength += CS;
- }
- let CSOffset = 0;
- for (_C = C = 0; _C < elts.length; ++_C) {
- elt = elts[_C];
- CS = +getColspan(elt) || 1;
- const col = _C + CSOffset;
- if (isColumnIgnored($(elt), _CLength, col + (col < C ? C - col : 0)))
- continue;
- CSOffset += CS - 1;
- for (midx = 0; midx < merges.length; ++midx) {
- const m = merges[midx];
- if (m.s.c == C && m.s.r <= R && R <= m.e.r) {
- C = m.e.c + 1;
- midx = -1;
- }
- }
- if ((RS = +getRowspan(elt)) > 0 || CS > 1)
- merges.push({s: {r: R, c: C}, e: {r: R + (RS || 1) - 1, c: C + CS - 1}});
- const cellInfo = {type: ''};
- let v = parseString(elt, _R, _C + CSOffset, cellInfo);
- let o = {t: 's', v: v};
- let _t = '';
- const cellFormat = $(elt).attr('data-tableexport-cellformat') || undefined;
- if (cellFormat !== '') {
- ssfId = parseInt($(elt).attr('data-tableexport-xlsxformatid') || 0);
- if (ssfId === 0 &&
- typeof defaults.mso.xlsx.formatId.numbers === 'function')
- ssfId = defaults.mso.xlsx.formatId.numbers($(elt), _R, _C + CSOffset);
- if (ssfId === 0 &&
- typeof defaults.mso.xlsx.formatId.date === 'function')
- ssfId = defaults.mso.xlsx.formatId.date($(elt), _R, _C + CSOffset);
- if (ssfId === 49 || ssfId === '@')
- _t = 's';
- else if (cellInfo.type === 'number' ||
- (ssfId > 0 && ssfId < 14) || (ssfId > 36 && ssfId < 41) || ssfId === 48)
- _t = 'n';
- else if (cellInfo.type === 'date' ||
- (ssfId > 13 && ssfId < 37) || (ssfId > 44 && ssfId < 48) || ssfId === 56)
- _t = 'd';
- } else
- _t = 's';
- if (v != null) {
- let vd;
- if (v.length === 0) {
- o.t = 'z';
- }
- else if (v.trim().length === 0) {
- }
- else if (_t === 's') {
- }
- else if (cellInfo.type === 'function') {
- o = {f: v};
- }
- else if (v === 'TRUE') {
- o = {t: 'b', v: true};
- }
- else if (v === 'FALSE') {
- o = {t: 'b', v: false};
- }
- else if (_t === 'n' || isFinite(xlsxToNumber(v, defaults.numbers.output))) { // yes, defaults.numbers.output is right
- const vn = xlsxToNumber(v, defaults.numbers.output);
- if (ssfId === 0 && typeof defaults.mso.xlsx.formatId.numbers !== 'function') {
- ssfId = defaults.mso.xlsx.formatId.numbers;
- }
- if (isFinite(vn) || isFinite(v))
- o = {
- t: 'n',
- v: (isFinite(vn) ? vn : v),
- z: (typeof ssfId === 'string') ? ssfId :
- (ssfId in ssfTable ? ssfTable[ssfId] :
- ssfId === defaults.mso.xlsx.formatId.currency ? defaults.mso.xlsx.format.currency :
- '0.00')
- };
- }
- else if ((vd = parseDateUTC(v)) !== false || _t === 'd') {
- if (ssfId === 0 && typeof defaults.mso.xlsx.formatId.date !== 'function') {
- ssfId = defaults.mso.xlsx.formatId.date;
- }
- o = {
- t: 'd',
- v: (vd !== false ? vd : v),
- z: (typeof ssfId === 'string') ? ssfId : (ssfId in ssfTable ? ssfTable[ssfId] : 'm/d/yy')
- };
- }
- const $aTag = $(elt).find('a');
- if ($aTag && $aTag.length) {
- const href = $aTag[0].hasAttribute("href") ? $aTag.attr('href') : '';
- const content = (defaults.htmlHyperlink !== 'href' || href === '') ? v : '';
- const hyperlink = (href !== '') ? '=HYPERLINK("' + href + (content.length ? '","' + content : '') + '")' : '';
- if (hyperlink !== '') {
- if (typeof defaults.mso.xlsx.onHyperlink === 'function') {
- v = defaults.mso.xlsx.onHyperlink($(elt), _R, _C, href, content, hyperlink);
- if (v.indexOf('=HYPERLINK') !== 0) {
- o = {t: 's', v: v};
- } else {
- o = {f: v};
- }
- } else {
- o = {f: hyperlink};
- }
- }
- }
- }
- ws[xlsxEncodeCell({c: C, r: R})] = o;
- if (range.e.c < C) {
- range.e.c = C;
- }
- C += CS;
- }
- ++R;
- }
- if (merges.length) {
- ws['!merges'] = (ws["!merges"] || []).concat(merges);
- }
- range.e.r = Math.max(range.e.r, R - 1);
- ws['!ref'] = xlsxEncodeRange(range);
- if (R >= sheetRows) {
- ws['!fullref'] = xlsxEncodeRange((range.e.r = rows.length - _R + R - 1, range));
- }
- return ws;
- }
- function xlsxEncodeRow (row) {
- return '' + (row + 1);
- }
- function xlsxEncodeCol (col) {
- let s = '';
- for (++col; col; col = Math.floor((col - 1) / 26)) {
- s = String.fromCharCode(((col - 1) % 26) + 65) + s;
- }
- return s;
- }
- function xlsxEncodeCell (cell) {
- return xlsxEncodeCol(cell.c) + xlsxEncodeRow(cell.r);
- }
- function xlsxEncodeRange (cs, ce) {
- if (typeof ce === 'undefined' || typeof ce === 'number') {
- return xlsxEncodeRange(cs.s, cs.e);
- }
- if (typeof cs !== 'string') {
- cs = xlsxEncodeCell((cs));
- }
- if (typeof ce !== 'string') {
- ce = xlsxEncodeCell((ce));
- }
- return cs === ce ? cs : cs + ':' + ce;
- }
- function xlsxToNumber (s, numbersFormat) {
- let v = Number(s);
- if (isFinite(v)) return v;
- let wt = 1;
- let ss = s;
- if ('' !== numbersFormat.thousandsSeparator)
- ss = ss.replace(new RegExp('([\\d])' + numbersFormat.thousandsSeparator + '([\\d])', 'g'), '$1$2');
- if ('.' !== numbersFormat.decimalMark)
- ss = ss.replace(new RegExp('([\\d])' + numbersFormat.decimalMark + '([\\d])', 'g'), '$1.$2');
- ss = ss.replace(/[$]/g, '').replace(/[%]/g, function () {
- wt *= 100;
- return '';
- });
- if (isFinite(v = Number(ss))) return v / wt;
- ss = ss.replace(/[(](.*)[)]/, function ($$, $1) {
- wt = -wt;
- return $1;
- });
- if (isFinite(v = Number(ss))) return v / wt;
- return v;
- }
- function strHashCode (str) {
- let hash = 0, i, chr, len;
- if (str.length === 0) return hash;
- for (i = 0, len = str.length; i < len; i++) {
- chr = str.charCodeAt(i);
- hash = ((hash << 5) - hash) + chr;
- hash |= 0; // Convert to 32bit integer
- }
- return hash;
- }
- function saveToFile (data, fileName, type, charset, encoding, bom) {
- let saveIt = true;
- if (typeof defaults.onBeforeSaveToFile === 'function') {
- saveIt = defaults.onBeforeSaveToFile(data, fileName, type, charset, encoding);
- if (typeof saveIt !== 'boolean')
- saveIt = true;
- }
- if (saveIt) {
- try {
- if (bom)
- blob = new Blob([String.fromCharCode(0xFEFF), [data]], { type: type + ';charset=' + charset});
- else
- blob = new Blob([data], {type: type + ';charset=' + charset});
- saveAs(blob, fileName, {autoBom: false});
- if (typeof defaults.onAfterSaveToFile === 'function')
- defaults.onAfterSaveToFile(data, fileName);
- } catch (e) {
- downloadFile(fileName,
- 'data:' + type +
- (charset.length ? ';charset=' + charset : '') +
- (encoding.length ? ';' + encoding : '') + ',',
- (bom ? ('\ufeff' + data) : data));
- }
- }
- }
- function downloadFile (filename, header, data) {
- const ua = window.navigator.userAgent;
- if (filename !== false && window.navigator.msSaveOrOpenBlob) {
- //noinspection JSUnresolvedFunction
- window.navigator.msSaveOrOpenBlob(new Blob([data]), filename);
- } else if (filename !== false && (ua.indexOf('MSIE ') > 0 || !!ua.match(/Trident.*rv\:11\./))) {
- // Internet Explorer (<= 9) workaround by Darryl (https://github.com/dawiong/tableExport.jquery.plugin)
- // based on sampopes answer on http://stackoverflow.com/questions/22317951
- // ! Not working for json and pdf format !
- const frame = document.createElement('iframe');
- if (frame) {
- document.body.appendChild(frame);
- frame.setAttribute('style', 'display:none');
- frame.contentDocument.open('txt/plain', 'replace');
- frame.contentDocument.write(data);
- frame.contentDocument.close();
- frame.contentWindow.focus();
- const extension = filename.substr((filename.lastIndexOf('.') + 1));
- switch (extension) {
- case 'doc':
- case 'json':
- case 'png':
- case 'pdf':
- case 'xls':
- case 'xlsx':
- filename += '.txt';
- break;
- }
- frame.contentDocument.execCommand('SaveAs', true, filename);
- document.body.removeChild(frame);
- }
- } else {
- const DownloadLink = document.createElement('a');
- if (DownloadLink) {
- let blobUrl = null;
- DownloadLink.style.display = 'none';
- if (filename !== false)
- DownloadLink.download = filename;
- else
- DownloadLink.target = '_blank';
- if (typeof data === 'object') {
- window.URL = window.URL || window.webkitURL;
- const binaryData = [];
- binaryData.push(data);
- blobUrl = window.URL.createObjectURL(new Blob(binaryData, {type: header}));
- DownloadLink.href = blobUrl;
- }
- else if (header.toLowerCase().indexOf('base64,') >= 0) {
- DownloadLink.href = header + base64encode(data);
- }
- else {
- DownloadLink.href = header + encodeURIComponent(data);
- }
- document.body.appendChild(DownloadLink);
- if (document.createEvent) {
- if (DownloadEvt === null)
- DownloadEvt = document.createEvent('MouseEvents');
- DownloadEvt.initEvent('click', true, false);
- DownloadLink.dispatchEvent(DownloadEvt);
- }
- else if (document.createEventObject)
- DownloadLink.fireEvent('onclick');
- else if (typeof DownloadLink.onclick === 'function')
- DownloadLink.onclick();
- setTimeout(function () {
- if (blobUrl)
- window.URL.revokeObjectURL(blobUrl);
- document.body.removeChild(DownloadLink);
- if (typeof defaults.onAfterSaveToFile === 'function')
- defaults.onAfterSaveToFile(data, filename);
- }, 100);
- }
- }
- }
- function utf8Encode (text) {
- if (typeof text === 'string') {
- text = text.replace(/\x0d\x0a/g, '\x0a');
- let utfText = '';
- for (let n = 0; n < text.length; n++) {
- const c = text.charCodeAt(n);
- if (c < 128) {
- utfText += String.fromCharCode(c);
- } else if ((c > 127) && (c < 2048)) {
- utfText += String.fromCharCode((c >> 6) | 192);
- utfText += String.fromCharCode((c & 63) | 128);
- } else {
- utfText += String.fromCharCode((c >> 12) | 224);
- utfText += String.fromCharCode(((c >> 6) & 63) | 128);
- utfText += String.fromCharCode((c & 63) | 128);
- }
- }
- return utfText;
- }
- return text;
- }
- function base64encode (input) {
- let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
- const keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
- let output = '';
- let i = 0;
- input = utf8Encode(input);
- while (i < input.length) {
- chr1 = input.charCodeAt(i++);
- chr2 = input.charCodeAt(i++);
- chr3 = input.charCodeAt(i++);
- enc1 = chr1 >> 2;
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
- enc4 = chr3 & 63;
- if (isNaN(chr2)) {
- enc3 = enc4 = 64;
- } else if (isNaN(chr3)) {
- enc4 = 64;
- }
- output = output +
- keyStr.charAt(enc1) + keyStr.charAt(enc2) +
- keyStr.charAt(enc3) + keyStr.charAt(enc4);
- }
- return output;
- }
- // ----------------------------------------------------------------------------------------------------
- // jsPDF-AutoTable 2.0.17 - BEGIN
- // Adopted and adapted source code from https://github.com/simonbengtsson/jsPDF-AutoTable
- // ----------------------------------------------------------------------------------------------------
- var jsPdfDoc, // The current jspdf instance
- jsPdfCursor, // An object keeping track of the x and y position of the next table cell to draw
- jsPdfSettings, // Default options merged with user options
- jsPdfPageCount, // The page count the current table spans
- jsPdfTable; // The current Table instance
- function jsPdfAutoTable (doc, headers, data, options) {
- jsPdfValidateInput(headers, data, options);
- jsPdfDoc = doc;
- jsPdfSettings = jsPdfInitOptions(options || {});
- jsPdfPageCount = 1;
- // Need a cursor y as it needs to be reset after each page (row.y can't do that)
- jsPdfCursor = { y: jsPdfSettings.startY === false ? jsPdfSettings.margin.top : jsPdfSettings.startY };
- const userStyles = {
- textColor: 30, // Setting text color to dark gray as it can't be obtained from jsPDF
- fontSize: jsPdfDoc.internal.getFontSize(),
- fontStyle: jsPdfDoc.internal.getFont().fontStyle,
- fontName: jsPdfDoc.internal.getFont().fontName
- };
- // Create the table model with its columns, rows and cells
- jsPdfCreateModels(headers, data);
- jsPdfCalculateWidths();
- // Page break if there is room for only the first data row
- const firstRowHeight = jsPdfTable.rows[0] && jsPdfSettings.pageBreak === 'auto' ? jsPdfTable.rows[0].height : 0;
- let minTableBottomPos = jsPdfSettings.startY + jsPdfSettings.margin.bottom + jsPdfTable.headerRow.height + firstRowHeight;
- if (jsPdfSettings.pageBreak === 'avoid') {
- minTableBottomPos += jsPdfTable.height;
- }
- if ((jsPdfSettings.pageBreak === 'always' && jsPdfSettings.startY !== false) ||
- (jsPdfSettings.startY !== false && minTableBottomPos > jsPdfDoc.internal.pageSize.height)) {
- jsPdfDoc.addPage();
- jsPdfCursor.y = jsPdfSettings.margin.top;
- }
- jsPdfApplyStyles(userStyles);
- jsPdfSettings.beforePageContent(jsPdfHooksData());
- if (jsPdfSettings.drawHeaderRow(jsPdfTable.headerRow, jsPdfHooksData({row: jsPdfTable.headerRow})) !== false) {
- jsPdfPrintRow(jsPdfTable.headerRow, jsPdfSettings.drawHeaderCell);
- }
- jsPdfApplyStyles(userStyles);
- jsPdfPrintRows();
- jsPdfSettings.afterPageContent(jsPdfHooksData());
- jsPdfApplyStyles(userStyles);
- return jsPdfDoc;
- }
- /**
- * Returns the Y position of the last drawn cell
- * @returns int
- */
- function jsPdfAutoTableEndPosY () {
- if (typeof jsPdfCursor === 'undefined' || typeof jsPdfCursor.y === 'undefined') {
- return 0;
- }
- return jsPdfCursor.y;
- }
- /**
- * Improved text function with halign and valign support
- * Inspiration from:
- * http://stackoverflow.com/questions/28327510/align-text-right-using-jspdf/28433113#28433113
- */
- function jsPdfAutoTableText (text, x, y, styles) {
- if (typeof x !== 'number' || typeof y !== 'number') {
- console.error('The x and y parameters are required. Missing for the text: ', text);
- }
- const fontSize = jsPdfDoc.internal.getFontSize() / jsPdfDoc.internal.scaleFactor;
- // As defined in jsPDF source code
- const lineHeightProportion = FONT_ROW_RATIO;
- const splitRegex = /\r\n|\r|\n/g;
- let splittedText = null;
- let lineCount = 1;
- if (styles.valign === 'middle' || styles.valign === 'bottom' || styles.halign === 'center' || styles.halign === 'right') {
- splittedText = typeof text === 'string' ? text.split(splitRegex) : text;
- lineCount = splittedText.length || 1;
- }
- // Align the top
- y += fontSize * (2 - lineHeightProportion);
- if (styles.valign === 'middle')
- y -= (lineCount / 2) * fontSize;
- else if (styles.valign === 'bottom')
- y -= lineCount * fontSize;
- if (styles.halign === 'center' || styles.halign === 'right') {
- let alignSize = fontSize;
- if (styles.halign === 'center')
- alignSize *= 0.5;
- if (splittedText && lineCount >= 1) {
- for (let iLine = 0; iLine < splittedText.length; iLine++) {
- jsPdfDoc.text(splittedText[iLine], x - jsPdfDoc.getStringUnitWidth(splittedText[iLine]) * alignSize, y);
- y += fontSize;
- }
- return jsPdfDoc;
- }
- x -= jsPdfDoc.getStringUnitWidth(text) * alignSize;
- }
- jsPdfDoc.text(text, x, y);
- return jsPdfDoc;
- }
- function jsPdfValidateInput(headers, data, options) {
- if (!headers || typeof headers !== 'object') {
- console.error("The headers should be an object or array, is: " + typeof headers);
- }
- if (!data || typeof data !== 'object') {
- console.error("The data should be an object or array, is: " + typeof data);
- }
- if (!!options && typeof options !== 'object') {
- console.error("The data should be an object or array, is: " + typeof data);
- }
- if (!Array.prototype.forEach) {
- console.error("The current browser does not support Array.prototype.forEach which is required for jsPDF-AutoTable");
- }
- }
- function jsPdfInitOptions(userOptions) {
- const settings = jsPdfExtend(jsPdfDefaultOptions(), userOptions);
- // Options
- if (typeof settings.extendWidth !== 'undefined') {
- settings.tableWidth = settings.extendWidth ? 'auto' : 'wrap';
- console.error("Use of deprecated option: extendWidth, use tableWidth instead.");
- }
- if (typeof settings.margins !== 'undefined') {
- if (typeof settings.margin === 'undefined') settings.margin = settings.margins;
- console.error("Use of deprecated option: margins, use margin instead.");
- }
- [['padding', 'cellPadding'], ['lineHeight', 'rowHeight'], 'fontSize', 'overflow'].forEach(function (o) {
- const deprecatedOption = typeof o === 'string' ? o : o[0];
- const style = typeof o === 'string' ? o : o[1];
- if (typeof settings[deprecatedOption] !== 'undefined') {
- if (typeof settings.styles[style] === 'undefined') {
- settings.styles[style] = settings[deprecatedOption];
- }
- console.error("Use of deprecated option: " + deprecatedOption + ", use the style " + style + " instead.");
- }
- });
- // Unifying
- const marginSetting = settings.margin;
- settings.margin = {};
- if (typeof marginSetting.horizontal === 'number') {
- marginSetting.right = marginSetting.horizontal;
- marginSetting.left = marginSetting.horizontal;
- }
- if (typeof marginSetting.vertical === 'number') {
- marginSetting.top = marginSetting.vertical;
- marginSetting.bottom = marginSetting.vertical;
- }
- ['top', 'right', 'bottom', 'left'].forEach(function (side, i) {
- if (typeof marginSetting === 'number') {
- settings.margin[side] = marginSetting;
- } else {
- const key = Array.isArray(marginSetting) ? i : side;
- settings.margin[side] = typeof marginSetting[key] === 'number' ? marginSetting[key] : 40;
- }
- });
- return settings;
- }
- /**
- * Create models from the user input
- *
- * @param inputHeaders
- * @param inputData
- */
- function jsPdfCreateModels(inputHeaders, inputData) {
- jsPdfTable = new jsPdfTableClass();
- jsPdfTable.x = jsPdfSettings.margin.left;
- const splitRegex = /\r\n|\r|\n/g;
- // Header row and columns
- const headerRow = new jsPdfRowClass(inputHeaders);
- headerRow.index = -1;
- const themeStyles = jsPdfExtend(jsPdfDefaultStyles, jsPdfThemes[jsPdfSettings.theme].table, jsPdfThemes[jsPdfSettings.theme].header);
- headerRow.styles = jsPdfExtend(themeStyles, jsPdfSettings.styles, jsPdfSettings.headerStyles);
- // Columns and header row
- inputHeaders.forEach(function (rawColumn, dataKey) {
- if (typeof rawColumn === 'object') {
- dataKey = typeof rawColumn.dataKey !== 'undefined' ? rawColumn.dataKey : rawColumn.key;
- }
- if (typeof rawColumn.width !== 'undefined') {
- console.error("Use of deprecated option: column.width, use column.styles.columnWidth instead.");
- }
- const col = new jsPdfColumnClass(dataKey);
- col.styles = jsPdfSettings.columnStyles[col.dataKey] || {};
- jsPdfTable.columns.push(col);
- const cell = new jsPdfCellClass();
- cell.raw = typeof rawColumn === 'object' ? rawColumn.title : rawColumn;
- // jsPDF AutoTable plugin v2.0.14 fix: each cell needs its own styles object
- //cell.styles = jsPdfExtend(headerRow.styles);
- cell.styles = $.extend({}, headerRow.styles);
- cell.text = '' + cell.raw;
- cell.contentWidth = cell.styles.cellPadding * 2 + jsPdfGetStringWidth(cell.text, cell.styles);
- cell.text = cell.text.split(splitRegex);
- headerRow.cells[dataKey] = cell;
- jsPdfSettings.createdHeaderCell(cell, {column: col, row: headerRow, settings: jsPdfSettings});
- });
- jsPdfTable.headerRow = headerRow;
- // Rows och cells
- inputData.forEach(function (rawRow, i) {
- const row = new jsPdfRowClass(rawRow);
- const isAlternate = i % 2 === 0;
- const themeStyles = jsPdfExtend(jsPdfDefaultStyles, jsPdfThemes[jsPdfSettings.theme].table, isAlternate ? jsPdfThemes[jsPdfSettings.theme].alternateRow : {});
- const userStyles = jsPdfExtend(jsPdfSettings.styles, jsPdfSettings.bodyStyles, isAlternate ? jsPdfSettings.alternateRowStyles : {});
- row.styles = jsPdfExtend(themeStyles, userStyles);
- row.index = i;
- jsPdfTable.columns.forEach(function (column) {
- const cell = new jsPdfCellClass();
- cell.raw = rawRow[column.dataKey];
- cell.styles = jsPdfExtend(row.styles, column.styles);
- cell.text = typeof cell.raw !== 'undefined' ? '' + cell.raw : ''; // Stringify 0 and false, but not undefined
- row.cells[column.dataKey] = cell;
- jsPdfSettings.createdCell(cell, jsPdfHooksData({column: column, row: row}));
- cell.contentWidth = cell.styles.cellPadding * 2 + jsPdfGetStringWidth(cell.text, cell.styles);
- cell.text = cell.text.split(splitRegex);
- });
- jsPdfTable.rows.push(row);
- });
- }
- /**
- * Calculate the column widths
- */
- function jsPdfCalculateWidths() {
- // Column and table content width
- let tableContentWidth = 0;
- jsPdfTable.columns.forEach(function (column) {
- column.contentWidth = jsPdfTable.headerRow.cells[column.dataKey].contentWidth;
- jsPdfTable.rows.forEach(function (row) {
- const cellWidth = row.cells[column.dataKey].contentWidth;
- if (cellWidth > column.contentWidth) {
- column.contentWidth = cellWidth;
- }
- });
- column.width = column.contentWidth;
- tableContentWidth += column.contentWidth;
- });
- jsPdfTable.contentWidth = tableContentWidth;
- const maxTableWidth = jsPdfDoc.internal.pageSize.width - jsPdfSettings.margin.left - jsPdfSettings.margin.right;
- let preferredTableWidth = maxTableWidth; // settings.tableWidth === 'auto'
- if (typeof jsPdfSettings.tableWidth === 'number') {
- preferredTableWidth = jsPdfSettings.tableWidth;
- } else if (jsPdfSettings.tableWidth === 'wrap') {
- preferredTableWidth = jsPdfTable.contentWidth;
- }
- jsPdfTable.width = preferredTableWidth < maxTableWidth ? preferredTableWidth : maxTableWidth;
- // To avoid subjecting columns with little content with the chosen overflow method,
- // never shrink a column more than the table divided by column count (its "fair part")
- const dynamicColumns = [];
- let dynamicColumnsContentWidth = 0;
- const fairWidth = jsPdfTable.width / jsPdfTable.columns.length;
- let staticWidth = 0;
- jsPdfTable.columns.forEach(function (column) {
- const colStyles = jsPdfExtend(jsPdfDefaultStyles, jsPdfThemes[jsPdfSettings.theme].table, jsPdfSettings.styles, column.styles);
- if (colStyles.columnWidth === 'wrap') {
- column.width = column.contentWidth;
- } else if (typeof colStyles.columnWidth === 'number') {
- column.width = colStyles.columnWidth;
- } else if (colStyles.columnWidth === 'auto' || true) {
- if (column.contentWidth <= fairWidth && jsPdfTable.contentWidth > jsPdfTable.width) {
- column.width = column.contentWidth;
- } else {
- dynamicColumns.push(column);
- dynamicColumnsContentWidth += column.contentWidth;
- column.width = 0;
- }
- }
- staticWidth += column.width;
- });
- // Distributes extra width or trims columns down to fit
- jsPdfDistributeWidth(dynamicColumns, staticWidth, dynamicColumnsContentWidth, fairWidth);
- // Row height, table height and text overflow
- jsPdfTable.height = 0;
- const all = jsPdfTable.rows.concat(jsPdfTable.headerRow);
- all.forEach(function (row, i) {
- let lineBreakCount = 0;
- let cursorX = jsPdfTable.x;
- jsPdfTable.columns.forEach(function (col) {
- const cell = row.cells[col.dataKey];
- col.x = cursorX;
- jsPdfApplyStyles(cell.styles);
- const textSpace = col.width - cell.styles.cellPadding * 2;
- if (cell.styles.overflow === 'linebreak') {
- // Add one pt to textSpace to fix rounding error
- cell.text = jsPdfDoc.splitTextToSize(cell.text, textSpace + 1, {fontSize: cell.styles.fontSize});
- } else if (cell.styles.overflow === 'ellipsize') {
- cell.text = jsPdfEllipsize(cell.text, textSpace, cell.styles);
- } else if (cell.styles.overflow === 'visible') {
- // Do nothing
- } else if (cell.styles.overflow === 'hidden') {
- cell.text = jsPdfEllipsize(cell.text, textSpace, cell.styles, '');
- } else if (typeof cell.styles.overflow === 'function') {
- cell.text = cell.styles.overflow(cell.text, textSpace);
- } else {
- console.error("Unrecognized overflow type: " + cell.styles.overflow);
- }
- const count = Array.isArray(cell.text) ? cell.text.length - 1 : 0;
- if (count > lineBreakCount) {
- lineBreakCount = count;
- }
- cursorX += col.width;
- });
- row.heightStyle = row.styles.rowHeight;
- // TODO Pick the highest row based on font size as well
- row.height = (row.heightStyle + lineBreakCount * row.styles.fontSize * FONT_ROW_RATIO) +
- ((2 - FONT_ROW_RATIO) / 2 * row.styles.fontSize); // Fix jsPDF Autotable's row height calculation
- jsPdfTable.height += row.height;
- });
- }
- function jsPdfDistributeWidth(dynamicColumns, staticWidth, dynamicColumnsContentWidth, fairWidth) {
- const extraWidth = jsPdfTable.width - staticWidth - dynamicColumnsContentWidth;
- for (let i = 0; i < dynamicColumns.length; i++) {
- const col = dynamicColumns[i];
- const ratio = col.contentWidth / dynamicColumnsContentWidth;
- // A column turned out to be none dynamic, start over recursively
- const isNoneDynamic = col.contentWidth + extraWidth * ratio < fairWidth;
- if (extraWidth < 0 && isNoneDynamic) {
- dynamicColumns.splice(i, 1);
- dynamicColumnsContentWidth -= col.contentWidth;
- col.width = fairWidth;
- staticWidth += col.width;
- jsPdfDistributeWidth(dynamicColumns, staticWidth, dynamicColumnsContentWidth, fairWidth);
- break;
- } else {
- col.width = col.contentWidth + extraWidth * ratio;
- }
- }
- }
- function jsPdfPrintRows() {
- jsPdfTable.rows.forEach(function (row, i) {
- if (jsPdfIsNewPage(row.height)) {
- jsPdfAddPage();
- }
- row.y = jsPdfCursor.y;
- if (jsPdfSettings.drawRow(row, jsPdfHooksData({row: row})) !== false) {
- jsPdfPrintRow(row, jsPdfSettings.drawCell);
- }
- });
- }
- function jsPdfAddPage() {
- jsPdfSettings.afterPageContent(jsPdfHooksData());
- jsPdfDoc.addPage();
- jsPdfPageCount++;
- jsPdfCursor = {x: jsPdfSettings.margin.left, y: jsPdfSettings.margin.top};
- jsPdfSettings.beforePageContent(jsPdfHooksData());
- if (jsPdfSettings.drawHeaderRow(jsPdfTable.headerRow, jsPdfHooksData({row: jsPdfTable.headerRow})) !== false) {
- jsPdfPrintRow(jsPdfTable.headerRow, jsPdfSettings.drawHeaderCell);
- }
- }
- /**
- * Add a new page if cursor is at the end of page
- * @param rowHeight
- * @returns {boolean}
- */
- function jsPdfIsNewPage(rowHeight) {
- const afterRowPos = jsPdfCursor.y + rowHeight + jsPdfSettings.margin.bottom;
- return afterRowPos >= jsPdfDoc.internal.pageSize.height;
- }
- function jsPdfPrintRow(row, hookHandler) {
- for (let i = 0; i < jsPdfTable.columns.length; i++) {
- const column = jsPdfTable.columns[i];
- const cell = row.cells[column.dataKey];
- if(!cell) {
- continue;
- }
- jsPdfApplyStyles(cell.styles);
- cell.x = column.x;
- cell.y = jsPdfCursor.y;
- cell.height = row.height;
- cell.width = column.width;
- if (cell.styles.valign === 'top') {
- cell.textPos.y = jsPdfCursor.y + cell.styles.cellPadding;
- } else if (cell.styles.valign === 'bottom') {
- cell.textPos.y = jsPdfCursor.y + row.height - cell.styles.cellPadding;
- } else {
- cell.textPos.y = jsPdfCursor.y + row.height / 2;
- }
- if (cell.styles.halign === 'right') {
- cell.textPos.x = cell.x + cell.width - cell.styles.cellPadding;
- } else if (cell.styles.halign === 'center') {
- cell.textPos.x = cell.x + cell.width / 2;
- } else {
- cell.textPos.x = cell.x + cell.styles.cellPadding;
- }
- const data = jsPdfHooksData({column: column, row: row});
- if (hookHandler(cell, data) !== false) {
- jsPdfDoc.rect(cell.x, cell.y, cell.width, cell.height, cell.styles.fillStyle);
- jsPdfAutoTableText(cell.text, cell.textPos.x, cell.textPos.y, {
- halign: cell.styles.halign,
- valign: cell.styles.valign
- });
- }
- }
- jsPdfCursor.y += row.height;
- }
- function jsPdfApplyStyles(styles) {
- const arr = [
- {func: jsPdfDoc.setFillColor, value: styles.fillColor},
- {func: jsPdfDoc.setTextColor, value: styles.textColor},
- {func: jsPdfDoc.setFont, value: styles.font, style: styles.fontStyle},
- {func: jsPdfDoc.setDrawColor, value: styles.lineColor},
- {func: jsPdfDoc.setLineWidth, value: styles.lineWidth},
- {func: jsPdfDoc.setFont, value: styles.font},
- {func: jsPdfDoc.setFontSize, value: styles.fontSize}
- ];
- arr.forEach(function (obj) {
- if (typeof obj.value !== 'undefined') {
- if (obj.value.constructor === Array) {
- obj.func.apply(jsPdfDoc, obj.value);
- } else if (typeof obj.style !== 'undefined') {
- obj.func(obj.value, obj.style);
- } else {
- obj.func(obj.value);
- }
- }
- });
- }
- function jsPdfHooksData(additionalData) {
- additionalData = additionalData || {};
- const data = {
- pageCount: jsPdfPageCount,
- settings: jsPdfSettings,
- table: jsPdfTable,
- cursor: jsPdfCursor
- };
- for (let prop in additionalData) {
- if (additionalData.hasOwnProperty(prop)) {
- data[prop] = additionalData[prop];
- }
- }
- return data;
- }
- /**
- * Ellipsize the text to fit in the width
- */
- function jsPdfEllipsize(text, width, styles, ellipsizeStr) {
- ellipsizeStr = typeof ellipsizeStr !== 'undefined' ? ellipsizeStr : '...';
- if (Array.isArray(text)) {
- text.forEach(function (str, i) {
- text[i] = jsPdfEllipsize(str, width, styles, ellipsizeStr);
- });
- return text;
- }
- if (width >= jsPdfGetStringWidth(text, styles)) {
- return text;
- }
- while (width < jsPdfGetStringWidth(text + ellipsizeStr, styles)) {
- if (text.length < 2) {
- break;
- }
- text = text.substring(0, text.length - 1);
- }
- return text.trim() + ellipsizeStr;
- }
- function jsPdfGetStringWidth(text, styles) {
- jsPdfApplyStyles(styles);
- const w = jsPdfDoc.getStringUnitWidth(text);
- return w * styles.fontSize;
- }
- function jsPdfExtend(defaults) {
- const extended = {};
- let prop;
- for (prop in defaults) {
- if (defaults.hasOwnProperty(prop)) {
- extended[prop] = defaults[prop];
- }
- }
- for (let i = 1; i < arguments.length; i++) {
- const options = arguments[i]
- for (prop in options) {
- if (options.hasOwnProperty(prop)) {
- extended[prop] = options[prop];
- }
- }
- }
- return extended;
- }
- // ----------------------------------------------------------------------------------------------------
- // jsPDF-AutoTable 2.0.17 - END
- // ----------------------------------------------------------------------------------------------------
- if (typeof defaults.onTableExportEnd === 'function')
- defaults.onTableExportEnd();
- return this;
- };
- // See README.md for documentation of the options
- // See examples.js for usage examples
- function jsPdfDefaultOptions () {
- return {
- // Styling
- theme: 'striped', // 'striped', 'grid' or 'plain'
- styles: {},
- headerStyles: {},
- bodyStyles: {},
- alternateRowStyles: {},
- columnStyles: {},
- // Properties
- startY: false, // false indicates the margin.top value
- margin: 40,
- pageBreak: 'auto', // 'auto', 'avoid', 'always'
- tableWidth: 'auto', // number, 'auto', 'wrap'
- // Hooks
- createdHeaderCell: function (cell, data) {},
- createdCell: function (cell, data) {},
- drawHeaderRow: function (row, data) {},
- drawRow: function (row, data) {},
- drawHeaderCell: function (cell, data) {},
- drawCell: function (cell, data) {},
- beforePageContent: function (data) {},
- afterPageContent: function (data) {}
- }
- }
- var jsPdfTableClass = /** class */ (function () {
- function jsPdfTableClass() { /** constructor */
- this.height = 0;
- this.width = 0;
- this.x = 0;
- this.y = 0;
- this.contentWidth = 0;
- this.rows = [];
- this.columns = [];
- this.headerRow = null;
- this.settings = {};
- }
- return jsPdfTableClass;
- }());
- var jsPdfRowClass = /** class */ (function () {
- function jsPdfRowClass(raw) { /** constructor */
- this.raw = raw || {};
- this.index = 0;
- this.styles = {};
- this.cells = {};
- this.height = 0;
- this.y = 0;
- }
- return jsPdfRowClass;
- }());
- var jsPdfCellClass = /** class */ (function () {
- function jsPdfCellClass(raw) { /** constructor */
- this.raw = raw;
- this.styles = {};
- this.text = '';
- this.contentWidth = 0;
- this.textPos = {};
- this.height = 0;
- this.width = 0;
- this.x = 0;
- this.y = 0;
- }
- return jsPdfCellClass;
- }());
- var jsPdfColumnClass = /** class */ (function () {
- function jsPdfColumnClass(dataKey) { /** constructor */
- this.dataKey = dataKey;
- this.options = {};
- this.styles = {};
- this.contentWidth = 0;
- this.width = 0;
- this.x = 0;
- }
- return jsPdfColumnClass;
- }());
- })(jQuery);
|