@capawesome/capacitor-pdf-generator 0.0.1
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/CapawesomeCapacitorPdfGenerator.podspec +17 -0
- package/LICENSE +21 -0
- package/Package.swift +28 -0
- package/README.md +219 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/android/print/PdfGeneratorPrintDriver.java +92 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/PdfGenerator.java +255 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/PdfGeneratorPlugin.java +88 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/classes/CustomException.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/classes/CustomExceptions.java +15 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/classes/options/GenerateFromHtmlOptions.java +45 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/classes/options/GenerateFromUrlOptions.java +30 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/classes/options/GenerateOptions.java +80 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/classes/results/GenerateResult.java +25 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +401 -0
- package/dist/esm/definitions.d.ts +204 -0
- package/dist/esm/definitions.js +77 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +6 -0
- package/dist/esm/web.js +10 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +101 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +104 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Classes/Options/GenerateFromHtmlOptions.swift +27 -0
- package/ios/Plugin/Classes/Options/GenerateFromUrlOptions.swift +21 -0
- package/ios/Plugin/Classes/Options/GenerateOptions.swift +47 -0
- package/ios/Plugin/Classes/Results/GenerateResult.swift +16 -0
- package/ios/Plugin/Enums/CustomError.swift +48 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/PdfGenerator.swift +171 -0
- package/ios/Plugin/PdfGeneratorPlugin.swift +63 -0
- package/ios/Plugin/Protocols/Result.swift +6 -0
- package/package.json +94 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAIjD,MAAM,YAAY,GAAG,cAAc,CAAqB,cAAc,EAAE;IACtE,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,eAAe,EAAE,CAAC;CAC9D,CAAC,CAAC;AAEH,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,CAAC","sourcesContent":["import { registerPlugin } from '@capacitor/core';\n\nimport type { PdfGeneratorPlugin } from './definitions';\n\nconst PdfGenerator = registerPlugin<PdfGeneratorPlugin>('PdfGenerator', {\n web: () => import('./web').then(m => new m.PdfGeneratorWeb()),\n});\n\nexport * from './definitions';\nexport { PdfGenerator };\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
import type { GenerateFromHtmlOptions, GenerateFromUrlOptions, GenerateResult, PdfGeneratorPlugin } from './definitions';
|
|
3
|
+
export declare class PdfGeneratorWeb extends WebPlugin implements PdfGeneratorPlugin {
|
|
4
|
+
generateFromHtml(_options: GenerateFromHtmlOptions): Promise<GenerateResult>;
|
|
5
|
+
generateFromUrl(_options: GenerateFromUrlOptions): Promise<GenerateResult>;
|
|
6
|
+
}
|
package/dist/esm/web.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
export class PdfGeneratorWeb extends WebPlugin {
|
|
3
|
+
async generateFromHtml(_options) {
|
|
4
|
+
throw this.unimplemented('Not implemented on web.');
|
|
5
|
+
}
|
|
6
|
+
async generateFromUrl(_options) {
|
|
7
|
+
throw this.unimplemented('Not implemented on web.');
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=web.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAS5C,MAAM,OAAO,eAAgB,SAAQ,SAAS;IAC5C,KAAK,CAAC,gBAAgB,CACpB,QAAiC;QAEjC,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC,CAAC;IACtD,CAAC;IAED,KAAK,CAAC,eAAe,CACnB,QAAgC;QAEhC,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC,CAAC;IACtD,CAAC;CACF","sourcesContent":["import { WebPlugin } from '@capacitor/core';\n\nimport type {\n GenerateFromHtmlOptions,\n GenerateFromUrlOptions,\n GenerateResult,\n PdfGeneratorPlugin,\n} from './definitions';\n\nexport class PdfGeneratorWeb extends WebPlugin implements PdfGeneratorPlugin {\n async generateFromHtml(\n _options: GenerateFromHtmlOptions,\n ): Promise<GenerateResult> {\n throw this.unimplemented('Not implemented on web.');\n }\n\n async generateFromUrl(\n _options: GenerateFromUrlOptions,\n ): Promise<GenerateResult> {\n throw this.unimplemented('Not implemented on web.');\n }\n}\n"]}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@capacitor/core');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The page orientation of the generated PDF file.
|
|
7
|
+
*
|
|
8
|
+
* @since 0.1.0
|
|
9
|
+
*/
|
|
10
|
+
exports.Orientation = void 0;
|
|
11
|
+
(function (Orientation) {
|
|
12
|
+
/**
|
|
13
|
+
* Landscape orientation.
|
|
14
|
+
*
|
|
15
|
+
* @since 0.1.0
|
|
16
|
+
*/
|
|
17
|
+
Orientation["Landscape"] = "LANDSCAPE";
|
|
18
|
+
/**
|
|
19
|
+
* Portrait orientation.
|
|
20
|
+
*
|
|
21
|
+
* @since 0.1.0
|
|
22
|
+
*/
|
|
23
|
+
Orientation["Portrait"] = "PORTRAIT";
|
|
24
|
+
})(exports.Orientation || (exports.Orientation = {}));
|
|
25
|
+
/**
|
|
26
|
+
* The page size of the generated PDF file.
|
|
27
|
+
*
|
|
28
|
+
* @since 0.1.0
|
|
29
|
+
*/
|
|
30
|
+
exports.PageSize = void 0;
|
|
31
|
+
(function (PageSize) {
|
|
32
|
+
/**
|
|
33
|
+
* ISO A3 (297 x 420 mm).
|
|
34
|
+
*
|
|
35
|
+
* @since 0.1.0
|
|
36
|
+
*/
|
|
37
|
+
PageSize["A3"] = "A3";
|
|
38
|
+
/**
|
|
39
|
+
* ISO A4 (210 x 297 mm).
|
|
40
|
+
*
|
|
41
|
+
* @since 0.1.0
|
|
42
|
+
*/
|
|
43
|
+
PageSize["A4"] = "A4";
|
|
44
|
+
/**
|
|
45
|
+
* ISO A5 (148 x 210 mm).
|
|
46
|
+
*
|
|
47
|
+
* @since 0.1.0
|
|
48
|
+
*/
|
|
49
|
+
PageSize["A5"] = "A5";
|
|
50
|
+
/**
|
|
51
|
+
* US Letter (8.5 x 11 in).
|
|
52
|
+
*
|
|
53
|
+
* @since 0.1.0
|
|
54
|
+
*/
|
|
55
|
+
PageSize["Letter"] = "LETTER";
|
|
56
|
+
})(exports.PageSize || (exports.PageSize = {}));
|
|
57
|
+
/**
|
|
58
|
+
* @since 0.1.0
|
|
59
|
+
*/
|
|
60
|
+
exports.ErrorCode = void 0;
|
|
61
|
+
(function (ErrorCode) {
|
|
62
|
+
/**
|
|
63
|
+
* The PDF file could not be generated.
|
|
64
|
+
*
|
|
65
|
+
* @since 0.1.0
|
|
66
|
+
*/
|
|
67
|
+
ErrorCode["GenerationFailed"] = "GENERATION_FAILED";
|
|
68
|
+
/**
|
|
69
|
+
* The HTML content or URL could not be loaded.
|
|
70
|
+
*
|
|
71
|
+
* @since 0.1.0
|
|
72
|
+
*/
|
|
73
|
+
ErrorCode["LoadFailed"] = "LOAD_FAILED";
|
|
74
|
+
/**
|
|
75
|
+
* The PDF generation timed out.
|
|
76
|
+
*
|
|
77
|
+
* @since 0.1.0
|
|
78
|
+
*/
|
|
79
|
+
ErrorCode["Timeout"] = "TIMEOUT";
|
|
80
|
+
})(exports.ErrorCode || (exports.ErrorCode = {}));
|
|
81
|
+
|
|
82
|
+
const PdfGenerator = core.registerPlugin('PdfGenerator', {
|
|
83
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.PdfGeneratorWeb()),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
class PdfGeneratorWeb extends core.WebPlugin {
|
|
87
|
+
async generateFromHtml(_options) {
|
|
88
|
+
throw this.unimplemented('Not implemented on web.');
|
|
89
|
+
}
|
|
90
|
+
async generateFromUrl(_options) {
|
|
91
|
+
throw this.unimplemented('Not implemented on web.');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
96
|
+
__proto__: null,
|
|
97
|
+
PdfGeneratorWeb: PdfGeneratorWeb
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
exports.PdfGenerator = PdfGenerator;
|
|
101
|
+
//# sourceMappingURL=plugin.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.cjs.js","sources":["esm/definitions.js","esm/index.js","esm/web.js"],"sourcesContent":["/**\n * The page orientation of the generated PDF file.\n *\n * @since 0.1.0\n */\nexport var Orientation;\n(function (Orientation) {\n /**\n * Landscape orientation.\n *\n * @since 0.1.0\n */\n Orientation[\"Landscape\"] = \"LANDSCAPE\";\n /**\n * Portrait orientation.\n *\n * @since 0.1.0\n */\n Orientation[\"Portrait\"] = \"PORTRAIT\";\n})(Orientation || (Orientation = {}));\n/**\n * The page size of the generated PDF file.\n *\n * @since 0.1.0\n */\nexport var PageSize;\n(function (PageSize) {\n /**\n * ISO A3 (297 x 420 mm).\n *\n * @since 0.1.0\n */\n PageSize[\"A3\"] = \"A3\";\n /**\n * ISO A4 (210 x 297 mm).\n *\n * @since 0.1.0\n */\n PageSize[\"A4\"] = \"A4\";\n /**\n * ISO A5 (148 x 210 mm).\n *\n * @since 0.1.0\n */\n PageSize[\"A5\"] = \"A5\";\n /**\n * US Letter (8.5 x 11 in).\n *\n * @since 0.1.0\n */\n PageSize[\"Letter\"] = \"LETTER\";\n})(PageSize || (PageSize = {}));\n/**\n * @since 0.1.0\n */\nexport var ErrorCode;\n(function (ErrorCode) {\n /**\n * The PDF file could not be generated.\n *\n * @since 0.1.0\n */\n ErrorCode[\"GenerationFailed\"] = \"GENERATION_FAILED\";\n /**\n * The HTML content or URL could not be loaded.\n *\n * @since 0.1.0\n */\n ErrorCode[\"LoadFailed\"] = \"LOAD_FAILED\";\n /**\n * The PDF generation timed out.\n *\n * @since 0.1.0\n */\n ErrorCode[\"Timeout\"] = \"TIMEOUT\";\n})(ErrorCode || (ErrorCode = {}));\n//# sourceMappingURL=definitions.js.map","import { registerPlugin } from '@capacitor/core';\nconst PdfGenerator = registerPlugin('PdfGenerator', {\n web: () => import('./web').then(m => new m.PdfGeneratorWeb()),\n});\nexport * from './definitions';\nexport { PdfGenerator };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class PdfGeneratorWeb extends WebPlugin {\n async generateFromHtml(_options) {\n throw this.unimplemented('Not implemented on web.');\n }\n async generateFromUrl(_options) {\n throw this.unimplemented('Not implemented on web.');\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["Orientation","PageSize","ErrorCode","registerPlugin","WebPlugin"],"mappings":";;;;AAAA;AACA;AACA;AACA;AACA;AACWA;AACX,CAAC,UAAU,WAAW,EAAE;AACxB;AACA;AACA;AACA;AACA;AACA,IAAI,WAAW,CAAC,WAAW,CAAC,GAAG,WAAW;AAC1C;AACA;AACA;AACA;AACA;AACA,IAAI,WAAW,CAAC,UAAU,CAAC,GAAG,UAAU;AACxC,CAAC,EAAEA,mBAAW,KAAKA,mBAAW,GAAG,EAAE,CAAC,CAAC;AACrC;AACA;AACA;AACA;AACA;AACWC;AACX,CAAC,UAAU,QAAQ,EAAE;AACrB;AACA;AACA;AACA;AACA;AACA,IAAI,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI;AACzB;AACA;AACA;AACA;AACA;AACA,IAAI,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI;AACzB;AACA;AACA;AACA;AACA;AACA,IAAI,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI;AACzB;AACA;AACA;AACA;AACA;AACA,IAAI,QAAQ,CAAC,QAAQ,CAAC,GAAG,QAAQ;AACjC,CAAC,EAAEA,gBAAQ,KAAKA,gBAAQ,GAAG,EAAE,CAAC,CAAC;AAC/B;AACA;AACA;AACWC;AACX,CAAC,UAAU,SAAS,EAAE;AACtB;AACA;AACA;AACA;AACA;AACA,IAAI,SAAS,CAAC,kBAAkB,CAAC,GAAG,mBAAmB;AACvD;AACA;AACA;AACA;AACA;AACA,IAAI,SAAS,CAAC,YAAY,CAAC,GAAG,aAAa;AAC3C;AACA;AACA;AACA;AACA;AACA,IAAI,SAAS,CAAC,SAAS,CAAC,GAAG,SAAS;AACpC,CAAC,EAAEA,iBAAS,KAAKA,iBAAS,GAAG,EAAE,CAAC,CAAC;;AC1E5B,MAAC,YAAY,GAAGC,mBAAc,CAAC,cAAc,EAAE;AACpD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,eAAe,EAAE,CAAC;AACjE,CAAC;;ACFM,MAAM,eAAe,SAASC,cAAS,CAAC;AAC/C,IAAI,MAAM,gBAAgB,CAAC,QAAQ,EAAE;AACrC,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC;AAC3D,IAAI;AACJ,IAAI,MAAM,eAAe,CAAC,QAAQ,EAAE;AACpC,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC;AAC3D,IAAI;AACJ;;;;;;;;;"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
var capacitorPdfGenerator = (function (exports, core) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The page orientation of the generated PDF file.
|
|
6
|
+
*
|
|
7
|
+
* @since 0.1.0
|
|
8
|
+
*/
|
|
9
|
+
exports.Orientation = void 0;
|
|
10
|
+
(function (Orientation) {
|
|
11
|
+
/**
|
|
12
|
+
* Landscape orientation.
|
|
13
|
+
*
|
|
14
|
+
* @since 0.1.0
|
|
15
|
+
*/
|
|
16
|
+
Orientation["Landscape"] = "LANDSCAPE";
|
|
17
|
+
/**
|
|
18
|
+
* Portrait orientation.
|
|
19
|
+
*
|
|
20
|
+
* @since 0.1.0
|
|
21
|
+
*/
|
|
22
|
+
Orientation["Portrait"] = "PORTRAIT";
|
|
23
|
+
})(exports.Orientation || (exports.Orientation = {}));
|
|
24
|
+
/**
|
|
25
|
+
* The page size of the generated PDF file.
|
|
26
|
+
*
|
|
27
|
+
* @since 0.1.0
|
|
28
|
+
*/
|
|
29
|
+
exports.PageSize = void 0;
|
|
30
|
+
(function (PageSize) {
|
|
31
|
+
/**
|
|
32
|
+
* ISO A3 (297 x 420 mm).
|
|
33
|
+
*
|
|
34
|
+
* @since 0.1.0
|
|
35
|
+
*/
|
|
36
|
+
PageSize["A3"] = "A3";
|
|
37
|
+
/**
|
|
38
|
+
* ISO A4 (210 x 297 mm).
|
|
39
|
+
*
|
|
40
|
+
* @since 0.1.0
|
|
41
|
+
*/
|
|
42
|
+
PageSize["A4"] = "A4";
|
|
43
|
+
/**
|
|
44
|
+
* ISO A5 (148 x 210 mm).
|
|
45
|
+
*
|
|
46
|
+
* @since 0.1.0
|
|
47
|
+
*/
|
|
48
|
+
PageSize["A5"] = "A5";
|
|
49
|
+
/**
|
|
50
|
+
* US Letter (8.5 x 11 in).
|
|
51
|
+
*
|
|
52
|
+
* @since 0.1.0
|
|
53
|
+
*/
|
|
54
|
+
PageSize["Letter"] = "LETTER";
|
|
55
|
+
})(exports.PageSize || (exports.PageSize = {}));
|
|
56
|
+
/**
|
|
57
|
+
* @since 0.1.0
|
|
58
|
+
*/
|
|
59
|
+
exports.ErrorCode = void 0;
|
|
60
|
+
(function (ErrorCode) {
|
|
61
|
+
/**
|
|
62
|
+
* The PDF file could not be generated.
|
|
63
|
+
*
|
|
64
|
+
* @since 0.1.0
|
|
65
|
+
*/
|
|
66
|
+
ErrorCode["GenerationFailed"] = "GENERATION_FAILED";
|
|
67
|
+
/**
|
|
68
|
+
* The HTML content or URL could not be loaded.
|
|
69
|
+
*
|
|
70
|
+
* @since 0.1.0
|
|
71
|
+
*/
|
|
72
|
+
ErrorCode["LoadFailed"] = "LOAD_FAILED";
|
|
73
|
+
/**
|
|
74
|
+
* The PDF generation timed out.
|
|
75
|
+
*
|
|
76
|
+
* @since 0.1.0
|
|
77
|
+
*/
|
|
78
|
+
ErrorCode["Timeout"] = "TIMEOUT";
|
|
79
|
+
})(exports.ErrorCode || (exports.ErrorCode = {}));
|
|
80
|
+
|
|
81
|
+
const PdfGenerator = core.registerPlugin('PdfGenerator', {
|
|
82
|
+
web: () => Promise.resolve().then(function () { return web; }).then(m => new m.PdfGeneratorWeb()),
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
class PdfGeneratorWeb extends core.WebPlugin {
|
|
86
|
+
async generateFromHtml(_options) {
|
|
87
|
+
throw this.unimplemented('Not implemented on web.');
|
|
88
|
+
}
|
|
89
|
+
async generateFromUrl(_options) {
|
|
90
|
+
throw this.unimplemented('Not implemented on web.');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
var web = /*#__PURE__*/Object.freeze({
|
|
95
|
+
__proto__: null,
|
|
96
|
+
PdfGeneratorWeb: PdfGeneratorWeb
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
exports.PdfGenerator = PdfGenerator;
|
|
100
|
+
|
|
101
|
+
return exports;
|
|
102
|
+
|
|
103
|
+
})({}, capacitorExports);
|
|
104
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["esm/definitions.js","esm/index.js","esm/web.js"],"sourcesContent":["/**\n * The page orientation of the generated PDF file.\n *\n * @since 0.1.0\n */\nexport var Orientation;\n(function (Orientation) {\n /**\n * Landscape orientation.\n *\n * @since 0.1.0\n */\n Orientation[\"Landscape\"] = \"LANDSCAPE\";\n /**\n * Portrait orientation.\n *\n * @since 0.1.0\n */\n Orientation[\"Portrait\"] = \"PORTRAIT\";\n})(Orientation || (Orientation = {}));\n/**\n * The page size of the generated PDF file.\n *\n * @since 0.1.0\n */\nexport var PageSize;\n(function (PageSize) {\n /**\n * ISO A3 (297 x 420 mm).\n *\n * @since 0.1.0\n */\n PageSize[\"A3\"] = \"A3\";\n /**\n * ISO A4 (210 x 297 mm).\n *\n * @since 0.1.0\n */\n PageSize[\"A4\"] = \"A4\";\n /**\n * ISO A5 (148 x 210 mm).\n *\n * @since 0.1.0\n */\n PageSize[\"A5\"] = \"A5\";\n /**\n * US Letter (8.5 x 11 in).\n *\n * @since 0.1.0\n */\n PageSize[\"Letter\"] = \"LETTER\";\n})(PageSize || (PageSize = {}));\n/**\n * @since 0.1.0\n */\nexport var ErrorCode;\n(function (ErrorCode) {\n /**\n * The PDF file could not be generated.\n *\n * @since 0.1.0\n */\n ErrorCode[\"GenerationFailed\"] = \"GENERATION_FAILED\";\n /**\n * The HTML content or URL could not be loaded.\n *\n * @since 0.1.0\n */\n ErrorCode[\"LoadFailed\"] = \"LOAD_FAILED\";\n /**\n * The PDF generation timed out.\n *\n * @since 0.1.0\n */\n ErrorCode[\"Timeout\"] = \"TIMEOUT\";\n})(ErrorCode || (ErrorCode = {}));\n//# sourceMappingURL=definitions.js.map","import { registerPlugin } from '@capacitor/core';\nconst PdfGenerator = registerPlugin('PdfGenerator', {\n web: () => import('./web').then(m => new m.PdfGeneratorWeb()),\n});\nexport * from './definitions';\nexport { PdfGenerator };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class PdfGeneratorWeb extends WebPlugin {\n async generateFromHtml(_options) {\n throw this.unimplemented('Not implemented on web.');\n }\n async generateFromUrl(_options) {\n throw this.unimplemented('Not implemented on web.');\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["Orientation","PageSize","ErrorCode","registerPlugin","WebPlugin"],"mappings":";;;IAAA;IACA;IACA;IACA;IACA;AACWA;IACX,CAAC,UAAU,WAAW,EAAE;IACxB;IACA;IACA;IACA;IACA;IACA,IAAI,WAAW,CAAC,WAAW,CAAC,GAAG,WAAW;IAC1C;IACA;IACA;IACA;IACA;IACA,IAAI,WAAW,CAAC,UAAU,CAAC,GAAG,UAAU;IACxC,CAAC,EAAEA,mBAAW,KAAKA,mBAAW,GAAG,EAAE,CAAC,CAAC;IACrC;IACA;IACA;IACA;IACA;AACWC;IACX,CAAC,UAAU,QAAQ,EAAE;IACrB;IACA;IACA;IACA;IACA;IACA,IAAI,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI;IACzB;IACA;IACA;IACA;IACA;IACA,IAAI,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI;IACzB;IACA;IACA;IACA;IACA;IACA,IAAI,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI;IACzB;IACA;IACA;IACA;IACA;IACA,IAAI,QAAQ,CAAC,QAAQ,CAAC,GAAG,QAAQ;IACjC,CAAC,EAAEA,gBAAQ,KAAKA,gBAAQ,GAAG,EAAE,CAAC,CAAC;IAC/B;IACA;IACA;AACWC;IACX,CAAC,UAAU,SAAS,EAAE;IACtB;IACA;IACA;IACA;IACA;IACA,IAAI,SAAS,CAAC,kBAAkB,CAAC,GAAG,mBAAmB;IACvD;IACA;IACA;IACA;IACA;IACA,IAAI,SAAS,CAAC,YAAY,CAAC,GAAG,aAAa;IAC3C;IACA;IACA;IACA;IACA;IACA,IAAI,SAAS,CAAC,SAAS,CAAC,GAAG,SAAS;IACpC,CAAC,EAAEA,iBAAS,KAAKA,iBAAS,GAAG,EAAE,CAAC,CAAC;;AC1E5B,UAAC,YAAY,GAAGC,mBAAc,CAAC,cAAc,EAAE;IACpD,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,eAAe,EAAE,CAAC;IACjE,CAAC;;ICFM,MAAM,eAAe,SAASC,cAAS,CAAC;IAC/C,IAAI,MAAM,gBAAgB,CAAC,QAAQ,EAAE;IACrC,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC;IAC3D,IAAI;IACJ,IAAI,MAAM,eAAe,CAAC,QAAQ,EAAE;IACpC,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,yBAAyB,CAAC;IAC3D,IAAI;IACJ;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class GenerateFromHtmlOptions: GenerateOptions {
|
|
5
|
+
let baseUrl: URL?
|
|
6
|
+
let html: String
|
|
7
|
+
|
|
8
|
+
override init(_ call: CAPPluginCall) throws {
|
|
9
|
+
self.baseUrl = GenerateFromHtmlOptions.getBaseUrlFromCall(call)
|
|
10
|
+
self.html = try GenerateFromHtmlOptions.getHtmlFromCall(call)
|
|
11
|
+
try super.init(call)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
private static func getBaseUrlFromCall(_ call: CAPPluginCall) -> URL? {
|
|
15
|
+
guard let baseUrl = call.getString("baseUrl") else {
|
|
16
|
+
return nil
|
|
17
|
+
}
|
|
18
|
+
return URL(string: baseUrl)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private static func getHtmlFromCall(_ call: CAPPluginCall) throws -> String {
|
|
22
|
+
guard let html = call.getString("html") else {
|
|
23
|
+
throw CustomError.htmlMissing
|
|
24
|
+
}
|
|
25
|
+
return html
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class GenerateFromUrlOptions: GenerateOptions {
|
|
5
|
+
let url: URL
|
|
6
|
+
|
|
7
|
+
override init(_ call: CAPPluginCall) throws {
|
|
8
|
+
self.url = try GenerateFromUrlOptions.getUrlFromCall(call)
|
|
9
|
+
try super.init(call)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
private static func getUrlFromCall(_ call: CAPPluginCall) throws -> URL {
|
|
13
|
+
guard let urlString = call.getString("url") else {
|
|
14
|
+
throw CustomError.urlMissing
|
|
15
|
+
}
|
|
16
|
+
guard let url = URL(string: urlString) else {
|
|
17
|
+
throw CustomError.urlInvalid
|
|
18
|
+
}
|
|
19
|
+
return url
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class GenerateOptions: NSObject {
|
|
5
|
+
let fileName: String
|
|
6
|
+
let paperSize: CGSize
|
|
7
|
+
let timeout: Int
|
|
8
|
+
|
|
9
|
+
init(_ call: CAPPluginCall) throws {
|
|
10
|
+
self.fileName = GenerateOptions.getFileNameFromCall(call)
|
|
11
|
+
self.paperSize = try GenerateOptions.getPaperSizeFromCall(call)
|
|
12
|
+
self.timeout = GenerateOptions.getTimeoutFromCall(call)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
private static func getFileNameFromCall(_ call: CAPPluginCall) -> String {
|
|
16
|
+
let fileName = call.getString("fileName") ?? UUID().uuidString + ".pdf"
|
|
17
|
+
return fileName.hasSuffix(".pdf") ? fileName : fileName + ".pdf"
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private static func getPaperSizeFromCall(_ call: CAPPluginCall) throws -> CGSize {
|
|
21
|
+
let pageSize: CGSize
|
|
22
|
+
switch call.getString("pageSize") ?? "A4" {
|
|
23
|
+
case "A3":
|
|
24
|
+
pageSize = CGSize(width: 842, height: 1191)
|
|
25
|
+
case "A4":
|
|
26
|
+
pageSize = CGSize(width: 595, height: 842)
|
|
27
|
+
case "A5":
|
|
28
|
+
pageSize = CGSize(width: 420, height: 595)
|
|
29
|
+
case "LETTER":
|
|
30
|
+
pageSize = CGSize(width: 612, height: 792)
|
|
31
|
+
default:
|
|
32
|
+
throw CustomError.pageSizeInvalid
|
|
33
|
+
}
|
|
34
|
+
switch call.getString("orientation") ?? "PORTRAIT" {
|
|
35
|
+
case "LANDSCAPE":
|
|
36
|
+
return CGSize(width: pageSize.height, height: pageSize.width)
|
|
37
|
+
case "PORTRAIT":
|
|
38
|
+
return pageSize
|
|
39
|
+
default:
|
|
40
|
+
throw CustomError.orientationInvalid
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private static func getTimeoutFromCall(_ call: CAPPluginCall) -> Int {
|
|
45
|
+
return call.getInt("timeout") ?? 30000
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class GenerateResult: NSObject, Result {
|
|
5
|
+
let url: URL
|
|
6
|
+
|
|
7
|
+
init(url: URL) {
|
|
8
|
+
self.url = url
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["path"] = url.absoluteString
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
enum CustomError: Error {
|
|
4
|
+
case generationFailed
|
|
5
|
+
case htmlMissing
|
|
6
|
+
case loadFailed
|
|
7
|
+
case orientationInvalid
|
|
8
|
+
case pageSizeInvalid
|
|
9
|
+
case timeout
|
|
10
|
+
case urlInvalid
|
|
11
|
+
case urlMissing
|
|
12
|
+
|
|
13
|
+
var code: String? {
|
|
14
|
+
switch self {
|
|
15
|
+
case .generationFailed:
|
|
16
|
+
return "GENERATION_FAILED"
|
|
17
|
+
case .loadFailed:
|
|
18
|
+
return "LOAD_FAILED"
|
|
19
|
+
case .timeout:
|
|
20
|
+
return "TIMEOUT"
|
|
21
|
+
default:
|
|
22
|
+
return nil
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
extension CustomError: LocalizedError {
|
|
28
|
+
public var errorDescription: String? {
|
|
29
|
+
switch self {
|
|
30
|
+
case .generationFailed:
|
|
31
|
+
return NSLocalizedString("The PDF file could not be generated.", comment: "generationFailed")
|
|
32
|
+
case .htmlMissing:
|
|
33
|
+
return NSLocalizedString("html must be provided.", comment: "htmlMissing")
|
|
34
|
+
case .loadFailed:
|
|
35
|
+
return NSLocalizedString("The HTML content or URL could not be loaded.", comment: "loadFailed")
|
|
36
|
+
case .orientationInvalid:
|
|
37
|
+
return NSLocalizedString("orientation must be one of the supported values.", comment: "orientationInvalid")
|
|
38
|
+
case .pageSizeInvalid:
|
|
39
|
+
return NSLocalizedString("pageSize must be one of the supported values.", comment: "pageSizeInvalid")
|
|
40
|
+
case .timeout:
|
|
41
|
+
return NSLocalizedString("The PDF generation timed out.", comment: "timeout")
|
|
42
|
+
case .urlInvalid:
|
|
43
|
+
return NSLocalizedString("url must be a valid URL.", comment: "urlInvalid")
|
|
44
|
+
case .urlMissing:
|
|
45
|
+
return NSLocalizedString("url must be provided.", comment: "urlMissing")
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>CFBundleDevelopmentRegion</key>
|
|
6
|
+
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
|
7
|
+
<key>CFBundleExecutable</key>
|
|
8
|
+
<string>$(EXECUTABLE_NAME)</string>
|
|
9
|
+
<key>CFBundleIdentifier</key>
|
|
10
|
+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
|
11
|
+
<key>CFBundleInfoDictionaryVersion</key>
|
|
12
|
+
<string>6.0</string>
|
|
13
|
+
<key>CFBundleName</key>
|
|
14
|
+
<string>$(PRODUCT_NAME)</string>
|
|
15
|
+
<key>CFBundlePackageType</key>
|
|
16
|
+
<string>FMWK</string>
|
|
17
|
+
<key>CFBundleShortVersionString</key>
|
|
18
|
+
<string>1.0</string>
|
|
19
|
+
<key>CFBundleVersion</key>
|
|
20
|
+
<string>$(CURRENT_PROJECT_VERSION)</string>
|
|
21
|
+
<key>NSPrincipalClass</key>
|
|
22
|
+
<string></string>
|
|
23
|
+
</dict>
|
|
24
|
+
</plist>
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
import UIKit
|
|
4
|
+
import WebKit
|
|
5
|
+
|
|
6
|
+
@objc public class PdfGenerator: NSObject {
|
|
7
|
+
private let documentsDirectoryName = "capawesome_capacitor_pdf_generator_documents"
|
|
8
|
+
private let marginInPoints: CGFloat = 20
|
|
9
|
+
private let plugin: PdfGeneratorPlugin
|
|
10
|
+
private let settleDelayInSeconds: TimeInterval = 0.5
|
|
11
|
+
|
|
12
|
+
private var currentCompletion: ((GenerateResult?, Error?) -> Void)?
|
|
13
|
+
private var currentOptions: GenerateOptions?
|
|
14
|
+
private var currentSettleWorkItem: DispatchWorkItem?
|
|
15
|
+
private var currentTimeoutWorkItem: DispatchWorkItem?
|
|
16
|
+
private var currentWebView: WKWebView?
|
|
17
|
+
private var generating = false
|
|
18
|
+
private var queue: [() -> Void] = []
|
|
19
|
+
|
|
20
|
+
init(plugin: PdfGeneratorPlugin) {
|
|
21
|
+
self.plugin = plugin
|
|
22
|
+
super.init()
|
|
23
|
+
cleanUpDocumentsDirectory()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@objc public func generateFromHtml(_ options: GenerateFromHtmlOptions, completion: @escaping (GenerateResult?, Error?) -> Void) {
|
|
27
|
+
enqueue {
|
|
28
|
+
let webView = self.startGeneration(options, completion: completion)
|
|
29
|
+
webView.loadHTMLString(options.html, baseURL: options.baseUrl)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@objc public func generateFromUrl(_ options: GenerateFromUrlOptions, completion: @escaping (GenerateResult?, Error?) -> Void) {
|
|
34
|
+
enqueue {
|
|
35
|
+
let webView = self.startGeneration(options, completion: completion)
|
|
36
|
+
webView.load(URLRequest(url: options.url))
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private func cleanUpDocumentsDirectory() {
|
|
41
|
+
guard let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
let directory = cachesDirectory.appendingPathComponent(documentsDirectoryName)
|
|
45
|
+
try? FileManager.default.removeItem(at: directory)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private func createDocument() {
|
|
49
|
+
guard let options = currentOptions, let webView = currentWebView else {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
let renderer = UIPrintPageRenderer()
|
|
53
|
+
renderer.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAt: 0)
|
|
54
|
+
let paperRect = CGRect(origin: .zero, size: options.paperSize)
|
|
55
|
+
let printableRect = paperRect.insetBy(dx: marginInPoints, dy: marginInPoints)
|
|
56
|
+
// The `paperRect` and `printableRect` properties are read-only and can only be set via key-value coding
|
|
57
|
+
renderer.setValue(NSValue(cgRect: paperRect), forKey: "paperRect")
|
|
58
|
+
renderer.setValue(NSValue(cgRect: printableRect), forKey: "printableRect")
|
|
59
|
+
let pageCount = renderer.numberOfPages
|
|
60
|
+
if pageCount == 0 {
|
|
61
|
+
finish(nil, CustomError.generationFailed)
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
let data = NSMutableData()
|
|
65
|
+
UIGraphicsBeginPDFContextToData(data, paperRect, nil)
|
|
66
|
+
for pageIndex in 0..<pageCount {
|
|
67
|
+
UIGraphicsBeginPDFPage()
|
|
68
|
+
renderer.drawPage(at: pageIndex, in: UIGraphicsGetPDFContextBounds())
|
|
69
|
+
}
|
|
70
|
+
UIGraphicsEndPDFContext()
|
|
71
|
+
do {
|
|
72
|
+
let url = try getDocumentsDirectory().appendingPathComponent(options.fileName)
|
|
73
|
+
try data.write(to: url, options: .atomic)
|
|
74
|
+
finish(GenerateResult(url: url), nil)
|
|
75
|
+
} catch {
|
|
76
|
+
finish(nil, CustomError.generationFailed)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private func enqueue(_ task: @escaping () -> Void) {
|
|
81
|
+
DispatchQueue.main.async {
|
|
82
|
+
self.queue.append(task)
|
|
83
|
+
self.processQueue()
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private func finish(_ result: GenerateResult?, _ error: Error?) {
|
|
88
|
+
guard let completion = currentCompletion else {
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
currentSettleWorkItem?.cancel()
|
|
92
|
+
currentSettleWorkItem = nil
|
|
93
|
+
currentTimeoutWorkItem?.cancel()
|
|
94
|
+
currentTimeoutWorkItem = nil
|
|
95
|
+
currentWebView?.navigationDelegate = nil
|
|
96
|
+
currentWebView?.removeFromSuperview()
|
|
97
|
+
currentWebView = nil
|
|
98
|
+
currentCompletion = nil
|
|
99
|
+
currentOptions = nil
|
|
100
|
+
generating = false
|
|
101
|
+
processQueue()
|
|
102
|
+
completion(result, error)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private func getDocumentsDirectory() throws -> URL {
|
|
106
|
+
guard let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
|
|
107
|
+
throw CustomError.generationFailed
|
|
108
|
+
}
|
|
109
|
+
let directory = cachesDirectory.appendingPathComponent(documentsDirectoryName)
|
|
110
|
+
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
111
|
+
return directory
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private func handleTimeout() {
|
|
115
|
+
finish(nil, CustomError.timeout)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private func processQueue() {
|
|
119
|
+
if generating || queue.isEmpty {
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
generating = true
|
|
123
|
+
let task = queue.removeFirst()
|
|
124
|
+
task()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private func scheduleDocumentCreation() {
|
|
128
|
+
currentSettleWorkItem?.cancel()
|
|
129
|
+
let workItem = DispatchWorkItem { [weak self] in
|
|
130
|
+
self?.createDocument()
|
|
131
|
+
}
|
|
132
|
+
currentSettleWorkItem = workItem
|
|
133
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + settleDelayInSeconds, execute: workItem)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private func startGeneration(_ options: GenerateOptions, completion: @escaping (GenerateResult?, Error?) -> Void) -> WKWebView {
|
|
137
|
+
currentCompletion = completion
|
|
138
|
+
currentOptions = options
|
|
139
|
+
let webView = WKWebView(frame: CGRect(origin: .zero, size: options.paperSize))
|
|
140
|
+
webView.isHidden = true
|
|
141
|
+
webView.navigationDelegate = self
|
|
142
|
+
plugin.bridge?.viewController?.view.addSubview(webView)
|
|
143
|
+
currentWebView = webView
|
|
144
|
+
let workItem = DispatchWorkItem { [weak self] in
|
|
145
|
+
self?.handleTimeout()
|
|
146
|
+
}
|
|
147
|
+
currentTimeoutWorkItem = workItem
|
|
148
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + (Double(options.timeout) / 1000), execute: workItem)
|
|
149
|
+
return webView
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
extension PdfGenerator: WKNavigationDelegate {
|
|
154
|
+
public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
|
|
155
|
+
if webView == currentWebView {
|
|
156
|
+
finish(nil, CustomError.loadFailed)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
|
|
161
|
+
if webView == currentWebView {
|
|
162
|
+
finish(nil, CustomError.loadFailed)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
167
|
+
if webView == currentWebView {
|
|
168
|
+
scheduleDocumentCreation()
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc(PdfGeneratorPlugin)
|
|
5
|
+
public class PdfGeneratorPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
6
|
+
public let identifier = "PdfGeneratorPlugin"
|
|
7
|
+
public let jsName = "PdfGenerator"
|
|
8
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
9
|
+
CAPPluginMethod(name: "generateFromHtml", returnType: CAPPluginReturnPromise),
|
|
10
|
+
CAPPluginMethod(name: "generateFromUrl", returnType: CAPPluginReturnPromise)
|
|
11
|
+
]
|
|
12
|
+
public static let tag = "PdfGeneratorPlugin"
|
|
13
|
+
|
|
14
|
+
private var implementation: PdfGenerator?
|
|
15
|
+
|
|
16
|
+
override public func load() {
|
|
17
|
+
self.implementation = PdfGenerator(plugin: self)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@objc func generateFromHtml(_ call: CAPPluginCall) {
|
|
21
|
+
do {
|
|
22
|
+
let options = try GenerateFromHtmlOptions(call)
|
|
23
|
+
implementation?.generateFromHtml(options) { result, error in
|
|
24
|
+
if let error = error {
|
|
25
|
+
self.rejectCall(call, error)
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
self.resolveCall(call, result)
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
rejectCall(call, error)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@objc func generateFromUrl(_ call: CAPPluginCall) {
|
|
36
|
+
do {
|
|
37
|
+
let options = try GenerateFromUrlOptions(call)
|
|
38
|
+
implementation?.generateFromUrl(options) { result, error in
|
|
39
|
+
if let error = error {
|
|
40
|
+
self.rejectCall(call, error)
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
self.resolveCall(call, result)
|
|
44
|
+
}
|
|
45
|
+
} catch {
|
|
46
|
+
rejectCall(call, error)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private func rejectCall(_ call: CAPPluginCall, _ error: Error) {
|
|
51
|
+
CAPLog.print("[", PdfGeneratorPlugin.tag, "] ", error)
|
|
52
|
+
let code = (error as? CustomError)?.code
|
|
53
|
+
call.reject(error.localizedDescription, code)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private func resolveCall(_ call: CAPPluginCall, _ result: Result?) {
|
|
57
|
+
if let result = result?.toJSObject() as? JSObject {
|
|
58
|
+
call.resolve(result)
|
|
59
|
+
} else {
|
|
60
|
+
call.resolve()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|