[api-minor] Add a new getXfaPageViewport helper function to support printing

This patch provides an overall simpler *and* more consistent way of handling the `viewport` parameter during printing of XFA forms, since it's now again guaranteed to always be an instance of `PageViewport`.
Furthermore, for anyone attempting to e.g. implement custom printing of XFA forms this probably cannot hurt either.
This commit is contained in:
Jonas Jenwald 2021-06-18 12:31:58 +02:00
parent 45c1390c42
commit 87be43c193
6 changed files with 28 additions and 16 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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");

View File

@ -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,

View File

@ -15,26 +15,26 @@
import { CSS_UNITS } from "./ui_utils.js"; import { CSS_UNITS } from "./ui_utils.js";
import { DefaultXfaLayerFactory } from "./xfa_layer_builder.js"; import { DefaultXfaLayerFactory } from "./xfa_layer_builder.js";
import { getXfaPageViewport } from "pdfjs-lib";
function getXfaHtmlForPrinting(printContainer, pdfDocument) { function getXfaHtmlForPrinting(printContainer, pdfDocument) {
const xfaHtml = pdfDocument.allXfaHtml; const xfaHtml = pdfDocument.allXfaHtml;
const factory = new DefaultXfaLayerFactory(); const factory = new DefaultXfaLayerFactory();
const scale = Math.round(CSS_UNITS * 100) / 100; const scale = Math.round(CSS_UNITS * 100) / 100;
for (const xfaPage of xfaHtml.children) { for (const xfaPage of xfaHtml.children) {
const page = document.createElement("div"); const page = document.createElement("div");
page.className = "xfaPrintedPage"; page.className = "xfaPrintedPage";
printContainer.appendChild(page); 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( const builder = factory.createXfaLayerBuilder(
/* pageDiv = */ page, /* pageDiv = */ page,
/* pdfPage = */ null, /* pdfPage = */ null,
/* annotationStorage = */ pdfDocument.annotationStorage, /* annotationStorage = */ pdfDocument.annotationStorage,
/* xfaHtml = */ xfaPage /* xfaHtml = */ xfaPage
); );
const viewport = getXfaPageViewport(xfaPage, { scale });
builder.render(viewport, "print"); builder.render(viewport, "print");
} }
} }

View File

@ -44,9 +44,8 @@ class XfaLayerBuilder {
*/ */
render(viewport, intent = "display") { render(viewport, intent = "display") {
if (intent === "print") { if (intent === "print") {
viewport.dontFlip = true;
const parameters = { const parameters = {
viewport, viewport: viewport.clone({ dontFlip: true }),
div: this.div, div: this.div,
xfa: this.xfaHtml, xfa: this.xfaHtml,
page: null, page: null,
@ -76,6 +75,7 @@ class XfaLayerBuilder {
xfa, xfa,
page: this.pdfPage, page: this.pdfPage,
annotationStorage: this.annotationStorage, annotationStorage: this.annotationStorage,
intent,
}; };
if (this.div) { if (this.div) {