-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathinit.js
More file actions
170 lines (149 loc) · 5.82 KB
/
Copy pathinit.js
File metadata and controls
170 lines (149 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict'
import Browser from './browser'
import Modal from './modal'
import Pdf from './pdf'
import Html from './html'
import RawHtml from './raw-html'
import Image from './image'
import Json from './json'
const printTypes = ['pdf', 'html', 'image', 'json', 'raw-html']
export default {
init () {
const params = {
printable: null,
fallbackPrintable: null,
type: 'pdf',
header: null,
headerStyle: 'font-weight: 300;',
maxWidth: 800,
properties: null,
rowsBefore: null,
gridHeaderStyle: 'font-weight: bold; padding: 5px; border: 1px solid #dddddd;',
gridStyle: 'border: 1px solid lightgray; margin-bottom: -1px;',
showModal: false,
onError: (error) => { throw error },
onLoadingStart: null,
onLoadingEnd: null,
onPrintDialogClose: () => {},
onIncompatibleBrowser: () => {},
modalMessage: 'Retrieving Document...',
frameId: 'printJS',
printableElement: null,
documentTitle: 'Document',
targetStyle: ['clear', 'display', 'width', 'min-width', 'height', 'min-height', 'max-height'],
targetStyles: ['border', 'box', 'break', 'text-decoration'],
ignoreElements: [],
repeatTableHeader: true,
css: null,
style: null,
scanStyles: true,
base64: false,
// Deprecated
onPdfOpen: null,
font: 'TimesNewRoman',
font_size: '12pt',
honorMarginPadding: true,
honorColor: false,
imageStyle: 'max-width: 100%;'
}
// Check if a printable document or object was supplied
const args = arguments[0]
if (args === undefined) {
throw new Error('printJS expects at least 1 attribute.')
}
// Process parameters
switch (typeof args) {
case 'string':
params.printable = encodeURI(args)
params.fallbackPrintable = params.printable
params.type = arguments[1] || params.type
break
case 'object':
params.printable = args.printable
params.rowsBefore = args.rowsBefore
params.fallbackPrintable = typeof args.fallbackPrintable !== 'undefined' ? args.fallbackPrintable : params.printable
params.fallbackPrintable = params.base64 ? `data:application/pdf;base64,${params.fallbackPrintable}` : params.fallbackPrintable
for (const k in params) {
if (k === 'printable' || k === 'fallbackPrintable') continue
params[k] = typeof args[k] !== 'undefined' ? args[k] : params[k]
}
break
default:
throw new Error('Unexpected argument type! Expected "string" or "object", got ' + typeof args)
}
// Validate printable
if (!params.printable) throw new Error('Missing printable information.')
// Validate type
if (!params.type || typeof params.type !== 'string' || printTypes.indexOf(params.type.toLowerCase()) === -1) {
throw new Error('Invalid print type. Available types are: pdf, html, image and json.')
}
// Check if we are showing a feedback message to the user (useful for large files)
if (params.showModal) Modal.show(params)
// Check for a print start hook function
if (params.onLoadingStart) params.onLoadingStart()
// To prevent duplication and issues, remove any used printFrame from the DOM
const usedFrame = document.getElementById(params.frameId)
if (usedFrame) usedFrame.parentNode.removeChild(usedFrame)
// Create a new iframe for the print job
const printFrame = document.createElement('iframe')
if (Browser.isFirefox()) {
// Set the iframe to be is visible on the page (guaranteed by fixed position) but hidden using opacity 0, because
// this works in Firefox. The height needs to be sufficient for some part of the document other than the PDF
// viewer's toolbar to be visible in the page
printFrame.setAttribute('style', 'width: 1px; height: 100px; position: fixed; left: 0; top: 0; opacity: 0; border-width: 0; margin: 0; padding: 0')
} else {
// Hide the iframe in other browsers
printFrame.setAttribute('style', 'visibility: hidden; height: 0; width: 0; position: absolute; border: 0')
}
// Set iframe element id
printFrame.setAttribute('id', params.frameId)
// For non pdf printing, pass an html document string to srcdoc (force onload callback)
if (params.type !== 'pdf') {
printFrame.srcdoc = '<html><head><title>' + params.documentTitle + '</title>'
// Attach css files
if (params.css) {
// Add support for single file
if (!Array.isArray(params.css)) params.css = [params.css]
// Create link tags for each css file
params.css.forEach(file => {
printFrame.srcdoc += '<link rel="stylesheet" href="' + file + '">'
})
}
printFrame.srcdoc += '</head><body></body></html>'
}
// Check printable type
switch (params.type) {
case 'pdf':
// Check browser support for pdf and if not supported we will just open the pdf file instead
if (Browser.isIE()) {
try {
console.info('Print.js doesn\'t support PDF printing in Internet Explorer.')
const win = window.open(params.fallbackPrintable, '_blank')
win.focus()
params.onIncompatibleBrowser()
} catch (error) {
params.onError(error)
} finally {
// Make sure there is no loading modal opened
if (params.showModal) Modal.close()
if (params.onLoadingEnd) params.onLoadingEnd()
}
} else {
Pdf.print(params, printFrame)
}
break
case 'image':
Image.print(params, printFrame)
break
case 'html':
Html.print(params, printFrame)
break
case 'raw-html':
RawHtml.print(params, printFrame)
break
case 'json':
Json.print(params, printFrame)
break
}
}
}