diff --git a/package-lock.json b/package-lock.json index acecfc9bae..6c39d990b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3535,6 +3535,36 @@ "@types/cordova": "^0.0.34" } }, + "@ionic-native/file": { + "version": "5.26.0", + "resolved": "https://registry.npmjs.org/@ionic-native/file/-/file-5.26.0.tgz", + "integrity": "sha512-sJONMhwEpZtONU59m0Rs6PmzD+C3lcaZkc7TryAXgF+9F6XUjhZu6efis3qzgxWHTMXNr7vBdrzfLAQB5bCGMA==", + "requires": { + "@types/cordova": "^0.0.34" + }, + "dependencies": { + "@types/cordova": { + "version": "0.0.34", + "resolved": "https://registry.npmjs.org/@types/cordova/-/cordova-0.0.34.tgz", + "integrity": "sha1-6nrd907Ow9dimCegw54smt3HPQQ=" + } + } + }, + "@ionic-native/file-opener": { + "version": "5.26.0", + "resolved": "https://registry.npmjs.org/@ionic-native/file-opener/-/file-opener-5.26.0.tgz", + "integrity": "sha512-X9pnrLg7NiiKSvz4d6U06EnGWONnmLljvYdc3rHTf2Kt3kD/ONQANshDlngX3HNA04Ziecbkx9bY00rtIB9UjA==", + "requires": { + "@types/cordova": "^0.0.34" + }, + "dependencies": { + "@types/cordova": { + "version": "0.0.34", + "resolved": "https://registry.npmjs.org/@types/cordova/-/cordova-0.0.34.tgz", + "integrity": "sha1-6nrd907Ow9dimCegw54smt3HPQQ=" + } + } + }, "@ionic-native/network": { "version": "5.16.0", "resolved": "https://registry.npmjs.org/@ionic-native/network/-/network-5.16.0.tgz", diff --git a/package.json b/package.json index 180861c9d5..362b5ac606 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,8 @@ "@ionic-native/core": "^5.10.0", "@ionic-native/network": "^5.12.0", "@ionic/angular": "^4.6.2", + "@ionic-native/file": "^5.6.0", + "@ionic-native/file-opener": "^5.6.0", "@mat-datetimepicker/core": "^3.0.0-beta.0", "@mdi/angular-material": "^4.7.95", "@ngx-translate/core": "^10.0.1", diff --git a/packages/geo/src/lib/print/shared/print-ionic.service.ts b/packages/geo/src/lib/print/shared/print-ionic.service.ts new file mode 100644 index 0000000000..47c2c09303 --- /dev/null +++ b/packages/geo/src/lib/print/shared/print-ionic.service.ts @@ -0,0 +1,62 @@ +import { Injectable } from '@angular/core'; +import * as jsPDF from 'jspdf'; +import { Platform } from '@ionic/angular'; +import { File } from '@ionic-native/file/ngx'; +import { FileOpener } from '@ionic-native/file-opener/ngx'; + +import { MessageService, ActivityService, LanguageService } from '@igo2/core'; +import { PrintService } from './print.service'; + +@Injectable({ + providedIn: 'root' +}) +export class PrintIonicService extends PrintService { + date: Date; + day: string; + month: any; + hour: string; + minute: string; + year: string; + + constructor( + messageService: MessageService, + activityService: ActivityService, + languageService: LanguageService, + private platform: Platform, + private fileOpener: FileOpener, + private file: File + ) { + super(messageService, activityService, languageService); + } + + protected saveDoc(doc: jsPDF) { + if (this.platform.is('cordova')) { + const docOutput = doc.output(); + const buffer = new ArrayBuffer(docOutput.length); + const array = new Uint8Array(buffer); + this.setDate(); + for (let i = 0; i < docOutput.length; i++) { + array[i] = docOutput.charCodeAt(i); + } + const fileName = 'map' + this.year + '-' + this.month + '-' + this.day + '-' + this.hour + '-' + this.minute + '.pdf'; + const directory = this.file.externalRootDirectory + 'Download'; + this.file.writeFile(directory, fileName, buffer, { replace: true }).then((success) => + this.fileOpener.open(directory + '/' + fileName, 'application/pdf')); + } else { + super.saveDoc(doc); + } + } + private setDate() { + this.date = new Date(); + this.day = this.date.getDate().toString(); + this.month = this.date.getMonth() + 1; + if (this.month < 10) { + this.month = '0' + this.month.toString(); + } else { + this.month = this.month.toString(); + } + this.year = this.date.getFullYear().toString(); + this.hour = this.date.getHours().toString(); + this.minute = this.date.getMinutes().toString(); + } +} diff --git a/packages/geo/src/lib/print/shared/print.service.ts b/packages/geo/src/lib/print/shared/print.service.ts index 22f4edc122..d4c4bd6691 100644 --- a/packages/geo/src/lib/print/shared/print.service.ts +++ b/packages/geo/src/lib/print/shared/print.service.ts @@ -594,7 +594,7 @@ export class PrintService { * Save document * @param doc - Document to save */ - private saveDoc(doc: jsPDF) { + protected saveDoc(doc: jsPDF) { doc.save('map.pdf'); }