Skip to content

Instantly share code, notes, and snippets.

@trevorfoskett
Last active November 5, 2019 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trevorfoskett/7bdff5ec339976a6f89738ded97e04e3 to your computer and use it in GitHub Desktop.
Save trevorfoskett/7bdff5ec339976a6f89738ded97e04e3 to your computer and use it in GitHub Desktop.
Convert the base-64 encoded blob generated on the server into an array buffer for encryption.
/*
* Handles client-side conversion of document
* data from server to a format we can use for encryption.
* Called in both download & email workflows.
*
* @return {arrayBuffer} Array Buffer of the document content.
*/
async function convertPDFData() {
// Gets document content; converts
// document content back into a usable
// PDF-type blob.
var blobB64 = await genPDF();
var binary = atob(blobB64.replace(/\s/g, ''));
var len = binary.length;
var buffer = new ArrayBuffer(len);
var view = new Uint8Array(buffer);
for (var i =0; i < len; i++) {
view[i] = binary.charCodeAt(i);
}
var blob = new Blob([view], {type: "application/pdf"});
// Generates an array buffer from the blob.
// Will be used as source data for encryption.
var arrayBuffer = null;
arrayBuffer = await new Response(blob).arrayBuffer();
return arrayBuffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment