@driveflux/frontend-pdf 3.0.3 → 3.0.4

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,159 +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), d = Object.defineProperty;
68
- return d(g, "next", {
69
- value: verb(0)
70
- }), d(g, "throw", {
71
- value: verb(1)
72
- }), d(g, "return", {
73
- value: verb(2)
74
- }), typeof Symbol === "function" && d(g, Symbol.iterator, {
75
- value: function() {
76
- return this;
77
- }
78
- }), g;
79
- function verb(n) {
80
- return function(v) {
81
- return step([
82
- n,
83
- v
84
- ]);
85
- };
86
- }
87
- function step(op) {
88
- if (f) throw new TypeError("Generator is already executing.");
89
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
90
- 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;
91
- if (y = 0, t) op = [
92
- op[0] & 2,
93
- t.value
94
- ];
95
- switch(op[0]){
96
- case 0:
97
- case 1:
98
- t = op;
99
- break;
100
- case 4:
101
- _.label++;
102
- return {
103
- value: op[1],
104
- done: false
105
- };
106
- case 5:
107
- _.label++;
108
- y = op[1];
109
- op = [
110
- 0
111
- ];
112
- continue;
113
- case 7:
114
- op = _.ops.pop();
115
- _.trys.pop();
116
- continue;
117
- default:
118
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
119
- _ = 0;
120
- continue;
121
- }
122
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
123
- _.label = op[1];
124
- break;
125
- }
126
- if (op[0] === 6 && _.label < t[1]) {
127
- _.label = t[1];
128
- t = op;
129
- break;
130
- }
131
- if (t && _.label < t[2]) {
132
- _.label = t[2];
133
- _.ops.push(op);
134
- break;
135
- }
136
- if (t[2]) _.ops.pop();
137
- _.trys.pop();
138
- continue;
139
- }
140
- op = body.call(thisArg, _);
141
- } catch (e) {
142
- op = [
143
- 6,
144
- e
145
- ];
146
- y = 0;
147
- } finally{
148
- f = t = 0;
149
- }
150
- if (op[0] & 5) throw op[1];
151
- return {
152
- value: op[0] ? op[1] : void 0,
153
- done: true
154
- };
155
- }
156
- }
157
1
  import html2canvasPro from 'html2canvas-pro';
158
2
  import jsPDF from 'jspdf';
