@driveflux/frontend-pdf 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export * from './pdf.js';
2
+ //# sourceMappingURL=index.js.map
package/dist/pdf.js CHANGED
@@ -1,151 +1,3 @@
1
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
- try {
3
- var info = gen[key](arg);
4
- var value = info.value;
5
- } catch (error) {
6
- reject(error);
7
- return;
8
- }
9
- if (info.done) {
10
- resolve(value);
11
- } else {
12
- Promise.resolve(value).then(_next, _throw);
13
- }
14
- }
15
- function _async_to_generator(fn) {
16
- return function() {
17
- var self = this, args = arguments;
18
- return new Promise(function(resolve, reject) {
19
- var gen = fn.apply(self, args);
20
- function _next(value) {
21
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
- }
23
- function _throw(err) {
24
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
- }
26
- _next(undefined);
27
- });
28
- };
29
- }
30
- function _define_property(obj, key, value) {
31
- if (key in obj) {
32
- Object.defineProperty(obj, key, {
33
- value: value,
34
- enumerable: true,
35
- configurable: true,
36
- writable: true
37
- });
38
- } else {
39
- obj[key] = value;
40
- }
41
- return obj;
42
- }
43
- function _object_spread(target) {
44
- for(var i = 1; i < arguments.length; i++){
45
- var source = arguments[i] != null ? arguments[i] : {};
46
- var ownKeys = Object.keys(source);
47
- if (typeof Object.getOwnPropertySymbols === "function") {
48
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
49
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
50
- }));
51
- }
52
- ownKeys.forEach(function(key) {
53
- _define_property(target, key, source[key]);
54
- });
55
- }
56
- return target;
57
- }
58
- function _ts_generator(thisArg, body) {
59
- var f, y, t, _ = {
60
- label: 0,
61
- sent: function() {
62
- if (t[0] & 1) throw t[1];
63
- return t[1];
64
- },
65
- trys: [],
66
- ops: []
67
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
68
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
69
- return this;
70
- }), g;
71
- function verb(n) {
72
- return function(v) {
73
- return step([
74
- n,
75
- v
76
- ]);
77
- };
78
- }
79
- function step(op) {
80
- if (f) throw new TypeError("Generator is already executing.");
81
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
82
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
83
- if (y = 0, t) op = [
84
- op[0] & 2,
85
- t.value
86
- ];
87
- switch(op[0]){
88
- case 0:
89
- case 1:
90
- t = op;
91
- break;
92
- case 4:
93
- _.label++;
94
- return {
95
- value: op[1],
96
- done: false
97
- };
98
- case 5:
99
- _.label++;
100
- y = op[1];
101
- op = [
102
- 0
103
- ];
104
- continue;
105
- case 7:
106
- op = _.ops.pop();
107
- _.trys.pop();
108
- continue;
109
- default:
110
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
111
- _ = 0;
112
- continue;
113
- }
114
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
115
- _.label = op[1];
116
- break;
117
- }
118
- if (op[0] === 6 && _.label < t[1]) {
119
- _.label = t[1];
120
- t = op;
121
- break;
122
- }
123
- if (t && _.label < t[2]) {
124
- _.label = t[2];
125
- _.ops.push(op);
126
- break;
127
- }
128
- if (t[2]) _.ops.pop();
129
- _.trys.pop();
130
- continue;
131
- }
132
- op = body.call(thisArg, _);
133
- } catch (e) {
134
- op = [
135
- 6,
136
- e
137
- ];
138
- y = 0;
139
- } finally{
140
- f = t = 0;
141
- }
142
- if (op[0] & 5) throw op[1];
143
- return {
144
- value: op[0] ? op[1] : void 0,
145
- done: true
146
- };
147
- }
148
- }
149
1
  import html2canvas from 'html2canvas';
150
2
  import jsPDF from 'jspdf';
