helloworld64.html 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>'Hello, world!' base64 example</title>
  6. </head>
  7. <body>
  8. <h1>'Hello, world!' example</h1>
  9. <canvas id="the-canvas" style="border: 1px solid black; direction: ltr;"></canvas>
  10. <script src="../../node_modules/pdfjs-dist/build/pdf.js"></script>
  11. <script id="script">
  12. // atob() is used to convert base64 encoded PDF to binary-like data.
  13. // (See also https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/
  14. // Base64_encoding_and_decoding.)
  15. var pdfData = atob(
  16. 'JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwog' +
  17. 'IC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAv' +
  18. 'TWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0K' +
  19. 'Pj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAg' +
  20. 'L1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+' +
  21. 'PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9u' +
  22. 'dAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2Jq' +
  23. 'Cgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJU' +
  24. 'CjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVu' +
  25. 'ZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4g' +
  26. 'CjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAw' +
  27. 'MDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9v' +
  28. 'dCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G');
  29. //
  30. // The workerSrc property shall be specified.
  31. //
  32. pdfjsLib.GlobalWorkerOptions.workerSrc =
  33. '../../node_modules/pdfjs-dist/build/pdf.worker.js';
  34. // Opening PDF by passing its binary data as a string. It is still preferable
  35. // to use Uint8Array, but string or array-like structure will work too.
  36. var loadingTask = pdfjsLib.getDocument({ data: pdfData, });
  37. (async function() {
  38. var pdf = await loadingTask.promise;
  39. // Fetch the first page.
  40. var page = await pdf.getPage(1);
  41. var scale = 1.5;
  42. var viewport = page.getViewport({ scale: scale, });
  43. // Support HiDPI-screens.
  44. var outputScale = window.devicePixelRatio || 1;
  45. // Prepare canvas using PDF page dimensions.
  46. var canvas = document.getElementById('the-canvas');
  47. var context = canvas.getContext('2d');
  48. canvas.width = Math.floor(viewport.width * outputScale);
  49. canvas.height = Math.floor(viewport.height * outputScale);
  50. canvas.style.width = Math.floor(viewport.width) + "px";
  51. canvas.style.height = Math.floor(viewport.height) + "px";
  52. var transform = outputScale !== 1
  53. ? [outputScale, 0, 0, outputScale, 0, 0]
  54. : null;
  55. // Render PDF page into canvas context.
  56. var renderContext = {
  57. canvasContext: context,
  58. transform,
  59. viewport,
  60. };
  61. page.render(renderContext);
  62. })();
  63. </script>
  64. <hr>
  65. <h2>JavaScript code:</h2>
  66. <pre id="code"></pre>
  67. <script>
  68. document.getElementById('code').textContent =
  69. document.getElementById('script').text;
  70. </script>
  71. </body>
  72. </html>