@capgo/capacitor-pdf-generator 8.0.17 → 8.0.18
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.
|
@@ -29,7 +29,7 @@ import java.util.Locale;
|
|
|
29
29
|
@CapacitorPlugin(name = "PdfGenerator")
|
|
30
30
|
public class PdfGeneratorPlugin extends Plugin {
|
|
31
31
|
|
|
32
|
-
private final String pluginVersion = "8.0.
|
|
32
|
+
private final String pluginVersion = "8.0.18";
|
|
33
33
|
|
|
34
34
|
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
35
35
|
private final List<PdfGenerationTask> tasks = new ArrayList<>();
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import Capacitor
|
|
2
2
|
import Foundation
|
|
3
|
+
import UIKit
|
|
3
4
|
import WebKit
|
|
4
5
|
|
|
5
6
|
@objc(PdfGeneratorPlugin)
|
|
6
7
|
public class PdfGeneratorPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
7
|
-
private let pluginVersion: String = "8.0.
|
|
8
|
+
private let pluginVersion: String = "8.0.18"
|
|
8
9
|
public let identifier = "PdfGeneratorPlugin"
|
|
9
10
|
public let jsName = "PdfGenerator"
|
|
10
11
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
@@ -119,6 +120,9 @@ private final class PdfGenerationTask: NSObject, WKNavigationDelegate {
|
|
|
119
120
|
case url(URL)
|
|
120
121
|
case html(String, URL?)
|
|
121
122
|
}
|
|
123
|
+
|
|
124
|
+
// PDF page margin in points (standard print margin)
|
|
125
|
+
private static let pdfPageMargin: CGFloat = 20.0
|
|
122
126
|
|
|
123
127
|
let call: CAPPluginCall
|
|
124
128
|
let options: PdfGeneratorOptions
|
|
@@ -197,17 +201,53 @@ private final class PdfGenerationTask: NSObject, WKNavigationDelegate {
|
|
|
197
201
|
return
|
|
198
202
|
}
|
|
199
203
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
webView.createPDF(configuration: configuration) { [weak self] result in
|
|
204
|
+
// All UIKit/UIGraphics operations must be on main thread
|
|
205
|
+
DispatchQueue.main.async { [weak self] in
|
|
204
206
|
guard let self else { return }
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
207
|
+
|
|
208
|
+
// Use UIPrintPageRenderer for proper multi-page PDF generation
|
|
209
|
+
let printFormatter = webView.viewPrintFormatter()
|
|
210
|
+
let renderer = UIPrintPageRenderer()
|
|
211
|
+
|
|
212
|
+
// Set up page size (A4 or A3) and printable area
|
|
213
|
+
let pageSize = self.options.pageSize
|
|
214
|
+
let pageRect = CGRect(origin: .zero, size: pageSize)
|
|
215
|
+
|
|
216
|
+
// Define printable area with standard print margins
|
|
217
|
+
let printableRect = pageRect.insetBy(dx: Self.pdfPageMargin, dy: Self.pdfPageMargin)
|
|
218
|
+
|
|
219
|
+
// Note: paperRect and printableRect are read-only properties with no public setter.
|
|
220
|
+
// KVC is the standard workaround for custom PDF generation outside the print dialog.
|
|
221
|
+
// This approach is documented in Apple's guides and widely used in production.
|
|
222
|
+
renderer.setValue(pageRect, forKey: "paperRect")
|
|
223
|
+
renderer.setValue(printableRect, forKey: "printableRect")
|
|
224
|
+
|
|
225
|
+
// Add the web view's print formatter to the renderer
|
|
226
|
+
renderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
|
|
227
|
+
|
|
228
|
+
// Prepare the renderer for drawing
|
|
229
|
+
renderer.prepare(forDrawingPages: NSRange(location: 0, length: renderer.numberOfPages))
|
|
230
|
+
|
|
231
|
+
// Check if we have any pages to render
|
|
232
|
+
guard renderer.numberOfPages > 0 else {
|
|
233
|
+
self.fail(with: "No content to generate PDF from")
|
|
234
|
+
return
|
|
210
235
|
}
|
|
236
|
+
|
|
237
|
+
// Create PDF data using UIGraphics context (must be on main thread)
|
|
238
|
+
let pdfData = NSMutableData()
|
|
239
|
+
UIGraphicsBeginPDFContextToData(pdfData, pageRect, nil)
|
|
240
|
+
|
|
241
|
+
for pageIndex in 0..<renderer.numberOfPages {
|
|
242
|
+
UIGraphicsBeginPDFPage()
|
|
243
|
+
let bounds = UIGraphicsGetPDFContextBounds()
|
|
244
|
+
renderer.drawPage(at: pageIndex, in: bounds)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
UIGraphicsEndPDFContext()
|
|
248
|
+
|
|
249
|
+
// Handle the generated PDF
|
|
250
|
+
self.plugin?.handle(pdfData: pdfData as Data, for: self)
|
|
211
251
|
}
|
|
212
252
|
}
|
|
213
253
|
}
|
package/package.json
CHANGED