Skip to content

Instantly share code, notes, and snippets.

@lealhugui
Created December 28, 2017 14:02
Show Gist options
  • Save lealhugui/a873129f6d4a7c542a73f40047947c4f to your computer and use it in GitHub Desktop.
Save lealhugui/a873129f6d4a7c542a73f40047947c4f to your computer and use it in GitHub Desktop.
Base64 to PDF test page
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Generate PDF from b64</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<div style="margin: 0 auto; width: 90%; height: 90%; display: block;">
<button onclick="generatePdf();" style="margin: 0 auto; width: 100%; height: 10%;">GO</button>
<textarea id="b64Area" style="width: 100%; height: 90%;"></textarea>
</div>
<script type="text/javascript">
function b64ToBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
};
function generatePdf(){
var inp = document.getElementById('b64Area');
var file = URL.createObjectURL(b64ToBlob(inp.value));
window.open(file);
}
</script>
<style>
html, body {
height: 100%;
}
</style>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment