Skip to content

Alternative fix fonts issue in printing #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/pdfjsWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ export default function(PDFJS) {
var CSS_UNITS = 96.0 / 72.0;

var iframeElt = document.createElement('iframe');
var tempElt = document.createElement('div');
tempElt.style.cssText = 'display: none';
window.document.body.appendChild(tempElt);

function removeIframe() {

iframeElt.parentNode.removeChild(iframeElt);
tempElt.parentNode.removeChild(tempElt);
}

new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -108,9 +112,8 @@ export default function(PDFJS) {
var viewport = page.getViewport(1);
win.document.head.appendChild(win.document.createElement('style')).textContent =
'@supports ((size:A4) and (size:1pt 1pt)) {' +
'@page { margin: 1pt; size: ' + ((viewport.width * PRINT_UNITS) / CSS_UNITS) + 'pt ' + ((viewport.height * PRINT_UNITS) / CSS_UNITS) + 'pt; }' +
'}' +

'@page { size: ' + ((viewport.width * PRINT_UNITS) / CSS_UNITS) + 'pt ' + ((viewport.height * PRINT_UNITS) / CSS_UNITS) + 'pt; }' +
'}'+
'@media print {' +
'body { margin: 0 }' +
'canvas { page-break-before: avoid; page-break-after: always; page-break-inside: avoid }' +
Expand Down Expand Up @@ -139,7 +142,7 @@ export default function(PDFJS) {

var viewport = page.getViewport(1);

var printCanvasElt = win.document.body.appendChild(win.document.createElement('canvas'));
var printCanvasElt = tempElt.appendChild(document.createElement('canvas'));
printCanvasElt.width = (viewport.width * PRINT_UNITS);
printCanvasElt.height = (viewport.height * PRINT_UNITS);

Expand All @@ -158,6 +161,15 @@ export default function(PDFJS) {

Promise.all(allPages)
.then(function() {
for (var i = 0; i < tempElt.children.length; ++i) {
var child = tempElt.children[i];
var canvas = child.cloneNode(true);

var ctx = canvas.getContext('2d');
ctx.drawImage(child, 0, 0);

win.document.body.appendChild(canvas);
}

win.focus(); // Required for IE
if (win.document.queryCommandSupported('print')) {
Expand Down