151
3
  /**
@@ -156,26 +8,10 @@ import jsPDF from 'jspdf';
156
8
  *
157
9
  * @example
158
10
  * prepareElementsToDownload(element, 'receipt.pdf', { unit: 'mm' });
159
- */ export var prepareElementsToDownload = function(elements) {
160
- var pdfName = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'print.pdf', options = arguments.length > 2 ? arguments[2] : void 0;
161
- return _async_to_generator(function() {
162
- var pdf;
163
- return _ts_generator(this, function(_state) {
164
- switch(_state.label){
165
- case 0:
166
- return [
167
- 4,
168
- generatePdf(elements, options)
169
- ];
170
- case 1:
171
- pdf = _state.sent();
172
- pdf.save(pdfName);
173
- return [
174
- 2
175
- ];
176
- }
177
- });
178
- })();
11
+ */
12
+ export const prepareElementsToDownload = async (elements, pdfName = 'print.pdf', options) => {
13
+ const pdf = await generatePdf(elements, options);
14
+ pdf.save(pdfName);
179
15
  };
180
16
  /**
181
17
  *
@@ -185,84 +21,46 @@ import jsPDF from 'jspdf';
185
21
  *
186
22
  * @example
187
23
  * prepareElementsToPrint(element, 'receipt.pdf', { unit: 'mm' });
188
- */ export var prepareElementsToPrint = function(elements, options) {
189
- return _async_to_generator(function() {
190
- var pdf;
191
- return _ts_generator(this, function(_state) {
192
- switch(_state.label){
193
- case 0:
194
- return [
195
- 4,
196
- generatePdf(elements, options)
197
- ];
198
- case 1:
199
- pdf = _state.sent();
200
- pdf.autoPrint();
201
- window.open(pdf.output('bloburl'), '_blank');
202
- return [
203
- 2
204
- ];
205
- }
206
- });
207
- })();
24
+ */
25
+ export const prepareElementsToPrint = async (elements, options) => {
26
+ const pdf = await generatePdf(elements, options);
27
+ pdf.autoPrint();
28
+ window.open(pdf.output('bloburl'), '_blank');
208
29
  };
209
- var defaultOptions = {
30
+ const defaultOptions = {
210
31
  unit: 'px',
211
- compress: true
32
+ compress: true,
212
33
  };
213
- var generatePdf = function(elements, options) {
214
- return _async_to_generator(function() {
215
- var pdf, i, _style_sheet, element, originalWidth, style, canvas, data, imgProperties, pdfWidth, pdfHeight;
216
- return _ts_generator(this, function(_state) {
217
- switch(_state.label){
218
- case 0:
219
- pdf = new jsPDF(_object_spread({}, defaultOptions, options));
220
- i = 0;
221
- _state.label = 1;
222
- case 1:
223
- if (!(i < elements.length)) return [
224
- 3,
225
- 4
226
- ];
227
- element = elements[i];
228
- // set width to container
229
- originalWidth = element.style.width;
230
- element.style.width = (options === null || options === void 0 ? void 0 : options.orientation) === 'l' || (options === null || options === void 0 ? void 0 : options.orientation) === 'landscape' ? '1263px' : '892.5px';
231
- element.style.height = (options === null || options === void 0 ? void 0 : options.orientation) === 'l' || (options === null || options === void 0 ? void 0 : options.orientation) === 'landscape' ? '892.5px' : '1263px';
232
- // workaround to remove canvas extra space to div
233
- style = document.createElement('style');
234
- document.head.appendChild(style);
235
- (_style_sheet = style.sheet) === null || _style_sheet === void 0 ? void 0 : _style_sheet.insertRule('body > div:last-child img { display: inline-block; }');
236
- return [
237
- 4,
238
- html2canvas(element, {
239
- scale: 3.0
240
- })
241
- ];
242
- case 2:
243
- canvas = _state.sent();
244
- data = canvas.toDataURL('image/png');
245
- imgProperties = pdf.getImageProperties(data);
246
- pdfWidth = pdf.internal.pageSize.getWidth();
247
- pdfHeight = imgProperties.height * pdfWidth / imgProperties.width;
248
- pdf.addImage(data, 'PNG', 0, 0, pdfWidth, pdfHeight, '', 'FAST');
249
- element.style.width = originalWidth;
250
- if (elements.length > 1 && i < elements.length - 1) {
251
- pdf.addPage();
252
- }
253
- _state.label = 3;
254
- case 3:
255
- i++;
256
- return [
257
- 3,
258
- 1
259
- ];
260
- case 4:
261
- return [
262
- 2,
263
- pdf
264
- ];
265
- }
266
- });
267
- })();
34
+ const generatePdf = async (elements, options) => {
35
+ const pdf = new jsPDF({ ...defaultOptions, ...options });
36
+ for (let i = 0; i < elements.length; i++) {
37
+ const element = elements[i];
38
+ // set width to container
39
+ const originalWidth = element.style.width;
40
+ element.style.width =
41
+ options?.orientation === 'l' || options?.orientation === 'landscape'
42
+ ? '1263px'
43
+ : '892.5px';
44
+ element.style.height =
45
+ options?.orientation === 'l' || options?.orientation === 'landscape'
46
+ ? '892.5px'
47
+ : '1263px';
48
+ // workaround to remove canvas extra space to div
49
+ const style = document.createElement('style');
50
+ document.head.appendChild(style);
51
+ style.sheet?.insertRule('body > div:last-child img { display: inline-block; }');
52
+ // create pdf page
53
+ const canvas = await html2canvas(element, { scale: 3.0 });
54
+ const data = canvas.toDataURL('image/png');
55
+ const imgProperties = pdf.getImageProperties(data);
56
+ const pdfWidth = pdf.internal.pageSize.getWidth();
57
+ const pdfHeight = (imgProperties.height * pdfWidth) / imgProperties.width;
58
+ pdf.addImage(data, 'PNG', 0, 0, pdfWidth, pdfHeight, '', 'FAST');
59
+ element.style.width = originalWidth;
60
+ if (elements.length > 1 && i < elements.length - 1) {
61
+ pdf.addPage();
62
+ }
63
+ }
64
+ return pdf;
268
65
  };
66
+ //# sourceMappingURL=pdf.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@driveflux/frontend-pdf",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -16,8 +16,8 @@
16
16
  "jspdf": "^3.0.1"
17
17
  },
18
18
  "devDependencies": {
19
- "@driveflux/fab": "3.0.0",
20
- "@driveflux/tsconfig": "2.0.0",
19
+ "@driveflux/fab": "3.0.2",
20
+ "@driveflux/tsconfig": "2.0.2",
21
21
  "@swc/cli": "0.7.5",
22
22
  "@swc/core": "1.11.24",
23
23
  "@types/lodash": "^4.17.16",