kunalSBasic commented on issue #623: URL: https://github.com/apache/cordova-plugin-file/issues/623#issuecomment-2125395540
@breautek Sure here's the entire implementation. So I have a component called orderList in which I have a button which is ``` <div style="text-align: end"> <ion-icon class="downloadIcon" (click)="fetchPDF(item.id, item.performaNo, item.orderNo)" name="cloud-download-outline" slot="icon-only" ></ion-icon> </div> ``` Here's the fetchPDF method: ``` fetchPDF(invoiceId, proformaNo, orderNo) { let ID = proformaNo ? proformaNo : orderNo; let name = `${ID}_${Date.now()}.pdf`; this.service .downloadProformaOrderPDF(invoiceId, this.tenantId) .subscribe((result) => { this.base64PdfDownload.downloadPDF(result['file'], name); }); } ``` Once I fetch the pdf then I call the method downloadPDF by importing the service base64PdfDownload, of which the code is given below: ``` import { Injectable } from '@angular/core'; import { Platform } from '@ionic/angular'; import { File } from '@ionic-native/file/ngx'; import { ToastController } from '@ionic/angular'; @Injectable({ providedIn: 'root', }) export class Base64pdfdownloadService { constructor( private file: File, private platform: Platform, public toastController: ToastController ) {} downloadPDF(base64Data: string, fileName: string) { if (this.platform.is('cordova')) { // For mobile devices using Cordova File plugin this.downloadPdfMobile(base64Data, fileName); } else { // For web browsers this.downloadPdfWeb(base64Data, fileName); } } downloadPdfMobile(base64Data, fileName) { const folderPath = this.platform.is('android') ? this.file.externalRootDirectory + 'Download/' : this.file.documentsDirectory; console.log(folderPath, 'FOLDERPATH'); // Convert base64 to Blob const byteCharacters = atob(base64Data); const byteNumbers = new Array(byteCharacters.length); for (let i = 0; i < byteCharacters.length; i++) { byteNumbers[i] = byteCharacters.charCodeAt(i); } const byteArray = new Uint8Array(byteNumbers); const blob = new Blob([byteArray], { type: 'application/pdf' }); // Write the file this.file .writeFile(folderPath, fileName, blob, { replace: true, }) .then(() => { this.presentToast('File downloaded successfully.'); console.log('File downloaded successfully.'); }) .catch((error) => { this.presentToast('Error while downloading the file:'); console.error('Error while downloading the file:', error); }); } downloadPdfWeb(base64Data: string, fileName: string) { // Convert base64 to Blob const byteCharacters = atob(base64Data); const byteNumbers = new Array(byteCharacters.length); for (let i = 0; i < byteCharacters.length; i++) { byteNumbers[i] = byteCharacters.charCodeAt(i); } const byteArray = new Uint8Array(byteNumbers); const blob = new Blob([byteArray], { type: 'application/pdf' }); // Create download link const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = fileName; // Trigger download link.click(); } async presentToast(message) { const toast = await this.toastController.create({ message: message, duration: 2000, color: 'dark', }); toast.present(); } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org For additional commands, e-mail: issues-h...@cordova.apache.org