Skip to content

Commit aee0766

Browse files
committed
XFA - Add support to print XFA forms
1 parent 0b156ca commit aee0766

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

web/firefox_print_service.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function composePage(
3333
canvas.height = Math.floor(size.height * PRINT_UNITS);
3434

3535
const canvasWrapper = document.createElement("div");
36+
canvasWrapper.setAttribute("class", "printedPage");
3637
canvasWrapper.appendChild(canvas);
3738
printContainer.appendChild(canvasWrapper);
3839

web/pdf_print_service.js

+36-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515

1616
import { PDFPrintServiceFactory, PDFViewerApplication } from "./app.js";
17+
import { CSS_UNITS } from "./ui_utils.js";
18+
import { DefaultXfaLayerFactory } from "./xfa_layer_builder.js";
1719
import { viewerCompatibilityParams } from "./viewer_compatibility.js";
1820

1921
let activeService = null;
@@ -27,8 +29,28 @@ function renderPage(
2729
pageNumber,
2830
size,
2931
printResolution,
30-
optionalContentConfigPromise
32+
optionalContentConfigPromise,
33+
printContainer
3134
) {
35+
if (pdfDocument.isPureXfa) {
36+
const page = document.createElement("div");
37+
page.setAttribute("class", "xfaPrintedPage");
38+
printContainer.appendChild(page);
39+
40+
return pdfDocument.getPage(pageNumber).then(pdfPage => {
41+
const builder = new DefaultXfaLayerFactory().createXfaLayerBuilder(
42+
page,
43+
pdfPage,
44+
pdfDocument.annotationStorage
45+
);
46+
const viewport = pdfPage.getViewport({
47+
scale: CSS_UNITS,
48+
rotation: size.rotation,
49+
});
50+
return builder.render(viewport, "print").promise;
51+
});
52+
}
53+
3254
const scratchCanvas = activeService.scratchCanvas;
3355

3456
// The size of the canvas in pixels for printing.
@@ -140,6 +162,10 @@ PDFPrintService.prototype = {
140162

141163
renderPages() {
142164
const pageCount = this.pagesOverview.length;
165+
if (pageCount === 0) {
166+
return Promise.resolve();
167+
}
168+
143169
const renderNextPage = (resolve, reject) => {
144170
this.throwIfInactive();
145171
if (++this.currentPage >= pageCount) {
@@ -155,10 +181,15 @@ PDFPrintService.prototype = {
155181
/* pageNumber = */ index + 1,
156182
this.pagesOverview[index],
157183
this._printResolution,
158-
this._optionalContentConfigPromise
184+
this._optionalContentConfigPromise,
185+
this.printContainer
159186
)
160-
.then(this.useRenderedPage.bind(this))
161-
.then(function () {
187+
.then(() => {
188+
if (!this.pdfDocument.isPureXfa) {
189+
this.useRenderedPage.bind(this);
190+
}
191+
})
192+
.then(() => {
162193
renderNextPage(resolve, reject);
163194
}, reject);
164195
};
@@ -181,6 +212,7 @@ PDFPrintService.prototype = {
181212
}
182213

183214
const wrapper = document.createElement("div");
215+
wrapper.setAttribute("class", "printedPage");
184216
wrapper.appendChild(img);
185217
this.printContainer.appendChild(wrapper);
186218

web/viewer.css

+14-4
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,8 @@ html[dir="rtl"] #documentPropertiesOverlay .row > * {
17721772
.toolbar,
17731773
#loadingBox,
17741774
#errorWrapper,
1775-
.textLayer {
1775+
.textLayer,
1776+
.canvasWrapper {
17761777
display: none;
17771778
}
17781779
#viewerContainer {
@@ -1816,7 +1817,7 @@ html[dir="rtl"] #documentPropertiesOverlay .row > * {
18161817
height: 100%;
18171818
}
18181819
/* wrapper around (scaled) print canvas elements */
1819-
#printContainer > div {
1820+
#printContainer > .printedPage {
18201821
page-break-after: always;
18211822
page-break-inside: avoid;
18221823

@@ -1829,8 +1830,17 @@ html[dir="rtl"] #documentPropertiesOverlay .row > * {
18291830
justify-content: center;
18301831
align-items: center;
18311832
}
1832-
#printContainer canvas,
1833-
#printContainer img {
1833+
1834+
#printContainer > .xfaPrintedPage {
1835+
page-break-after: always;
1836+
page-break-inside: avoid;
1837+
width: 100%;
1838+
height: 100%;
1839+
position: relative;
1840+
}
1841+
1842+
.printedPage canvas,
1843+
.printedPage img {
18341844
/* The intrinsic canvas / image size will make sure that we fit the page. */
18351845
max-width: 100%;
18361846
max-height: 100%;

web/xfa_layer_builder.css

+7
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,10 @@
262262
.xfaTable .xfaRlRow > div {
263263
flex: 1;
264264
}
265+
266+
@media print {
267+
.xfaTextfield,
268+
.xfaSelect {
269+
background-color: transparent;
270+
}
271+
}

0 commit comments

Comments
 (0)