-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathPDFExportCanvas.android.ts
More file actions
134 lines (128 loc) · 6.88 KB
/
Copy pathPDFExportCanvas.android.ts
File metadata and controls
134 lines (128 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Utils, knownFolders } from '@nativescript/core';
import { PDF_EXT } from '~/utils/constants';
import { getColorMatrix } from '~/utils/matrix';
import { PDFExportOptions } from './PDFCanvas';
import PDFExportCanvasBase from './PDFExportCanvas.common';
export default class PDFExportCanvas extends PDFExportCanvasBase {
async export({ compress = false, filename = Date.now() + PDF_EXT, folder = knownFolders.temp().path, pages }: PDFExportOptions) {
const start = Date.now();
pages.forEach((p) => {
const page = p.page;
if (page.colorType && !page.colorMatrix) {
page.colorMatrix = getColorMatrix(page.colorType);
}
});
const options = JSON.stringify({ ...this.options, pages });
DEV_LOG && console.log('PDFExportCanvas', 'export', folder, filename, compress, options);
const outputPath = com.akylas.documentscanner.utils.PDFUtils.Companion.generatePDF(Utils.android.getApplicationContext(), folder, filename, options);
DEV_LOG && console.log('PDFExportCanvas', 'export done', JSON.stringify(this.options), options.length, Date.now() - start, 'ms');
return outputPath;
// const options = this.options;
// if (options.paper_size === 'full') {
// // we enforce 1 item per page
// options.items_per_page = 1;
// }
// this.updatePages(pages);
// this.canvas = new Canvas();
// const pdfDocument = new android.graphics.pdf.PdfDocument();
// const items = this.items;
// for (let index = 0; index < items.length; index++) {
// const pages = items[index].pages;
// let pageWidth, pageHeight;
// switch (options.paper_size) {
// case 'a5':
// pageWidth = 420;
// pageHeight = 595;
// break;
// case 'a4':
// pageWidth = 595;
// pageHeight = 842;
// break;
// case 'a3':
// pageWidth = 842;
// pageHeight = 1191;
// break;
// case 'full':
// pageWidth = pages[0].width;
// pageHeight = pages[0].height;
// break;
// default:
// break;
// }
// if (options.paper_size !== 'full' && options.orientation === 'landscape') {
// const temp = pageWidth;
// pageWidth = pageHeight;
// pageHeight = temp;
// }
// const pageInfo = new android.graphics.pdf.PdfDocument.PageInfo.Builder(pageWidth, pageHeight, index).create();
// const page = pdfDocument.startPage(pageInfo);
// this.canvas.setNative(page.getCanvas());
// const scale = Screen.mainScreen.scale;
// this.canvas.scale(scale, scale);
// await this.loadImagesForPage(index);
// this.drawPages(index, items[index].pages, false, true);
// pdfDocument.finishPage(page);
// }
// DEV_LOG && console.log('PDFExportCanvas', 'export 1', Date.now() - start, 'ms');
// recycleImages(Object.values(this.imagesCache));
// DEV_LOG && console.log('PDFExportCanvas', 'export 2', Date.now() - start, 'ms');
// if (!compress) {
// if (folder.startsWith('content://')) {
// const outdocument = androidx.documentfile.provider.DocumentFile.fromTreeUri(Utils.android.getApplicationContext(), android.net.Uri.parse(folder));
// const outfile = outdocument.createFile('application/pdf', filename);
// const stream = Utils.android.getApplicationContext().getContentResolver().openOutputStream(outfile.getUri());
// pdfDocument.writeTo(stream);
// pdfDocument.close();
// DEV_LOG && console.log('PDFExportCanvas', 'export 3', Date.now() - start, 'ms');
// return outdocument.getUri().toString();
// } else {
// const pdfFile = new java.io.File(path.join(folder, filename));
// const stream = new java.io.FileOutputStream(pdfFile);
// pdfDocument.writeTo(stream);
// pdfDocument.close();
// DEV_LOG && console.log('PDFExportCanvas', 'export 3', Date.now() - start, 'ms');
// return pdfFile.getPath();
// }
// } else {
// // we need to compress the pdf
// const tempFile = knownFolders.temp().getFile(filename);
// const stream = new java.io.FileOutputStream(tempFile.path);
// pdfDocument.writeTo(stream);
// pdfDocument.close();
// DEV_LOG && console.log('PDFExportCanvas', 'export 3', Date.now() - start, 'ms');
// if (folder.startsWith('content://')) {
// const tempFile2 = knownFolders.temp().getFile('compressed.pdf');
// DEV_LOG && console.log('compressPDF', tempFile.path, tempFile2.path);
// com.akylas.documentscanner.utils.PDFUtils.Companion.compressPDF(tempFile.path, tempFile2.path, IMG_COMPRESS);
// const outdocument = androidx.documentfile.provider.DocumentFile.fromTreeUri(Utils.android.getApplicationContext(), android.net.Uri.parse(folder));
// let outfile = outdocument.createFile('application/pdf', filename);
// if (outfile == null) {
// outfile = outdocument.findFile(filename);
// }
// if (!outfile) {
// throw new Error(`error creating file "${filename}" in "${folder}"`);
// }
// await tempFile2.copy(outfile.getUri().toString());
// DEV_LOG && console.log('PDFExportCanvas', 'export 4', Date.now() - start, 'ms');
// return outdocument.getUri().toString();
// } else {
// let outputPath;
// if (knownFolders.temp().path === folder) {
// outputPath = path.join(folder, Date.now() + '_1.pdf');
// } else {
// outputPath = path.join(folder, filename);
// }
// try {
// DEV_LOG && console.log('compressPDF', tempFile.path, outputPath);
// com.akylas.documentscanner.utils.PDFUtils.Companion.compressPDF(tempFile.path, outputPath, IMG_COMPRESS);
// DEV_LOG && console.log('PDFExportCanvas', 'export 4', Date.now() - start, 'ms');
// } catch (error) {
// tempFile.copySync(outputPath);
// console.error('compressPDF error', error, error.stack);
// throw error;
// }
// return outputPath;
// }
// }
}
}