xfa_layer_builder.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright 2021 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */
  16. // eslint-disable-next-line max-len
  17. /** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */
  18. /** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
  19. import { XfaLayer } from "pdfjs-lib";
  20. /**
  21. * @typedef {Object} XfaLayerBuilderOptions
  22. * @property {HTMLDivElement} pageDiv
  23. * @property {PDFPageProxy} pdfPage
  24. * @property {AnnotationStorage} [annotationStorage]
  25. * @property {IPDFLinkService} linkService
  26. * @property {Object} [xfaHtml]
  27. */
  28. class XfaLayerBuilder {
  29. /**
  30. * @param {XfaLayerBuilderOptions} options
  31. */
  32. constructor({
  33. pageDiv,
  34. pdfPage,
  35. annotationStorage = null,
  36. linkService,
  37. xfaHtml = null,
  38. }) {
  39. this.pageDiv = pageDiv;
  40. this.pdfPage = pdfPage;
  41. this.annotationStorage = annotationStorage;
  42. this.linkService = linkService;
  43. this.xfaHtml = xfaHtml;
  44. this.div = null;
  45. this._cancelled = false;
  46. }
  47. /**
  48. * @param {PageViewport} viewport
  49. * @param {string} intent (default value is 'display')
  50. * @returns {Promise<Object | void>} A promise that is resolved when rendering
  51. * of the XFA layer is complete. The first rendering will return an object
  52. * with a `textDivs` property that can be used with the TextHighlighter.
  53. */
  54. async render(viewport, intent = "display") {
  55. if (intent === "print") {
  56. const parameters = {
  57. viewport: viewport.clone({ dontFlip: true }),
  58. div: this.div,
  59. xfaHtml: this.xfaHtml,
  60. annotationStorage: this.annotationStorage,
  61. linkService: this.linkService,
  62. intent,
  63. };
  64. // Create an xfa layer div and render the form
  65. const div = document.createElement("div");
  66. this.pageDiv.append(div);
  67. parameters.div = div;
  68. return XfaLayer.render(parameters);
  69. }
  70. // intent === "display"
  71. const xfaHtml = await this.pdfPage.getXfa();
  72. if (this._cancelled || !xfaHtml) {
  73. return { textDivs: [] };
  74. }
  75. const parameters = {
  76. viewport: viewport.clone({ dontFlip: true }),
  77. div: this.div,
  78. xfaHtml,
  79. annotationStorage: this.annotationStorage,
  80. linkService: this.linkService,
  81. intent,
  82. };
  83. if (this.div) {
  84. return XfaLayer.update(parameters);
  85. }
  86. // Create an xfa layer div and render the form
  87. this.div = document.createElement("div");
  88. this.pageDiv.append(this.div);
  89. parameters.div = this.div;
  90. return XfaLayer.render(parameters);
  91. }
  92. cancel() {
  93. this._cancelled = true;
  94. }
  95. hide() {
  96. if (!this.div) {
  97. return;
  98. }
  99. this.div.hidden = true;
  100. }
  101. }
  102. export { XfaLayerBuilder };