Merge pull request #13613 from Snuffleupagus/xfa-printing-tweaks
[api-minor] Slightly tweak/improve various code related to XFA-printing
This commit is contained in:
commit
f74562b19c
@ -743,8 +743,10 @@ class PDFDocumentProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* NOTE: This is (mostly) intended to support printing of XFA forms.
|
||||||
|
*
|
||||||
* @type {Object | null} An object representing a HTML tree structure
|
* @type {Object | null} An object representing a HTML tree structure
|
||||||
* to render the XFA, or `null` when no XFA form exists.
|
* to render the XFA, or `null` when no XFA form exists.
|
||||||
*/
|
*/
|
||||||
get allXfaHtml() {
|
get allXfaHtml() {
|
||||||
return this._transport._htmlForXfa;
|
return this._transport._htmlForXfa;
|
||||||
|
@ -606,6 +606,20 @@ class PDFDateString {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NOTE: This is (mostly) intended to support printing of XFA forms.
|
||||||
|
*/
|
||||||
|
function getXfaPageViewport(xfaPage, { scale = 1, rotation = 0 }) {
|
||||||
|
const { width, height } = xfaPage.attributes.style;
|
||||||
|
const viewBox = [0, 0, parseInt(width), parseInt(height)];
|
||||||
|
|
||||||
|
return new PageViewport({
|
||||||
|
viewBox,
|
||||||
|
scale,
|
||||||
|
rotation,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
addLinkAttributes,
|
addLinkAttributes,
|
||||||
DEFAULT_LINK_REL,
|
DEFAULT_LINK_REL,
|
||||||
@ -616,6 +630,7 @@ export {
|
|||||||
DOMSVGFactory,
|
DOMSVGFactory,
|
||||||
getFilenameFromUrl,
|
getFilenameFromUrl,
|
||||||
getPdfFilenameFromUrl,
|
getPdfFilenameFromUrl,
|
||||||
|
getXfaPageViewport,
|
||||||
isDataScheme,
|
isDataScheme,
|
||||||
isPdfFile,
|
isPdfFile,
|
||||||
isValidFetchUrl,
|
isValidFetchUrl,
|
||||||
|
@ -13,8 +13,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { PageViewport } from "./display_utils.js";
|
|
||||||
|
|
||||||
class XfaLayer {
|
class XfaLayer {
|
||||||
static setupStorage(html, fieldId, element, storage, intent) {
|
static setupStorage(html, fieldId, element, storage, intent) {
|
||||||
const storedData = storage.getValue(fieldId, { value: null });
|
const storedData = storage.getValue(fieldId, { value: null });
|
||||||
@ -134,13 +132,8 @@ class XfaLayer {
|
|||||||
|
|
||||||
const rootDiv = parameters.div;
|
const rootDiv = parameters.div;
|
||||||
rootDiv.appendChild(rootHtml);
|
rootDiv.appendChild(rootHtml);
|
||||||
|
const transform = `matrix(${parameters.viewport.transform.join(",")})`;
|
||||||
let { viewport } = parameters;
|
rootDiv.style.transform = transform;
|
||||||
if (!(viewport instanceof PageViewport)) {
|
|
||||||
viewport = new PageViewport(viewport);
|
|
||||||
}
|
|
||||||
const coeffs = viewport.transform.join(",");
|
|
||||||
rootDiv.style.transform = `matrix(${coeffs})`;
|
|
||||||
|
|
||||||
// Set defaults.
|
// Set defaults.
|
||||||
rootDiv.setAttribute("class", "xfaLayer xfaFont");
|
rootDiv.setAttribute("class", "xfaLayer xfaFont");
|
||||||
|
@ -18,6 +18,7 @@ import {
|
|||||||
addLinkAttributes,
|
addLinkAttributes,
|
||||||
getFilenameFromUrl,
|
getFilenameFromUrl,
|
||||||
getPdfFilenameFromUrl,
|
getPdfFilenameFromUrl,
|
||||||
|
getXfaPageViewport,
|
||||||
isPdfFile,
|
isPdfFile,
|
||||||
isValidFetchUrl,
|
isValidFetchUrl,
|
||||||
LinkTarget,
|
LinkTarget,
|
||||||
@ -108,6 +109,7 @@ export {
|
|||||||
loadScript,
|
loadScript,
|
||||||
PDFDateString,
|
PDFDateString,
|
||||||
RenderingCancelledException,
|
RenderingCancelledException,
|
||||||
|
getXfaPageViewport,
|
||||||
// From "./display/api.js":
|
// From "./display/api.js":
|
||||||
build,
|
build,
|
||||||
getDocument,
|
getDocument,
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { RenderingCancelledException, shadow } from "pdfjs-lib";
|
import { RenderingCancelledException, shadow } from "pdfjs-lib";
|
||||||
import { getXfaHtmlForPrinting } from "./ui_utils.js";
|
import { getXfaHtmlForPrinting } from "./print_utils.js";
|
||||||
import { PDFPrintServiceFactory } from "./app.js";
|
import { PDFPrintServiceFactory } from "./app.js";
|
||||||
|
|
||||||
// Creates a placeholder with div and canvas with right size for the page.
|
// Creates a placeholder with div and canvas with right size for the page.
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { PDFPrintServiceFactory, PDFViewerApplication } from "./app.js";
|
import { PDFPrintServiceFactory, PDFViewerApplication } from "./app.js";
|
||||||
import { getXfaHtmlForPrinting } from "./ui_utils.js";
|
import { getXfaHtmlForPrinting } from "./print_utils.js";
|
||||||
import { viewerCompatibilityParams } from "./viewer_compatibility.js";
|
import { viewerCompatibilityParams } from "./viewer_compatibility.js";
|
||||||
|
|
||||||
let activeService = null;
|
let activeService = null;
|
||||||
|
42
web/print_utils.js
Normal file
42
web/print_utils.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/* Copyright 2021 Mozilla Foundation
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { CSS_UNITS } from "./ui_utils.js";
|
||||||
|
import { DefaultXfaLayerFactory } from "./xfa_layer_builder.js";
|
||||||
|
import { getXfaPageViewport } from "pdfjs-lib";
|
||||||
|
|
||||||
|
function getXfaHtmlForPrinting(printContainer, pdfDocument) {
|
||||||
|
const xfaHtml = pdfDocument.allXfaHtml;
|
||||||
|
const factory = new DefaultXfaLayerFactory();
|
||||||
|
const scale = Math.round(CSS_UNITS * 100) / 100;
|
||||||
|
|
||||||
|
for (const xfaPage of xfaHtml.children) {
|
||||||
|
const page = document.createElement("div");
|
||||||
|
page.className = "xfaPrintedPage";
|
||||||
|
printContainer.appendChild(page);
|
||||||
|
|
||||||
|
const builder = factory.createXfaLayerBuilder(
|
||||||
|
/* pageDiv = */ page,
|
||||||
|
/* pdfPage = */ null,
|
||||||
|
/* annotationStorage = */ pdfDocument.annotationStorage,
|
||||||
|
/* xfaHtml = */ xfaPage
|
||||||
|
);
|
||||||
|
const viewport = getXfaPageViewport(xfaPage, { scale });
|
||||||
|
|
||||||
|
builder.render(viewport, "print");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { getXfaHtmlForPrinting };
|
@ -13,8 +13,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { DefaultXfaLayerFactory } from "./xfa_layer_builder.js";
|
|
||||||
|
|
||||||
const CSS_UNITS = 96.0 / 72.0;
|
const CSS_UNITS = 96.0 / 72.0;
|
||||||
const DEFAULT_SCALE_VALUE = "auto";
|
const DEFAULT_SCALE_VALUE = "auto";
|
||||||
const DEFAULT_SCALE = 1.0;
|
const DEFAULT_SCALE = 1.0;
|
||||||
@ -995,29 +993,6 @@ function apiPageModeToSidebarView(mode) {
|
|||||||
return SidebarView.NONE; // Default value.
|
return SidebarView.NONE; // Default value.
|
||||||
}
|
}
|
||||||
|
|
||||||
function getXfaHtmlForPrinting(printContainer, pdfDocument) {
|
|
||||||
const xfaHtml = pdfDocument.allXfaHtml;
|
|
||||||
const factory = new DefaultXfaLayerFactory();
|
|
||||||
const scale = Math.round(CSS_UNITS * 100) / 100;
|
|
||||||
for (const xfaPage of xfaHtml.children) {
|
|
||||||
const page = document.createElement("div");
|
|
||||||
page.className = "xfaPrintedPage";
|
|
||||||
printContainer.appendChild(page);
|
|
||||||
|
|
||||||
const { width, height } = xfaPage.attributes.style;
|
|
||||||
const viewBox = [0, 0, parseInt(width), parseInt(height)];
|
|
||||||
const viewport = { viewBox, scale, rotation: 0 };
|
|
||||||
|
|
||||||
const builder = factory.createXfaLayerBuilder(
|
|
||||||
page,
|
|
||||||
null,
|
|
||||||
pdfDocument.annotationStorage,
|
|
||||||
xfaPage
|
|
||||||
);
|
|
||||||
builder.render(viewport, "print");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export {
|
export {
|
||||||
animationStarted,
|
animationStarted,
|
||||||
apiPageLayoutToSpreadMode,
|
apiPageLayoutToSpreadMode,
|
||||||
@ -1034,7 +1009,6 @@ export {
|
|||||||
getOutputScale,
|
getOutputScale,
|
||||||
getPageSizeInches,
|
getPageSizeInches,
|
||||||
getVisibleElements,
|
getVisibleElements,
|
||||||
getXfaHtmlForPrinting,
|
|
||||||
isPortraitOrientation,
|
isPortraitOrientation,
|
||||||
isValidRotation,
|
isValidRotation,
|
||||||
isValidScrollMode,
|
isValidScrollMode,
|
||||||
|
@ -43,56 +43,55 @@ class XfaLayerBuilder {
|
|||||||
* annotations is complete.
|
* annotations is complete.
|
||||||
*/
|
*/
|
||||||
render(viewport, intent = "display") {
|
render(viewport, intent = "display") {
|
||||||
if (intent === "display") {
|
if (intent === "print") {
|
||||||
return this.pdfPage
|
const parameters = {
|
||||||
.getXfa()
|
viewport: viewport.clone({ dontFlip: true }),
|
||||||
.then(xfa => {
|
div: this.div,
|
||||||
if (this._cancelled) {
|
xfa: this.xfaHtml,
|
||||||
return;
|
page: null,
|
||||||
}
|
annotationStorage: this.annotationStorage,
|
||||||
const parameters = {
|
intent,
|
||||||
viewport: viewport.clone({ dontFlip: true }),
|
};
|
||||||
div: this.div,
|
|
||||||
xfa,
|
|
||||||
page: this.pdfPage,
|
|
||||||
annotationStorage: this.annotationStorage,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (this.div) {
|
// Create an xfa layer div and render the form
|
||||||
XfaLayer.update(parameters);
|
const div = document.createElement("div");
|
||||||
} else {
|
this.pageDiv.appendChild(div);
|
||||||
// Create an xfa layer div and render the form
|
parameters.div = div;
|
||||||
this.div = document.createElement("div");
|
|
||||||
this.pageDiv.appendChild(this.div);
|
|
||||||
parameters.div = this.div;
|
|
||||||
|
|
||||||
XfaLayer.render(parameters);
|
XfaLayer.render(parameters);
|
||||||
}
|
return Promise.resolve();
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// intent === "print".
|
// intent === "display"
|
||||||
viewport.dontFlip = true;
|
return this.pdfPage
|
||||||
const parameters = {
|
.getXfa()
|
||||||
viewport,
|
.then(xfa => {
|
||||||
div: this.div,
|
if (this._cancelled) {
|
||||||
xfa: this.xfaHtml,
|
return;
|
||||||
page: null,
|
}
|
||||||
annotationStorage: this.annotationStorage,
|
const parameters = {
|
||||||
intent,
|
viewport: viewport.clone({ dontFlip: true }),
|
||||||
};
|
div: this.div,
|
||||||
|
xfa,
|
||||||
|
page: this.pdfPage,
|
||||||
|
annotationStorage: this.annotationStorage,
|
||||||
|
intent,
|
||||||
|
};
|
||||||
|
|
||||||
// Create an xfa layer div and render the form
|
if (this.div) {
|
||||||
const div = document.createElement("div");
|
XfaLayer.update(parameters);
|
||||||
this.pageDiv.appendChild(div);
|
} else {
|
||||||
parameters.div = div;
|
// 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);
|
||||||
|
}
|
||||||
return null;
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error(error);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel() {
|
cancel() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user