@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
package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/PdfGenerator.java
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.pdfgenerator;
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint;
|
|
4
|
+
import android.os.Handler;
|
|
5
|
+
import android.os.Looper;
|
|
6
|
+
import android.print.PdfGeneratorPrintDriver;
|
|
7
|
+
import android.print.PrintAttributes;
|
|
8
|
+
import android.print.PrintDocumentAdapter;
|
|
9
|
+
import android.view.View;
|
|
10
|
+
import android.webkit.WebResourceError;
|
|
11
|
+
import android.webkit.WebResourceRequest;
|
|
12
|
+
import android.webkit.WebView;
|
|
13
|
+
import android.webkit.WebViewClient;
|
|
14
|
+
import androidx.annotation.NonNull;
|
|
15
|
+
import androidx.annotation.Nullable;
|
|
16
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.CustomException;
|
|
17
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.CustomExceptions;
|
|
18
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.options.GenerateFromHtmlOptions;
|
|
19
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.options.GenerateFromUrlOptions;
|
|
20
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.options.GenerateOptions;
|
|
21
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.results.GenerateResult;
|
|
22
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.interfaces.NonEmptyResultCallback;
|
|
23
|
+
import java.io.File;
|
|
24
|
+
import java.util.ArrayDeque;
|
|
25
|
+
|
|
26
|
+
public class PdfGenerator {
|
|
27
|
+
|
|
28
|
+
private static final String DOCUMENTS_DIRECTORY_NAME = "capawesome_capacitor_pdf_generator_documents";
|
|
29
|
+
private static final int MARGIN_IN_MILS = 278; // ~20 pt
|
|
30
|
+
private static final int SCREEN_RESOLUTION_IN_DPI = 96;
|
|
31
|
+
private static final long SETTLE_DELAY_IN_MILLISECONDS = 500;
|
|
32
|
+
|
|
33
|
+
@NonNull
|
|
34
|
+
private final Handler handler = new Handler(Looper.getMainLooper());
|
|
35
|
+
|
|
36
|
+
@NonNull
|
|
37
|
+
private final PdfGeneratorPlugin plugin;
|
|
38
|
+
|
|
39
|
+
@NonNull
|
|
40
|
+
private final ArrayDeque<Runnable> queue = new ArrayDeque<>();
|
|
41
|
+
|
|
42
|
+
@Nullable
|
|
43
|
+
private NonEmptyResultCallback<GenerateResult> currentCallback;
|
|
44
|
+
|
|
45
|
+
@Nullable
|
|
46
|
+
private GenerateOptions currentOptions;
|
|
47
|
+
|
|
48
|
+
@Nullable
|
|
49
|
+
private Runnable currentSettleRunnable;
|
|
50
|
+
|
|
51
|
+
@Nullable
|
|
52
|
+
private Runnable currentTimeoutRunnable;
|
|
53
|
+
|
|
54
|
+
@Nullable
|
|
55
|
+
private WebView currentWebView;
|
|
56
|
+
|
|
57
|
+
private boolean generating = false;
|
|
58
|
+
|
|
59
|
+
public PdfGenerator(@NonNull PdfGeneratorPlugin plugin) {
|
|
60
|
+
this.plugin = plugin;
|
|
61
|
+
cleanUpDocumentsDirectory();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public void generateFromHtml(@NonNull GenerateFromHtmlOptions options, @NonNull NonEmptyResultCallback<GenerateResult> callback) {
|
|
65
|
+
enqueue(() -> {
|
|
66
|
+
WebView webView = startGeneration(options, callback);
|
|
67
|
+
webView.loadDataWithBaseURL(options.getBaseUrl(), options.getHtml(), "text/html", "UTF-8", null);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public void generateFromUrl(@NonNull GenerateFromUrlOptions options, @NonNull NonEmptyResultCallback<GenerateResult> callback) {
|
|
72
|
+
enqueue(() -> {
|
|
73
|
+
WebView webView = startGeneration(options, callback);
|
|
74
|
+
webView.loadUrl(options.getUrl());
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private void cleanUpDocumentsDirectory() {
|
|
79
|
+
File[] files = getDocumentsDirectory().listFiles();
|
|
80
|
+
if (files == null) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
for (File file : files) {
|
|
84
|
+
file.delete();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private void createDocument() {
|
|
89
|
+
GenerateOptions options = currentOptions;
|
|
90
|
+
WebView webView = currentWebView;
|
|
91
|
+
if (options == null || webView == null) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
PrintAttributes attributes = new PrintAttributes.Builder()
|
|
95
|
+
.setMediaSize(options.getMediaSize())
|
|
96
|
+
.setMinMargins(new PrintAttributes.Margins(MARGIN_IN_MILS, MARGIN_IN_MILS, MARGIN_IN_MILS, MARGIN_IN_MILS))
|
|
97
|
+
.setResolution(new PrintAttributes.Resolution("pdf", "pdf", 600, 600))
|
|
98
|
+
.build();
|
|
99
|
+
File file = new File(getDocumentsDirectory(), options.getFileName());
|
|
100
|
+
PrintDocumentAdapter adapter = webView.createPrintDocumentAdapter(options.getFileName());
|
|
101
|
+
NonEmptyResultCallback<GenerateResult> callback = currentCallback;
|
|
102
|
+
PdfGeneratorPrintDriver.print(
|
|
103
|
+
adapter,
|
|
104
|
+
attributes,
|
|
105
|
+
file,
|
|
106
|
+
new PdfGeneratorPrintDriver.Callback() {
|
|
107
|
+
@Override
|
|
108
|
+
public void onFailure(@Nullable String message) {
|
|
109
|
+
handler.post(() -> {
|
|
110
|
+
if (currentCallback != callback) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
finishWithError(
|
|
114
|
+
message == null ? CustomExceptions.GENERATION_FAILED : new CustomException("GENERATION_FAILED", message)
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@Override
|
|
120
|
+
public void onSuccess() {
|
|
121
|
+
handler.post(() -> {
|
|
122
|
+
if (currentCallback != callback) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
finishWithSuccess(new GenerateResult(file));
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
@SuppressLint("SetJavaScriptEnabled")
|
|
133
|
+
@NonNull
|
|
134
|
+
private WebView createWebView(@NonNull GenerateOptions options) {
|
|
135
|
+
WebView webView = new WebView(plugin.getContext());
|
|
136
|
+
webView.getSettings().setJavaScriptEnabled(true);
|
|
137
|
+
webView.getSettings().setDomStorageEnabled(true);
|
|
138
|
+
int widthInPixels = Math.round((options.getMediaSize().getWidthMils() / 1000f) * SCREEN_RESOLUTION_IN_DPI);
|
|
139
|
+
int heightInPixels = Math.round((options.getMediaSize().getHeightMils() / 1000f) * SCREEN_RESOLUTION_IN_DPI);
|
|
140
|
+
webView.measure(
|
|
141
|
+
View.MeasureSpec.makeMeasureSpec(widthInPixels, View.MeasureSpec.EXACTLY),
|
|
142
|
+
View.MeasureSpec.makeMeasureSpec(heightInPixels, View.MeasureSpec.EXACTLY)
|
|
143
|
+
);
|
|
144
|
+
webView.layout(0, 0, widthInPixels, heightInPixels);
|
|
145
|
+
webView.setWebViewClient(createWebViewClient());
|
|
146
|
+
return webView;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@NonNull
|
|
150
|
+
private WebViewClient createWebViewClient() {
|
|
151
|
+
return new WebViewClient() {
|
|
152
|
+
@Override
|
|
153
|
+
public void onPageFinished(WebView view, String url) {
|
|
154
|
+
if (view == currentWebView) {
|
|
155
|
+
scheduleDocumentCreation();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@Override
|
|
160
|
+
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
|
|
161
|
+
if (view == currentWebView && request.isForMainFrame()) {
|
|
162
|
+
finishWithError(CustomExceptions.LOAD_FAILED);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private void enqueue(@NonNull Runnable task) {
|
|
169
|
+
handler.post(() -> {
|
|
170
|
+
queue.add(task);
|
|
171
|
+
processQueue();
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
@Nullable
|
|
176
|
+
private NonEmptyResultCallback<GenerateResult> finishGeneration() {
|
|
177
|
+
NonEmptyResultCallback<GenerateResult> callback = currentCallback;
|
|
178
|
+
if (callback == null) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
if (currentSettleRunnable != null) {
|
|
182
|
+
handler.removeCallbacks(currentSettleRunnable);
|
|
183
|
+
currentSettleRunnable = null;
|
|
184
|
+
}
|
|
185
|
+
if (currentTimeoutRunnable != null) {
|
|
186
|
+
handler.removeCallbacks(currentTimeoutRunnable);
|
|
187
|
+
currentTimeoutRunnable = null;
|
|
188
|
+
}
|
|
189
|
+
if (currentWebView != null) {
|
|
190
|
+
currentWebView.destroy();
|
|
191
|
+
currentWebView = null;
|
|
192
|
+
}
|
|
193
|
+
currentCallback = null;
|
|
194
|
+
currentOptions = null;
|
|
195
|
+
generating = false;
|
|
196
|
+
processQueue();
|
|
197
|
+
return callback;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private void finishWithError(@NonNull Exception exception) {
|
|
201
|
+
NonEmptyResultCallback<GenerateResult> callback = finishGeneration();
|
|
202
|
+
if (callback != null) {
|
|
203
|
+
callback.error(exception);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private void finishWithSuccess(@NonNull GenerateResult result) {
|
|
208
|
+
NonEmptyResultCallback<GenerateResult> callback = finishGeneration();
|
|
209
|
+
if (callback != null) {
|
|
210
|
+
callback.success(result);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@NonNull
|
|
215
|
+
private File getDocumentsDirectory() {
|
|
216
|
+
File directory = new File(plugin.getContext().getCacheDir(), DOCUMENTS_DIRECTORY_NAME);
|
|
217
|
+
directory.mkdirs();
|
|
218
|
+
return directory;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
private void handleTimeout() {
|
|
222
|
+
finishWithError(CustomExceptions.TIMEOUT);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private void processQueue() {
|
|
226
|
+
if (generating) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
Runnable task = queue.poll();
|
|
230
|
+
if (task == null) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
generating = true;
|
|
234
|
+
task.run();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private void scheduleDocumentCreation() {
|
|
238
|
+
if (currentSettleRunnable != null) {
|
|
239
|
+
handler.removeCallbacks(currentSettleRunnable);
|
|
240
|
+
}
|
|
241
|
+
currentSettleRunnable = this::createDocument;
|
|
242
|
+
handler.postDelayed(currentSettleRunnable, SETTLE_DELAY_IN_MILLISECONDS);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
@NonNull
|
|
246
|
+
private WebView startGeneration(@NonNull GenerateOptions options, @NonNull NonEmptyResultCallback<GenerateResult> callback) {
|
|
247
|
+
currentCallback = callback;
|
|
248
|
+
currentOptions = options;
|
|
249
|
+
WebView webView = createWebView(options);
|
|
250
|
+
currentWebView = webView;
|
|
251
|
+
currentTimeoutRunnable = this::handleTimeout;
|
|
252
|
+
handler.postDelayed(currentTimeoutRunnable, options.getTimeout());
|
|
253
|
+
return webView;
|
|
254
|
+
}
|
|
255
|
+
}
|
package/android/src/main/java/io/capawesome/capacitorjs/plugins/pdfgenerator/PdfGeneratorPlugin.java
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.pdfgenerator;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.Logger;
|
|
5
|
+
import com.getcapacitor.Plugin;
|
|
6
|
+
import com.getcapacitor.PluginCall;
|
|
7
|
+
import com.getcapacitor.PluginMethod;
|
|
8
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
9
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.CustomException;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.options.GenerateFromHtmlOptions;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.options.GenerateFromUrlOptions;
|
|
12
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.results.GenerateResult;
|
|
13
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.interfaces.NonEmptyResultCallback;
|
|
14
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.interfaces.Result;
|
|
15
|
+
|
|
16
|
+
@CapacitorPlugin(name = "PdfGenerator")
|
|
17
|
+
public class PdfGeneratorPlugin extends Plugin {
|
|
18
|
+
|
|
19
|
+
public static final String ERROR_UNKNOWN_ERROR = "An unknown error has occurred.";
|
|
20
|
+
public static final String TAG = "PdfGeneratorPlugin";
|
|
21
|
+
|
|
22
|
+
private PdfGenerator implementation;
|
|
23
|
+
|
|
24
|
+
@Override
|
|
25
|
+
public void load() {
|
|
26
|
+
super.load();
|
|
27
|
+
this.implementation = new PdfGenerator(this);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@PluginMethod
|
|
31
|
+
public void generateFromHtml(PluginCall call) {
|
|
32
|
+
try {
|
|
33
|
+
GenerateFromHtmlOptions options = new GenerateFromHtmlOptions(call);
|
|
34
|
+
NonEmptyResultCallback<GenerateResult> callback = new NonEmptyResultCallback<>() {
|
|
35
|
+
@Override
|
|
36
|
+
public void success(@NonNull GenerateResult result) {
|
|
37
|
+
resolveCall(call, result);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@Override
|
|
41
|
+
public void error(Exception exception) {
|
|
42
|
+
rejectCall(call, exception);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
implementation.generateFromHtml(options, callback);
|
|
46
|
+
} catch (Exception exception) {
|
|
47
|
+
rejectCall(call, exception);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@PluginMethod
|
|
52
|
+
public void generateFromUrl(PluginCall call) {
|
|
53
|
+
try {
|
|
54
|
+
GenerateFromUrlOptions options = new GenerateFromUrlOptions(call);
|
|
55
|
+
NonEmptyResultCallback<GenerateResult> callback = new NonEmptyResultCallback<>() {
|
|
56
|
+
@Override
|
|
57
|
+
public void success(@NonNull GenerateResult result) {
|
|
58
|
+
resolveCall(call, result);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Override
|
|
62
|
+
public void error(Exception exception) {
|
|
63
|
+
rejectCall(call, exception);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
implementation.generateFromUrl(options, callback);
|
|
67
|
+
} catch (Exception exception) {
|
|
68
|
+
rejectCall(call, exception);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
|
|
73
|
+
String message = exception.getMessage();
|
|
74
|
+
if (message == null) {
|
|
75
|
+
message = ERROR_UNKNOWN_ERROR;
|
|
76
|
+
}
|
|
77
|
+
String code = null;
|
|
78
|
+
if (exception instanceof CustomException) {
|
|
79
|
+
code = ((CustomException) exception).getCode();
|
|
80
|
+
}
|
|
81
|
+
Logger.error(TAG, message, exception);
|
|
82
|
+
call.reject(message, code);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private void resolveCall(@NonNull PluginCall call, @NonNull Result result) {
|
|
86
|
+
call.resolve(result.toJSObject());
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.pdfgenerator.classes;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
|
|
6
|
+
public class CustomException extends Exception {
|
|
7
|
+
|
|
8
|
+
@Nullable
|
|
9
|
+
private final String code;
|
|
10
|
+
|
|
11
|
+
public CustomException(@Nullable String code, @NonNull String message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Nullable
|
|
17
|
+
public String getCode() {
|
|
18
|
+
return code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.pdfgenerator.classes;
|
|
2
|
+
|
|
3
|
+
public class CustomExceptions {
|
|
4
|
+
|
|
5
|
+
public static final CustomException GENERATION_FAILED = new CustomException(
|
|
6
|
+
"GENERATION_FAILED",
|
|
7
|
+
"The PDF file could not be generated."
|
|
8
|
+
);
|
|
9
|
+
public static final CustomException HTML_MISSING = new CustomException(null, "html must be provided.");
|
|
10
|
+
public static final CustomException LOAD_FAILED = new CustomException("LOAD_FAILED", "The HTML content or URL could not be loaded.");
|
|
11
|
+
public static final CustomException ORIENTATION_INVALID = new CustomException(null, "orientation must be one of the supported values.");
|
|
12
|
+
public static final CustomException PAGE_SIZE_INVALID = new CustomException(null, "pageSize must be one of the supported values.");
|
|
13
|
+
public static final CustomException TIMEOUT = new CustomException("TIMEOUT", "The PDF generation timed out.");
|
|
14
|
+
public static final CustomException URL_MISSING = new CustomException(null, "url must be provided.");
|
|
15
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.pdfgenerator.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import androidx.annotation.Nullable;
|
|
5
|
+
import com.getcapacitor.PluginCall;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.CustomExceptions;
|
|
7
|
+
|
|
8
|
+
public class GenerateFromHtmlOptions extends GenerateOptions {
|
|
9
|
+
|
|
10
|
+
@Nullable
|
|
11
|
+
private final String baseUrl;
|
|
12
|
+
|
|
13
|
+
@NonNull
|
|
14
|
+
private final String html;
|
|
15
|
+
|
|
16
|
+
public GenerateFromHtmlOptions(@NonNull PluginCall call) throws Exception {
|
|
17
|
+
super(call);
|
|
18
|
+
this.baseUrl = GenerateFromHtmlOptions.getBaseUrlFromCall(call);
|
|
19
|
+
this.html = GenerateFromHtmlOptions.getHtmlFromCall(call);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@Nullable
|
|
23
|
+
public String getBaseUrl() {
|
|
24
|
+
return baseUrl;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@NonNull
|
|
28
|
+
public String getHtml() {
|
|
29
|
+
return html;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@Nullable
|
|
33
|
+
private static String getBaseUrlFromCall(@NonNull PluginCall call) {
|
|
34
|
+
return call.getString("baseUrl");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@NonNull
|
|
38
|
+
private static String getHtmlFromCall(@NonNull PluginCall call) throws Exception {
|
|
39
|
+
String html = call.getString("html");
|
|
40
|
+
if (html == null) {
|
|
41
|
+
throw CustomExceptions.HTML_MISSING;
|
|
42
|
+
}
|
|
43
|
+
return html;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.pdfgenerator.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.PluginCall;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.CustomExceptions;
|
|
6
|
+
|
|
7
|
+
public class GenerateFromUrlOptions extends GenerateOptions {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String url;
|
|
11
|
+
|
|
12
|
+
public GenerateFromUrlOptions(@NonNull PluginCall call) throws Exception {
|
|
13
|
+
super(call);
|
|
14
|
+
this.url = GenerateFromUrlOptions.getUrlFromCall(call);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@NonNull
|
|
18
|
+
public String getUrl() {
|
|
19
|
+
return url;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@NonNull
|
|
23
|
+
private static String getUrlFromCall(@NonNull PluginCall call) throws Exception {
|
|
24
|
+
String url = call.getString("url");
|
|
25
|
+
if (url == null) {
|
|
26
|
+
throw CustomExceptions.URL_MISSING;
|
|
27
|
+
}
|
|
28
|
+
return url;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.pdfgenerator.classes.options;
|
|
2
|
+
|
|
3
|
+
import android.print.PrintAttributes;
|
|
4
|
+
import androidx.annotation.NonNull;
|
|
5
|
+
import com.getcapacitor.PluginCall;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.classes.CustomExceptions;
|
|
7
|
+
import java.util.UUID;
|
|
8
|
+
|
|
9
|
+
public class GenerateOptions {
|
|
10
|
+
|
|
11
|
+
@NonNull
|
|
12
|
+
private final String fileName;
|
|
13
|
+
|
|
14
|
+
@NonNull
|
|
15
|
+
private final PrintAttributes.MediaSize mediaSize;
|
|
16
|
+
|
|
17
|
+
private final long timeout;
|
|
18
|
+
|
|
19
|
+
public GenerateOptions(@NonNull PluginCall call) throws Exception {
|
|
20
|
+
this.fileName = GenerateOptions.getFileNameFromCall(call);
|
|
21
|
+
this.mediaSize = GenerateOptions.getMediaSizeFromCall(call);
|
|
22
|
+
this.timeout = GenerateOptions.getTimeoutFromCall(call);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@NonNull
|
|
26
|
+
public String getFileName() {
|
|
27
|
+
return fileName;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@NonNull
|
|
31
|
+
public PrintAttributes.MediaSize getMediaSize() {
|
|
32
|
+
return mediaSize;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public long getTimeout() {
|
|
36
|
+
return timeout;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@NonNull
|
|
40
|
+
private static String getFileNameFromCall(@NonNull PluginCall call) {
|
|
41
|
+
String fileName = call.getString("fileName");
|
|
42
|
+
if (fileName == null) {
|
|
43
|
+
fileName = UUID.randomUUID() + ".pdf";
|
|
44
|
+
}
|
|
45
|
+
return fileName.endsWith(".pdf") ? fileName : fileName + ".pdf";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@NonNull
|
|
49
|
+
private static PrintAttributes.MediaSize getMediaSizeFromCall(@NonNull PluginCall call) throws Exception {
|
|
50
|
+
PrintAttributes.MediaSize mediaSize;
|
|
51
|
+
switch (call.getString("pageSize", "A4")) {
|
|
52
|
+
case "A3":
|
|
53
|
+
mediaSize = PrintAttributes.MediaSize.ISO_A3;
|
|
54
|
+
break;
|
|
55
|
+
case "A4":
|
|
56
|
+
mediaSize = PrintAttributes.MediaSize.ISO_A4;
|
|
57
|
+
break;
|
|
58
|
+
case "A5":
|
|
59
|
+
mediaSize = PrintAttributes.MediaSize.ISO_A5;
|
|
60
|
+
break;
|
|
61
|
+
case "LETTER":
|
|
62
|
+
mediaSize = PrintAttributes.MediaSize.NA_LETTER;
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
throw CustomExceptions.PAGE_SIZE_INVALID;
|
|
66
|
+
}
|
|
67
|
+
switch (call.getString("orientation", "PORTRAIT")) {
|
|
68
|
+
case "LANDSCAPE":
|
|
69
|
+
return mediaSize.asLandscape();
|
|
70
|
+
case "PORTRAIT":
|
|
71
|
+
return mediaSize.asPortrait();
|
|
72
|
+
default:
|
|
73
|
+
throw CustomExceptions.ORIENTATION_INVALID;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private static long getTimeoutFromCall(@NonNull PluginCall call) {
|
|
78
|
+
return call.getInt("timeout", 30000);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.pdfgenerator.classes.results;
|
|
2
|
+
|
|
3
|
+
import android.net.Uri;
|
|
4
|
+
import androidx.annotation.NonNull;
|
|
5
|
+
import com.getcapacitor.JSObject;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.pdfgenerator.interfaces.Result;
|
|
7
|
+
import java.io.File;
|
|
8
|
+
|
|
9
|
+
public class GenerateResult implements Result {
|
|
10
|
+
|
|
11
|
+
@NonNull
|
|
12
|
+
private final File file;
|
|
13
|
+
|
|
14
|
+
public GenerateResult(@NonNull File file) {
|
|
15
|
+
this.file = file;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Override
|
|
19
|
+
@NonNull
|
|
20
|
+
public JSObject toJSObject() {
|
|
21
|
+
JSObject result = new JSObject();
|
|
22
|
+
result.put("path", Uri.fromFile(file).toString());
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
File without changes
|