1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

XFA - Add a storage to save fields values - this is required to be able to print (or save) a document. Some pages can be unloaded (because pdf.js is lazy) and this storage will help to save their data in order to resuse them when printing or just when displaying a page again.

This commit is contained in:
Calixte Denizet 2021-05-24 13:42:42 +02:00
parent 4d26623e59
commit 9478d2f064
5 changed files with 110 additions and 30 deletions

View file

@ -1317,12 +1317,16 @@ class BaseViewer {
/**
* @param {HTMLDivElement} pageDiv
* @param {PDFPage} pdfPage
* @param {AnnotationStorage} [annotationStorage] - Storage for annotation
* data in forms.
* @returns {XfaLayerBuilder}
*/
createXfaLayerBuilder(pageDiv, pdfPage) {
createXfaLayerBuilder(pageDiv, pdfPage, annotationStorage = null) {
return new XfaLayerBuilder({
pageDiv,
pdfPage,
annotationStorage:
annotationStorage || this.pdfDocument?.annotationStorage,
});
}

View file

@ -603,7 +603,8 @@ class PDFPageView {
if (!this.xfaLayer) {
this.xfaLayer = this.xfaLayerFactory.createXfaLayerBuilder(
div,
pdfPage
pdfPage,
/* annotationStorage = */ null
);
}
this._renderXfaLayer();

View file

@ -19,15 +19,17 @@ import { XfaLayer } from "pdfjs-lib";
* @typedef {Object} XfaLayerBuilderOptions
* @property {HTMLDivElement} pageDiv
* @property {PDFPage} pdfPage
* @property {AnnotationStorage} [annotationStorage]
*/
class XfaLayerBuilder {
/**
* @param {XfaLayerBuilderOptions} options
*/
constructor({ pageDiv, pdfPage }) {
constructor({ pageDiv, pdfPage, annotationStorage }) {
this.pageDiv = pageDiv;
this.pdfPage = pdfPage;
this.annotationStorage = annotationStorage;
this.div = null;
this._cancelled = false;
@ -40,29 +42,34 @@ class XfaLayerBuilder {
* annotations is complete.
*/
render(viewport, intent = "display") {
return this.pdfPage.getXfa().then(xfa => {
if (this._cancelled) {
return;
}
return this.pdfPage
.getXfa()
.then(xfa => {
if (this._cancelled) {
return;
}
const parameters = {
viewport: viewport.clone({ dontFlip: true }),
div: this.div,
xfa,
page: this.pdfPage,
annotationStorage: this.annotationStorage,
};
const parameters = {
viewport: viewport.clone({ dontFlip: true }),
div: this.div,
xfa,
page: this.pdfPage,
};
if (this.div) {
XfaLayer.update(parameters);
} else {
// Create an xfa layer div and render the form
this.div = document.createElement("div");
this.pageDiv.appendChild(this.div);
parameters.div = this.div;
if (this.div) {
XfaLayer.update(parameters);
} else {
// Create an xfa layer div and render the form
this.div = document.createElement("div");
this.pageDiv.appendChild(this.div);
parameters.div = this.div;
XfaLayer.render(parameters);
}
});
XfaLayer.render(parameters);
}
})
.catch(error => {
console.error(error);
});
}
cancel() {
@ -84,11 +91,13 @@ class DefaultXfaLayerFactory {
/**
* @param {HTMLDivElement} pageDiv
* @param {PDFPage} pdfPage
* @param {AnnotationStorage} [annotationStorage]
*/
createXfaLayerBuilder(pageDiv, pdfPage) {
createXfaLayerBuilder(pageDiv, pdfPage, annotationStorage = null) {
return new XfaLayerBuilder({
pageDiv,
pdfPage,
annotationStorage,
});
}
}