show_pdf.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html><meta charset="utf-8"/>
  2. <!--<script type="javascript" src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.3.200/pdf.js"></script> das funktioniert!!!!!
  3. <script type="javascript" src="https://unpkg.com/pdfjs-dist@latest/build/pdf.js" ></script>
  4. <script type="javascript" src="{{ url_for('static', filename='js/pdf.js') }}"></script>
  5. <script src="//mozilla.github.io/pdf.js/build/pdf.js"></script>
  6. <script src="https://mozilla.github.io/pdf.js/build/pdf.js"></script>
  7. -->
  8. <script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@1.9/build/pdf.min.js"></script>
  9. <script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@1.9/web/pdf_viewer.js"></script>
  10. <script src="https://cdn.jsdelivr.net/npm/pdfjs-dist@1.9/build/pdf.js"></script>
  11. <script type="javascript" src="{{ url_for('static', filename='js/text_layer_builder.js') }}"></script>
  12. <script type="javascript" src="{{ url_for('static', filename='js/ui_utils.js') }}"></script>
  13. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  14. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pdfjs-dist@1.9/web/pdf_viewer.css">
  15. <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"/>
  16. <link rel="stylesheet" href="{{ url_for('static', filename='css/text_layer_builder.css') }}" />
  17. <body>
  18. <div id="container" > {{og_filename}}</div>
  19. <div id="text-layer"></div>
  20. <script>
  21. var og_filename = "{{og_filename}}";
  22. var url = "{{ url_for('send_file', filename=og_filename) }}";
  23. console.log(og_filename);
  24. var pdfjsLib = window['pdfjs-dist/build/pdf'];
  25. pdfjsLib.getDocument(url)
  26. .promise.then(function(pdf) {
  27. // Get div#container and cache it for later use
  28. var container = document.getElementById("container");
  29. // Loop from 1 to total_number_of_pages in PDF document
  30. for (var i = 1; i <= pdf.numPages; i++) {
  31. // Get desired page
  32. pdf.getPage(i).then(function(page) {
  33. var scale = 1.5;
  34. var viewport = page.getViewport({scale: scale});
  35. var div = document.createElement("div");
  36. // Set id attribute with page-#{pdf_page_number} format
  37. div.setAttribute("id", "page-" + (page.pageIndex + 1));
  38. // This will keep positions of child elements as per our needs
  39. div.setAttribute("style", "position: relative");
  40. // Append div within div#container
  41. container.appendChild(div);
  42. // Create a new Canvas element
  43. var canvas = document.createElement("canvas");
  44. // Append Canvas within div#page-#{pdf_page_number}
  45. div.appendChild(canvas);
  46. var context = canvas.getContext('2d');
  47. canvas.height = viewport.height;
  48. canvas.width = viewport.width;
  49. var renderContext = {
  50. canvasContext: context,
  51. viewport: viewport
  52. };
  53. // Render PDF page
  54. page.render(renderContext).promise.then(function() {
  55. // Get text-fragments
  56. return page.getTextContent();
  57. })
  58. .then(function(textContent) {
  59. // Create div which will hold text-fragments
  60. console.log(textContent);
  61. var textLayerDiv = document.createElement("div");
  62. // Set it's class to textLayer which have required CSS styles
  63. textLayerDiv.setAttribute("class", "textLayer");
  64. // Append newly created div in `div#page-#{pdf_page_number}`
  65. div.appendChild(textLayerDiv);
  66. // Create new instance of TextLayerBuilder class
  67. var textLayer = new TextLayerBuilder({
  68. textLayerDiv: textLayerDiv,
  69. pageIndex: page.pageIndex,
  70. viewport: viewport
  71. });
  72. // Set text-fragments
  73. textLayer.setTextContent(textContent);
  74. // Render text-fragments
  75. textLayer.render();
  76. });
  77. });
  78. }
  79. });
  80. </script>
  81. </body>
  82. </html>