templates/pdf-to-base64.html.twig line 1

Open in your IDE?
  1. {% extends 'base.html.twig' %}
  2. {% block title %}
  3. ToBase64.dev - Convert pdf to base64, Encode pdf to base64
  4. {% endblock %}
  5. {% block h1 %}Convert your PDF to Base64{% endblock %}
  6. {% block body %}
  7. <div class="row" style="height: 60%;">
  8. <div class="padding col-5 d-flex flex-column">
  9. <div class="row" style="height:85%">
  10. <div class="col-md-12" style="height:100%">
  11. <div id="imageb64" style="
  12. border-left-width: 5px;
  13. border-top-width: 5px;
  14. border-right-width: 5px;
  15. border-bottom-width: 5px;
  16. border-color: #17a2b8;
  17. border-style: solid;
  18. height: 100%;
  19. width: 100%;
  20. ">
  21. <object id="pdfb64" data="" type="application/pdf" width="100%" height="100%">
  22. </object>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="custom-file" style="margin-top:auto">
  27. <input type="file" class="custom-file-input" id="files">
  28. <label class="custom-file-label" for="files">Choose file</label>
  29. </div>
  30. <!--<input class="form-control" type="file" id="files" name="file" style="width:100%;height: 100%;">-->
  31. </div>
  32. <div class="col-2 d-flex flex-column">
  33. <div class="padding row" style="margin-top:auto;margin-bottom:auto;">
  34. <button type="button" onclick="document.getElementById('output').value=imageB64" class="col-12 btn btn-info"><span class="spinner-border spinner-border-sm" id="loading" role="status" aria-hidden="true" style="display:none"></span><i class="fas fa-long-arrow-alt-right"></i></button>
  35. </div>
  36. </div>
  37. <div class="padding col-5 d-flex flex-column">
  38. <div style="height: 50%" class="d-flex flex-column">
  39. <p style="margin-bottom: 0; text-align: left;">Base64:</p>
  40. <div class="flex-grow-1">
  41. <textarea class="form-control" id="output1" name="output" placeholder="Output will be displayed here..." spellcheck="false" style="width: 100%;height: 100%;"></textarea>
  42. </div>
  43. <button id="download" type="button" class="btn btn-info mt-3" onclick="copyToClipboard('output1')" style="margin-top:auto;">Copy to clipboard!</button>
  44. </div>
  45. <div style="height: 50%" class="d-flex flex-column">
  46. <p style="margin-top:1.5em; margin-bottom: 0; text-align: left;">Base64 for use in &lt;object&gt; html elements:</p>
  47. <div class="flex-grow-1">
  48. <textarea class="form-control" id="output2" name="output" placeholder="Output will be displayed here..." spellcheck="false" style="width: 100%;height: 100%;"></textarea>
  49. </div>
  50. <button id="download" type="button" class="btn btn-info mt-3" onclick="copyToClipboard('output2')" style="margin-top:auto;">Copy to clipboard!</button>
  51. </div>
  52. </div>
  53. </div>
  54. <script type="text/javascript">
  55. // Add the following code if you want the name of the file appear on select
  56. $(".custom-file-input").on("change", function() {
  57. var fileName = $(this).val().split("\\").pop();
  58. $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
  59. });
  60. function copyToClipboard(output) {
  61. var copyText = document.getElementById(output);
  62. copyText.select();
  63. document.execCommand("copy");
  64. }
  65. function download() {
  66. var url = document.getElementById('output3').src.replace(/^data:image\/[^;]+/, 'data:image/png');
  67. window.open(url);
  68. }
  69. function toimage() {
  70. var string = document.getElementById('input3').value;
  71. var image = "data:image/png;base64," + string;
  72. document.getElementById('output3').src = image;
  73. document.getElementById('download').disabled = false;
  74. }
  75. function totext() {
  76. var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0;
  77. e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++);
  78. r = e.charCodeAt(f++);
  79. i = e.charCodeAt(f++);
  80. s = n >> 2;
  81. o = (n & 3) << 4 | r >> 4;
  82. u = (r & 15) << 2 | i >> 6;
  83. a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function(e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0;
  84. e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++));
  85. o = this._keyStr.indexOf(e.charAt(f++));
  86. u = this._keyStr.indexOf(e.charAt(f++));
  87. a = this._keyStr.indexOf(e.charAt(f++));
  88. n = s << 2 | o >> 4;
  89. r = (o & 15) << 4 | u >> 2;
  90. i = (u & 3) << 6 | a;
  91. t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function(e) { e = e.replace(/\r\n/g, "\n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192);
  92. t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224);
  93. t += String.fromCharCode(r >> 6 & 63 | 128);
  94. t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function(e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r);
  95. n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1);
  96. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  97. n += 2 } else { c2 = e.charCodeAt(n + 1);
  98. c3 = e.charCodeAt(n + 2);
  99. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  100. n += 3 } } return t } }
  101. // Define the string
  102. var string = document.getElementById('input4').value;
  103. // Encode the String
  104. var decodedString = Base64.decode(string);
  105. document.getElementById('output4').value = decodedString;
  106. }
  107. function tobase64() {
  108. var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0;
  109. e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++);
  110. r = e.charCodeAt(f++);
  111. i = e.charCodeAt(f++);
  112. s = n >> 2;
  113. o = (n & 3) << 4 | r >> 4;
  114. u = (r & 15) << 2 | i >> 6;
  115. a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function(e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0;
  116. e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++));
  117. o = this._keyStr.indexOf(e.charAt(f++));
  118. u = this._keyStr.indexOf(e.charAt(f++));
  119. a = this._keyStr.indexOf(e.charAt(f++));
  120. n = s << 2 | o >> 4;
  121. r = (o & 15) << 4 | u >> 2;
  122. i = (u & 3) << 6 | a;
  123. t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function(e) { e = e.replace(/\r\n/g, "\n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192);
  124. t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224);
  125. t += String.fromCharCode(r >> 6 & 63 | 128);
  126. t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function(e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r);
  127. n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1);
  128. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  129. n += 2 } else { c2 = e.charCodeAt(n + 1);
  130. c3 = e.charCodeAt(n + 2);
  131. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  132. n += 3 } } return t } }
  133. // Define the string
  134. var string = document.getElementById('input2').value;
  135. // Encode the String
  136. var encodedString = Base64.encode(string);
  137. document.getElementById('output2').value = encodedString;
  138. }
  139. </script>
  140. <script>
  141. function handleFileSelect(evt) {
  142. document.getElementById("loading").style.display = "";
  143. var files = evt.target.files; // FileList object
  144. // Loop through the FileList and render image files as thumbnails.
  145. for (var i = 0, f; f = files[i]; i++) {
  146. // Only process image files.
  147. if (!f.type.match('application/pdf')) {
  148. continue;
  149. }
  150. var reader = new FileReader();
  151. // Closure to capture the file information.
  152. reader.onload = (function(theFile) {
  153. return function(e) {
  154. // Render thumbnail.
  155. document.getElementById('pdfb64').data = e.target.result;
  156. document.getElementById("output2").value = e.target.result;
  157. document.getElementById("output1").value = e.target.result.split(',')[1];
  158. imageB64 = e.target.result;
  159. document.getElementById("loading").style.display = "none";
  160. };
  161. })(f);
  162. // Read in the image file as a data URL.
  163. reader.readAsDataURL(f);
  164. }
  165. }
  166. document.getElementById('files').addEventListener('change', handleFileSelect, false);
  167. </script>
  168. {% endblock %}