pdf_textlayer.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@2.2.228/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. // Returns a promise, on resolving it will return text contents of the page
  56. return page.getTextContent();
  57. }).then(function(textContent) {
  58. // PDF canvas
  59. var pdf_canvas = $("#canvas");
  60. // Canvas offset
  61. var canvas_offset = pdf_canvas.offset();
  62. // Canvas height
  63. var canvas_height = canvas.height;
  64. // Canvas width
  65. var canvas_width = canvas.width;
  66. // Assign CSS to the text-layer element
  67. $("#text-layer").css({ left: canvas_offset.left + 'px', top: canvas_offset.top + 'px', height: canvas_height + 'px', width: canvas_width + 'px' });
  68. // Pass the data to the method for rendering of text over the pdf canvas.
  69. PDFJS.renderTextLayer({
  70. textContent: textContent,
  71. container: $("#text-layer").get(0),
  72. viewport: viewport,
  73. textDivs: []
  74. });
  75. });
  76. });
  77. }
  78. });
  79. </script>
  80. </body>
  81. </html>