Skip to content

Commit e4d90f5

Browse files
committed
Fix fonts issue in printing
The PDFJS library adds the fonts in the main document.fonts variable. So we need to passed those fonts to the iframe.
1 parent dc5890b commit e4d90f5

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

src/pdfjsWrapper.js

+16-25
Original file line numberDiff line numberDiff line change
@@ -75,37 +75,30 @@ export default function(PDFJS) {
7575
var PRINT_UNITS = PRINT_RESOLUTION / 72.0;
7676
var CSS_UNITS = 96.0 / 72.0;
7777

78-
var iframeElt = document.createElement('iframe');
78+
var printContainerElement = document.createElement('div');
7979

80-
function removeIframe() {
81-
82-
iframeElt.parentNode.removeChild(iframeElt);
80+
function removePrintContainer() {
81+
printContainerElement.parentNode.removeChild(printContainerElement);
8382
}
8483

8584
new Promise(function(resolve, reject) {
86-
87-
iframeElt.frameBorder = '0';
88-
iframeElt.scrolling = 'no';
89-
iframeElt.width = '0px;'
90-
iframeElt.height = '0px;'
91-
iframeElt.style.cssText = 'position: absolute; top: 0; left: 0';
92-
93-
iframeElt.onload = function() {
94-
95-
resolve(this.contentWindow);
96-
}
97-
98-
window.document.body.appendChild(iframeElt);
85+
printContainerElement.frameBorder = '0';
86+
printContainerElement.scrolling = 'no';
87+
printContainerElement.width = '0px;'
88+
printContainerElement.height = '0px;'
89+
printContainerElement.style.cssText = 'position: absolute; top: 0; left: 0';
90+
91+
window.document.body.appendChild(printContainerElement);
92+
resolve(window)
9993
})
10094
.then(function(win) {
10195

10296
win.document.title = '';
10397

10498
return pdfDoc.getPage(1)
10599
.then(function(page) {
106-
107100
var viewport = page.getViewport(1);
108-
win.document.head.appendChild(win.document.createElement('style')).textContent =
101+
printContainerElement.appendChild(win.document.createElement('style')).textContent =
109102
'@supports ((size:A4) and (size:1pt 1pt)) {' +
110103
'@page { margin: 1pt; size: ' + ((viewport.width * PRINT_UNITS) / CSS_UNITS) + 'pt ' + ((viewport.height * PRINT_UNITS) / CSS_UNITS) + 'pt; }' +
111104
'}' +
@@ -157,18 +150,16 @@ export default function(PDFJS) {
157150

158151
Promise.all(allPages)
159152
.then(function() {
160-
161153
win.focus(); // Required for IE
162154
if (win.document.queryCommandSupported('print')) {
163155
win.document.execCommand('print', false, null);
164-
} else {
156+
} else {
165157
win.print();
166-
}
167-
removeIframe();
158+
}
159+
removePrintContainer();
168160
})
169161
.catch(function(err) {
170-
171-
removeIframe();
162+
removePrintContainer();
172163
emitEvent('error', err);
173164
})
174165
})

0 commit comments

Comments
 (0)