Skip to main content

Convert Base64 code to PDF file in Angular

Component.ts:



import {DomSanitizer} from '@angular/platform-browser';
base64Code = 'your base64 code';


byteCharacters: any;
pdfUrl: any;
constructor(public sanitizer: DomSanitizer) {
}

convertToPdf() {
// tslint:disable-next-line:max-line-length
this.byteCharacters = this.base64Code;
// atob() function decodes a string of data which has been encoded using base-64 encoding
const byteCharacters = atob(this.base64Code);
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' });
this.pdfUrl = URL.createObjectURL(blob);


}



Component.html:

<embed [src]="sanitizer.bypassSecurityTrustResourceUrl(pdfUrl)"
type="application/pdf" width="700" height="400">

Comments

Post a Comment