Line data Source code
1 : import 'package:flutter/foundation.dart';
2 :
3 : import '../worker.dart';
4 :
5 : /// PDF page size for [PdfFromImagesWorker].
6 : enum PdfPageSize {
7 : /// ISO A4 (210×297 mm / 595×842 pt).
8 : a4,
9 :
10 : /// US Letter (215.9×279.4 mm / 612×792 pt).
11 : letter,
12 : }
13 :
14 : /// Merge multiple PDF files into a single output PDF.
15 : ///
16 : /// Uses Android `PdfRenderer`→`PdfDocument` (API 21+)
17 : /// and iOS `PDFKit` (iOS 11+).
18 : ///
19 : /// ```dart
20 : /// await NativeWorkManager.enqueue(
21 : /// taskId: 'merge-pdfs',
22 : /// worker: NativeWorker.pdfMerge(
23 : /// inputPaths: ['/data/.../a.pdf', '/data/.../b.pdf'],
24 : /// outputPath: '/data/.../merged.pdf',
25 : /// ),
26 : /// );
27 : /// ```
28 : ///
29 : /// **Result data:**
30 : /// ```json
31 : /// { "outputPath": "...", "outputSize": 102400, "pageCount": 5 }
32 : /// ```
33 : @immutable
34 : final class PdfMergeWorker extends Worker {
35 1 : const PdfMergeWorker({
36 : required this.inputPaths,
37 : required this.outputPath,
38 : });
39 :
40 : /// Ordered list of absolute paths to the PDF files to merge.
41 : final List<String> inputPaths;
42 :
43 : /// Absolute path for the merged output PDF.
44 : final String outputPath;
45 :
46 1 : @override
47 : String get workerClassName => 'PdfWorker';
48 :
49 1 : @override
50 1 : Map<String, dynamic> toMap() => {
51 : 'workerType': 'pdf',
52 : 'operation': 'merge',
53 1 : 'inputPaths': inputPaths,
54 1 : 'outputPath': outputPath,
55 : };
56 : }
57 :
58 : /// Re-render a PDF at reduced quality to shrink its file size.
59 : ///
60 : /// ```dart
61 : /// await NativeWorkManager.enqueue(
62 : /// taskId: 'compress-pdf',
63 : /// worker: NativeWorker.pdfCompress(
64 : /// inputPath: '/data/.../original.pdf',
65 : /// outputPath: '/data/.../compressed.pdf',
66 : /// quality: 60,
67 : /// ),
68 : /// );
69 : /// ```
70 : ///
71 : /// **[quality]:** 1–100. Default `80`. Lower values produce smaller files.
72 : ///
73 : /// **Result data:**
74 : /// ```json
75 : /// { "outputPath": "...", "outputSize": 51200, "pageCount": 3 }
76 : /// ```
77 : @immutable
78 : final class PdfCompressWorker extends Worker {
79 1 : const PdfCompressWorker({
80 : required this.inputPath,
81 : required this.outputPath,
82 : this.quality = 80,
83 : });
84 :
85 : /// Absolute path to the input PDF.
86 : final String inputPath;
87 :
88 : /// Absolute path for the compressed output PDF.
89 : final String outputPath;
90 :
91 : /// JPEG render quality (1–100). Default `80`.
92 : final int quality;
93 :
94 1 : @override
95 : String get workerClassName => 'PdfWorker';
96 :
97 1 : @override
98 1 : Map<String, dynamic> toMap() => {
99 : 'workerType': 'pdf',
100 : 'operation': 'compress',
101 1 : 'inputPath': inputPath,
102 1 : 'outputPath': outputPath,
103 1 : 'quality': quality,
104 : };
105 : }
106 :
107 : /// Convert a list of image files into a PDF (one image per page).
108 : ///
109 : /// Supported image formats: JPEG, PNG, and any format supported by the
110 : /// platform's native image decoder.
111 : ///
112 : /// ```dart
113 : /// await NativeWorkManager.enqueue(
114 : /// taskId: 'images-to-pdf',
115 : /// worker: NativeWorker.pdfFromImages(
116 : /// imagePaths: ['/data/.../1.jpg', '/data/.../2.png'],
117 : /// outputPath: '/data/.../album.pdf',
118 : /// pageSize: PdfPageSize.a4,
119 : /// ),
120 : /// );
121 : /// ```
122 : ///
123 : /// **Result data:**
124 : /// ```json
125 : /// { "outputPath": "...", "outputSize": 204800, "pageCount": 2 }
126 : /// ```
127 : @immutable
128 : final class PdfFromImagesWorker extends Worker {
129 1 : const PdfFromImagesWorker({
130 : required this.imagePaths,
131 : required this.outputPath,
132 : this.pageSize = PdfPageSize.a4,
133 : this.margin = 0,
134 : });
135 :
136 : /// Ordered list of absolute paths to the source images.
137 : final List<String> imagePaths;
138 :
139 : /// Absolute path for the output PDF.
140 : final String outputPath;
141 :
142 : /// Page dimensions. Defaults to [PdfPageSize.a4].
143 : final PdfPageSize pageSize;
144 :
145 : /// Page margin in PDF points (1 pt = 1/72 inch). Default `0`.
146 : final int margin;
147 :
148 1 : @override
149 : String get workerClassName => 'PdfWorker';
150 :
151 1 : @override
152 1 : Map<String, dynamic> toMap() => {
153 : 'workerType': 'pdf',
154 : 'operation': 'imagesToPdf',
155 1 : 'imagePaths': imagePaths,
156 1 : 'outputPath': outputPath,
157 2 : 'pageSize': pageSize == PdfPageSize.a4 ? 'A4' : 'letter',
158 1 : 'margin': margin,
159 : };
160 : }
|