How to resolve PDF issue in Acorbat Reader download using Javascript in IONIC
Today I am going to show how to download the pdf using javascript. Basically while using some javascript code we are able to download the file and we can open pdf in like document viewer plugin or some other but when we try open the same file using acrobat reader you will get an error like document unable to open either it is corrupted or failed.
Get Viswa Kartheek Reddy’s stories in your inbox
Join Medium for free to get updates from this writer.
By resolving this issue we can do the following code to revolve this issue.
downloadPDFFromBinary(binary, fileName, isUIL = false) {let binaryFile = binary;if (!binary.startsWith('%PDF')) {binaryFile = `%PDF-1.6\n${binary}`;}binaryFile = this.convertToBuffer(binaryFile);const blobFile = new Blob([binaryFile], { type: 'application/pdf' });this.file.writeFile(this.file.dataDirectory, fileName, blobFile, { replace: true }).then(() => {this.showpdf(this.file.dataDirectory + fileName);}).catch((error: any) => {console.log(error);});}convertToBuffer(response: any) {const binary_string = response;const len = binary_string.length;const bytes = new Uint8Array(len);for (let i = 0; i < len; i++) {bytes[i] = binary_string.charCodeAt(i);}return bytes.buffer;}
Please let me know if you are facing issue while downloading and mention it in comments.
Happy Coding







