Skip to content

Commit

Permalink
feat(print): print Ionic service (#685)
Browse files Browse the repository at this point in the history
* add params to clusterStyle

* print with ionic

* commit lock

* print service extends

* Update index.ts

* Update print-ionic.service.ts

Co-authored-by: drekss <bovictor123>
Co-authored-by: Marc-André Barbeau <[email protected]>
  • Loading branch information
drekss and mbarbeau authored Jul 8, 2020
1 parent bf06867 commit 56dbb84
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
62 changes: 62 additions & 0 deletions packages/geo/src/lib/print/shared/print-ionic.service.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion packages/geo/src/lib/print/shared/print.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down

0 comments on commit 56dbb84

Please sign in to comment.