@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,17 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'CapawesomeCapacitorPdfGenerator'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['repository']['url']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '15.0'
|
|
15
|
+
s.dependency 'Capacitor'
|
|
16
|
+
s.swift_version = '5.1'
|
|
17
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robin Genz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Package.swift
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
|
|
4
|
+
let package = Package(
|
|
5
|
+
name: "CapawesomeCapacitorPdfGenerator",
|
|
6
|
+
platforms: [.iOS(.v15)],
|
|
7
|
+
products: [
|
|
8
|
+
.library(
|
|
9
|
+
name: "CapawesomeCapacitorPdfGenerator",
|
|
10
|
+
targets: ["PdfGeneratorPlugin"])
|
|
11
|
+
],
|
|
12
|
+
dependencies: [
|
|
13
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0")
|
|
14
|
+
],
|
|
15
|
+
targets: [
|
|
16
|
+
.target(
|
|
17
|
+
name: "PdfGeneratorPlugin",
|
|
18
|
+
dependencies: [
|
|
19
|
+
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
20
|
+
.product(name: "Cordova", package: "capacitor-swift-pm")
|
|
21
|
+
],
|
|
22
|
+
path: "ios/Plugin"),
|
|
23
|
+
.testTarget(
|
|
24
|
+
name: "PdfGeneratorPluginTests",
|
|
25
|
+
dependencies: ["PdfGeneratorPlugin"],
|
|
26
|
+
path: "ios/PluginTests")
|
|
27
|
+
]
|
|
28
|
+
)
|
package/README.md
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# Capacitor PDF Generator Plugin
|
|
2
|
+
|
|
3
|
+
Capacitor plugin to generate paginated PDF files from HTML content or URLs.
|
|
4
|
+
|
|
5
|
+
<div class="capawesome-z29o10a">
|
|
6
|
+
<a href="https://cloud.capawesome.io/" target="_blank">
|
|
7
|
+
<img alt="Deliver Live Updates to your Capacitor app with Capawesome Cloud" src="https://cloud.capawesome.io/assets/banners/cloud-build-and-deploy-capacitor-apps.png?t=1" />
|
|
8
|
+
</a>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 📄 **HTML to PDF**: Generate PDF files from HTML strings, for example invoices, tickets, and reports.
|
|
14
|
+
- 🌐 **URL to PDF**: Generate PDF files from web pages.
|
|
15
|
+
- 📃 **Pagination**: Real page breaks with selectable page sizes (A3, A4, A5, Letter) and orientation.
|
|
16
|
+
- 🖋️ **Vector Output**: Text stays selectable and sharp at any zoom level.
|
|
17
|
+
- 📂 **File Output**: Results are written to a file so even large documents don't exhaust memory.
|
|
18
|
+
- ⏱️ **Timeout**: Configurable timeout so hung pages never hang your app.
|
|
19
|
+
- 🤝 **Compatibility**: Works alongside the [PDF Viewer](https://capawesome.io/docs/sdks/capacitor/pdf-viewer/), [Printer](https://capawesome.io/docs/sdks/capacitor/printer/) and [File Opener](https://capawesome.io/docs/sdks/capacitor/file-opener/) plugins.
|
|
20
|
+
- 🔒 **App Store safe**: Uses only official platform APIs.
|
|
21
|
+
- 📦 **CocoaPods & SPM**: Supports CocoaPods and Swift Package Manager for iOS.
|
|
22
|
+
- 🔁 **Up-to-date**: Always supports the latest Capacitor version.
|
|
23
|
+
|
|
24
|
+
Missing a feature? Just [open an issue](https://github.com/capawesome-team/capacitor-plugins/issues) and we'll take a look!
|
|
25
|
+
|
|
26
|
+
## Newsletter
|
|
27
|
+
|
|
28
|
+
Stay up to date with the latest news and updates about the Capawesome, Capacitor, and Ionic ecosystem by subscribing to our [Capawesome Newsletter](https://cloud.capawesome.io/newsletter/).
|
|
29
|
+
|
|
30
|
+
## Compatibility
|
|
31
|
+
|
|
32
|
+
| Plugin Version | Capacitor Version | Status |
|
|
33
|
+
| -------------- | ----------------- | -------------- |
|
|
34
|
+
| 0.x.x | >=8.x.x | Active support |
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
You can use our **AI-Assisted Setup** to install the plugin.
|
|
39
|
+
Add the [Capawesome Skills](https://github.com/capawesome-team/skills) to your AI tool using the following command:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npx skills add capawesome-team/skills --skill capacitor-plugins
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Then use the following prompt:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
Use the `capacitor-plugins` skill from `capawesome-team/skills` to install the `@capawesome/capacitor-pdf-generator` plugin in my project.
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
If you prefer **Manual Setup**, install the plugin by running the following commands and follow the platform-specific instructions below:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install @capawesome/capacitor-pdf-generator
|
|
55
|
+
npx cap sync
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Android
|
|
59
|
+
|
|
60
|
+
On Android, this plugin renders the HTML content in a hidden WebView and creates the PDF file with the [Android Print Framework](https://developer.android.com/training/printing). No additional configuration is required.
|
|
61
|
+
|
|
62
|
+
### iOS
|
|
63
|
+
|
|
64
|
+
On iOS, this plugin renders the HTML content in a hidden WKWebView and creates the PDF file with [UIKit](https://developer.apple.com/documentation/uikit). No additional configuration is required.
|
|
65
|
+
|
|
66
|
+
### Web
|
|
67
|
+
|
|
68
|
+
This plugin is not available on the web. Use `window.print()` with [print CSS](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_media_queries/Printing) to let the user save a web page as a PDF file.
|
|
69
|
+
|
|
70
|
+
## Configuration
|
|
71
|
+
|
|
72
|
+
No configuration required for this plugin.
|
|
73
|
+
|
|
74
|
+
## Usage
|
|
75
|
+
|
|
76
|
+
The generated PDF file is written to the cache directory and deleted on the next app launch. Move it to a permanent location if you want to keep it, for example with the `rename(...)` method of the [Filesystem](https://capacitorjs.com/docs/apis/filesystem) plugin. You can also hand the path to the [PDF Viewer](https://capawesome.io/docs/sdks/capacitor/pdf-viewer/), [Printer](https://capawesome.io/docs/sdks/capacitor/printer/), [File Opener](https://capawesome.io/docs/sdks/capacitor/file-opener/) or [Share](https://capacitorjs.com/docs/apis/share) plugin.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { PdfGenerator, Orientation, PageSize } from '@capawesome/capacitor-pdf-generator';
|
|
80
|
+
|
|
81
|
+
const generateFromHtml = async () => {
|
|
82
|
+
const { path } = await PdfGenerator.generateFromHtml({
|
|
83
|
+
html: '<h1>Invoice</h1><p>Thank you for your purchase!</p>',
|
|
84
|
+
fileName: 'invoice.pdf',
|
|
85
|
+
pageSize: PageSize.A4,
|
|
86
|
+
orientation: Orientation.Portrait,
|
|
87
|
+
});
|
|
88
|
+
return path;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const generateFromUrl = async () => {
|
|
92
|
+
const { path } = await PdfGenerator.generateFromUrl({
|
|
93
|
+
url: 'https://capawesome.io/',
|
|
94
|
+
pageSize: PageSize.Letter,
|
|
95
|
+
orientation: Orientation.Landscape,
|
|
96
|
+
timeout: 10000,
|
|
97
|
+
});
|
|
98
|
+
return path;
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## API
|
|
103
|
+
|
|
104
|
+
<docgen-index>
|
|
105
|
+
|
|
106
|
+
* [`generateFromHtml(...)`](#generatefromhtml)
|
|
107
|
+
* [`generateFromUrl(...)`](#generatefromurl)
|
|
108
|
+
* [Interfaces](#interfaces)
|
|
109
|
+
* [Enums](#enums)
|
|
110
|
+
|
|
111
|
+
</docgen-index>
|
|
112
|
+
|
|
113
|
+
<docgen-api>
|
|
114
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
115
|
+
|
|
116
|
+
### generateFromHtml(...)
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
generateFromHtml(options: GenerateFromHtmlOptions) => Promise<GenerateResult>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Generate a paginated PDF file from an HTML string.
|
|
123
|
+
|
|
124
|
+
Only available on Android and iOS.
|
|
125
|
+
|
|
126
|
+
| Param | Type |
|
|
127
|
+
| ------------- | --------------------------------------------------------------------------- |
|
|
128
|
+
| **`options`** | <code><a href="#generatefromhtmloptions">GenerateFromHtmlOptions</a></code> |
|
|
129
|
+
|
|
130
|
+
**Returns:** <code>Promise<<a href="#generateresult">GenerateResult</a>></code>
|
|
131
|
+
|
|
132
|
+
**Since:** 0.1.0
|
|
133
|
+
|
|
134
|
+
--------------------
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
### generateFromUrl(...)
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
generateFromUrl(options: GenerateFromUrlOptions) => Promise<GenerateResult>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Generate a paginated PDF file from a URL.
|
|
144
|
+
|
|
145
|
+
Only available on Android and iOS.
|
|
146
|
+
|
|
147
|
+
| Param | Type |
|
|
148
|
+
| ------------- | ------------------------------------------------------------------------- |
|
|
149
|
+
| **`options`** | <code><a href="#generatefromurloptions">GenerateFromUrlOptions</a></code> |
|
|
150
|
+
|
|
151
|
+
**Returns:** <code>Promise<<a href="#generateresult">GenerateResult</a>></code>
|
|
152
|
+
|
|
153
|
+
**Since:** 0.1.0
|
|
154
|
+
|
|
155
|
+
--------------------
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
### Interfaces
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
#### GenerateResult
|
|
162
|
+
|
|
163
|
+
| Prop | Type | Description | Since |
|
|
164
|
+
| ---------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
|
|
165
|
+
| **`path`** | <code>string</code> | The path of the generated PDF file. The file is stored in the cache directory and deleted on the next app launch. Move it to a permanent location if you want to keep it. | 0.1.0 |
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
#### GenerateFromHtmlOptions
|
|
169
|
+
|
|
170
|
+
| Prop | Type | Description | Default | Since |
|
|
171
|
+
| ----------------- | --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | ----- |
|
|
172
|
+
| **`baseUrl`** | <code>string</code> | The base URL to resolve relative resources (e.g. images, stylesheets) in the HTML content against. | | 0.1.0 |
|
|
173
|
+
| **`fileName`** | <code>string</code> | The name of the generated PDF file. If the name does not end with `.pdf`, the extension is appended. | <code>A randomly generated file name.</code> | 0.1.0 |
|
|
174
|
+
| **`html`** | <code>string</code> | The HTML content to generate the PDF file from. | | 0.1.0 |
|
|
175
|
+
| **`orientation`** | <code><a href="#orientation">Orientation</a></code> | The page orientation of the generated PDF file. | <code>Orientation.Portrait</code> | 0.1.0 |
|
|
176
|
+
| **`pageSize`** | <code><a href="#pagesize">PageSize</a></code> | The page size of the generated PDF file. | <code>PageSize.A4</code> | 0.1.0 |
|
|
177
|
+
| **`timeout`** | <code>number</code> | The maximum time in milliseconds to wait for the PDF generation to complete before the call is rejected with the `TIMEOUT` error code. | <code>30000</code> | 0.1.0 |
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
#### GenerateFromUrlOptions
|
|
181
|
+
|
|
182
|
+
| Prop | Type | Description | Default | Since |
|
|
183
|
+
| ----------------- | --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- | ----- |
|
|
184
|
+
| **`fileName`** | <code>string</code> | The name of the generated PDF file. If the name does not end with `.pdf`, the extension is appended. | <code>A randomly generated file name.</code> | 0.1.0 |
|
|
185
|
+
| **`orientation`** | <code><a href="#orientation">Orientation</a></code> | The page orientation of the generated PDF file. | <code>Orientation.Portrait</code> | 0.1.0 |
|
|
186
|
+
| **`pageSize`** | <code><a href="#pagesize">PageSize</a></code> | The page size of the generated PDF file. | <code>PageSize.A4</code> | 0.1.0 |
|
|
187
|
+
| **`timeout`** | <code>number</code> | The maximum time in milliseconds to wait for the PDF generation to complete before the call is rejected with the `TIMEOUT` error code. | <code>30000</code> | 0.1.0 |
|
|
188
|
+
| **`url`** | <code>string</code> | The URL of the web page to generate the PDF file from. | | 0.1.0 |
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
### Enums
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
#### Orientation
|
|
195
|
+
|
|
196
|
+
| Members | Value | Description | Since |
|
|
197
|
+
| --------------- | ------------------------ | ---------------------- | ----- |
|
|
198
|
+
| **`Landscape`** | <code>'LANDSCAPE'</code> | Landscape orientation. | 0.1.0 |
|
|
199
|
+
| **`Portrait`** | <code>'PORTRAIT'</code> | Portrait orientation. | 0.1.0 |
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
#### PageSize
|
|
203
|
+
|
|
204
|
+
| Members | Value | Description | Since |
|
|
205
|
+
| ------------ | --------------------- | ------------------------ | ----- |
|
|
206
|
+
| **`A3`** | <code>'A3'</code> | ISO A3 (297 x 420 mm). | 0.1.0 |
|
|
207
|
+
| **`A4`** | <code>'A4'</code> | ISO A4 (210 x 297 mm). | 0.1.0 |
|
|
208
|
+
| **`A5`** | <code>'A5'</code> | ISO A5 (148 x 210 mm). | 0.1.0 |
|
|
209
|
+
| **`Letter`** | <code>'LETTER'</code> | US Letter (8.5 x 11 in). | 0.1.0 |
|
|
210
|
+
|
|
211
|
+
</docgen-api>
|
|
212
|
+
|
|
213
|
+
## Changelog
|
|
214
|
+
|
|
215
|
+
See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/pdf-generator/CHANGELOG.md).
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/pdf-generator/LICENSE).
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
dependencies {
|
|
14
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
apply plugin: 'com.android.library'
|
|
19
|
+
|
|
20
|
+
android {
|
|
21
|
+
namespace = "io.capawesome.capacitorjs.plugins.pdfgenerator"
|
|
22
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
25
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.0"
|
|
28
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
29
|
+
}
|
|
30
|
+
buildTypes {
|
|
31
|
+
release {
|
|
32
|
+
minifyEnabled false
|
|
33
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
lintOptions {
|
|
37
|
+
abortOnError = false
|
|
38
|
+
}
|
|
39
|
+
compileOptions {
|
|
40
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
repositories {
|
|
46
|
+
google()
|
|
47
|
+
mavenCentral()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
dependencies {
|
|
52
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
53
|
+
implementation project(':capacitor-android')
|
|
54
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
55
|
+
testImplementation "junit:junit:$junitVersion"
|
|
56
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
57
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
58
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
package android.print;
|
|
2
|
+
|
|
3
|
+
import android.os.CancellationSignal;
|
|
4
|
+
import android.os.ParcelFileDescriptor;
|
|
5
|
+
import androidx.annotation.NonNull;
|
|
6
|
+
import androidx.annotation.Nullable;
|
|
7
|
+
import java.io.File;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Drives a {@link PrintDocumentAdapter} headlessly to write its content to a PDF file.
|
|
11
|
+
*
|
|
12
|
+
* <p>This class must be declared in the {@code android.print} package because the constructors of
|
|
13
|
+
* {@link PrintDocumentAdapter.LayoutResultCallback} and {@link PrintDocumentAdapter.WriteResultCallback}
|
|
14
|
+
* are package-private and can therefore only be subclassed from within this package.</p>
|
|
15
|
+
*/
|
|
16
|
+
public class PdfGeneratorPrintDriver {
|
|
17
|
+
|
|
18
|
+
public interface Callback {
|
|
19
|
+
void onFailure(@Nullable String message);
|
|
20
|
+
|
|
21
|
+
void onSuccess();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static void print(
|
|
25
|
+
@NonNull PrintDocumentAdapter adapter,
|
|
26
|
+
@NonNull PrintAttributes attributes,
|
|
27
|
+
@NonNull File file,
|
|
28
|
+
@NonNull Callback callback
|
|
29
|
+
) {
|
|
30
|
+
adapter.onStart();
|
|
31
|
+
adapter.onLayout(
|
|
32
|
+
null,
|
|
33
|
+
attributes,
|
|
34
|
+
new CancellationSignal(),
|
|
35
|
+
new PrintDocumentAdapter.LayoutResultCallback() {
|
|
36
|
+
@Override
|
|
37
|
+
public void onLayoutFailed(CharSequence error) {
|
|
38
|
+
adapter.onFinish();
|
|
39
|
+
callback.onFailure(error == null ? null : error.toString());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@Override
|
|
43
|
+
public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
|
|
44
|
+
write(adapter, file, callback);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
null
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private static void closeQuietly(@NonNull ParcelFileDescriptor descriptor) {
|
|
52
|
+
try {
|
|
53
|
+
descriptor.close();
|
|
54
|
+
} catch (Exception exception) {
|
|
55
|
+
// No operation
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private static void write(@NonNull PrintDocumentAdapter adapter, @NonNull File file, @NonNull Callback callback) {
|
|
60
|
+
ParcelFileDescriptor descriptor;
|
|
61
|
+
try {
|
|
62
|
+
descriptor = ParcelFileDescriptor.open(
|
|
63
|
+
file,
|
|
64
|
+
ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_TRUNCATE | ParcelFileDescriptor.MODE_READ_WRITE
|
|
65
|
+
);
|
|
66
|
+
} catch (Exception exception) {
|
|
67
|
+
adapter.onFinish();
|
|
68
|
+
callback.onFailure(exception.getMessage());
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
adapter.onWrite(
|
|
72
|
+
new PageRange[] { PageRange.ALL_PAGES },
|
|
73
|
+
descriptor,
|
|
74
|
+
new CancellationSignal(),
|
|
75
|
+
new PrintDocumentAdapter.WriteResultCallback() {
|
|
76
|
+
@Override
|
|
77
|
+
public void onWriteFailed(CharSequence error) {
|
|
78
|
+
closeQuietly(descriptor);
|
|
79
|
+
adapter.onFinish();
|
|
80
|
+
callback.onFailure(error == null ? null : error.toString());
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
@Override
|
|
84
|
+
public void onWriteFinished(PageRange[] pages) {
|
|
85
|
+
closeQuietly(descriptor);
|
|
86
|
+
adapter.onFinish();
|
|
87
|
+
callback.onSuccess();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|