159
3
  /**
@@ -164,26 +8,10 @@ import jsPDF from 'jspdf';
164
8
  *
165
9
  * @example
166
10
  * prepareElementsToDownload(element, 'receipt.pdf', { unit: 'mm' });
167
- */ export var prepareElementsToDownload = function prepareElementsToDownload(elements) {
168
- var pdfName = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'print.pdf', options = arguments.length > 2 ? arguments[2] : void 0;
169
- return _async_to_generator(function() {
170
- var pdf;
171
- return _ts_generator(this, function(_state) {
172
- switch(_state.label){
173
- case 0:
174
- return [
175
- 4,
176
- generatePdf(elements, options)
177
- ];
178
- case 1:
179
- pdf = _state.sent();
180
- pdf.save(pdfName);
181
- return [
182
- 2
183
- ];
184
- }
185
- });
186
- })();
11
+ */
12
+ export const prepareElementsToDownload = async (elements, pdfName = 'print.pdf', options) => {
13
+ const pdf = await generatePdf(elements, options);
14
+ pdf.save(pdfName);
187
15
  };
188
16
  /**
189
17
  *
@@ -193,86 +21,48 @@ import jsPDF from 'jspdf';
193
21
  *
194
22
  * @example
195
23
  * prepareElementsToPrint(element, 'receipt.pdf', { unit: 'mm' });
196
- */ export var prepareElementsToPrint = function prepareElementsToPrint(elements, options) {
197
- return _async_to_generator(function() {
198
- var pdf;
199
- return _ts_generator(this, function(_state) {
200
- switch(_state.label){
201
- case 0:
202
- return [
203
- 4,
204
- generatePdf(elements, options)
205
- ];
206
- case 1:
207
- pdf = _state.sent();
208
- pdf.autoPrint();
209
- window.open(pdf.output('bloburl'), '_blank');
210
- return [
211
- 2
212
- ];
213
- }
214
- });
215
- })();
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');
216
29
  };
217
- var defaultOptions = {
30
+ const defaultOptions = {
218
31
  unit: 'px',
219
- compress: true
32
+ compress: true,
220
33
  };
221
- var generatePdf = function generatePdf(elements, options) {
222
- return _async_to_generator(function() {
223
- var pdf, i, _style_sheet, element, originalWidth, style, canvas, data, imgProperties, pdfWidth, pdfHeight;
224
- return _ts_generator(this, function(_state) {
225
- switch(_state.label){
226
- case 0:
227
- pdf = new jsPDF(_object_spread({}, defaultOptions, options));
228
- i = 0;
229
- _state.label = 1;
230
- case 1:
231
- if (!(i < elements.length)) return [
232
- 3,
233
- 4
234
- ];
235
- element = elements[i];
236
- // set width to container
237
- originalWidth = element.style.width;
238
- // const originalHeight = element.style.height
239
- 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';
240
- 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';
241
- // workaround to remove canvas extra space to div
242
- style = document.createElement('style');
243
- document.head.appendChild(style);
244
- (_style_sheet = style.sheet) === null || _style_sheet === void 0 ? void 0 : _style_sheet.insertRule('body > div:last-child img { display: inline-block; }');
245
- return [
246
- 4,
247
- html2canvasPro(element, {
248
- scale: 3.0
249
- })
250
- ];
251
- case 2:
252
- canvas = _state.sent();
253
- data = canvas.toDataURL('image/jpeg');
254
- imgProperties = pdf.getImageProperties(data);
255
- pdfWidth = pdf.internal.pageSize.getWidth();
256
- pdfHeight = imgProperties.height * pdfWidth / imgProperties.width;
257
- pdf.addImage(data, 'PNG', 0, 0, pdfWidth, pdfHeight);
258
- element.style.width = originalWidth;
259
- // element.style.height = originalHeight
260
- if (elements.length > 1 && i < elements.length - 1) {
261
- pdf.addPage();
262
- }
263
- _state.label = 3;
264
- case 3:
265
- i++;
266
- return [
267
- 3,
268
- 1
269
- ];
270
- case 4:
271
- return [
272
- 2,
273
- pdf
274
- ];
275
- }
276
- });
277
- })();
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
+ // const originalHeight = element.style.height
41
+ element.style.width =
42
+ options?.orientation === 'l' || options?.orientation === 'landscape'
43
+ ? '1263px'
44
+ : '892.5px';
45
+ element.style.height =
46
+ options?.orientation === 'l' || options?.orientation === 'landscape'
47
+ ? '892.5px'
48
+ : '1263px';
49
+ // workaround to remove canvas extra space to div
50
+ const style = document.createElement('style');
51
+ document.head.appendChild(style);
52
+ style.sheet?.insertRule('body > div:last-child img { display: inline-block; }');
53
+ // create pdf page
54
+ const canvas = await html2canvasPro(element, { scale: 3.0 });
55
+ const data = canvas.toDataURL('image/jpeg');
56
+ const imgProperties = pdf.getImageProperties(data);
57
+ const pdfWidth = pdf.internal.pageSize.getWidth();
58
+ const pdfHeight = (imgProperties.height * pdfWidth) / imgProperties.width;
59
+ pdf.addImage(data, 'PNG', 0, 0, pdfWidth, pdfHeight);
60
+ element.style.width = originalWidth;
61
+ // element.style.height = originalHeight
62
+ if (elements.length > 1 && i < elements.length - 1) {
63
+ pdf.addPage();
64
+ }
65
+ }
66
+ return pdf;
278
67
  };
68
+ //# sourceMappingURL=pdf.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@driveflux/frontend-pdf",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -17,12 +17,12 @@
17
17
  "jspdf": "^4.2.1"
18
18
  },
19
19
  "devDependencies": {
20
- "@driveflux/fab": "4.0.1",
21
- "@driveflux/tsconfig": "3.0.1",
20
+ "@driveflux/fab": "4.0.2",
21
+ "@driveflux/tsconfig": "3.0.2",
22
22
  "@swc/cli": "^0.8.1",
23
23
  "@swc/core": "^1.15.33",
24
24
  "@types/lodash": "^4.17.24",
25
- "@types/node": "^25.7.0",
25
+ "@types/node": "^25.9.1",
26
26
  "del-cli": "^7.0.0",
27
27
  "typescript": "^6.0.3"
28
28
  },