Font lost when rendered in an iframe
See original GitHub issueI’m trying to use an iframe to print a PDF easily, everything is ok but the fonts.
Configuration:
- Web browser and its version: chrome 57
- Operating system and its version: MacOS Sierra 10.12.7
- PDF.js version: pdfjs-dist 1.8.177
Steps to reproduce the problem:
- Dynamically create an iframe with a canvas, render the pdf in the canvas
Here is my print function which takes the pdfDoc as parameter (from PDFJS.getDocument(url)
)
function print(pdfDoc) {
if (!pdfDoc || !pdfDoc.numPages) {
return ;
}
var scale = 5;
var iframe = window.document.createElement('iframe');
var doc = iframe.contentDocument || iframe.contentWindow.document;
window.document.body.append(iframe);
doc.open();
doc.write(
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<style>' +
'* {margin: 0 !important; padding: 0 !important}' +
'div{width:612pt; height:792pt;}' +
'canvas{width:100%; height:100%;}' +
'</style>' +
'</head>' +
'<body></body>' +
'</html>'
);
doc.close();
function renderPage(num) {
console.log('Rendering page %d of %d', num, pdfDoc.numPages);
return pdfDoc
.getPage(num)
.then(function (page) {
var div = doc.createElement('div');
var canvas = doc.createElement('canvas');
div.appendChild(canvas);
doc.body.appendChild(div);
var viewport = page.getViewport(scale);
var ctx = canvas.getContext('2d');
canvas.width = viewport.width;
canvas.height = viewport.height;
return page.render({canvasContext: ctx, viewport: viewport}).promise;
})
.then(function () {
if (num < pdfDoc.numPages) {
return renderPage(num + 1);
}
});
}
renderPage(1)
.then(function () {
iframe.contentWindow.print();
iframe.remove();
});
}
On the left, the main page canvas, on the right, the iframe canvas.
The rendering in the iframe seems to not embed the fonts.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Chrome text rendering different when same page is inside an ...
I'm asking because some fonts appear to be slightly blurry in an iframe. There's no transforms applied on the iframe whatsoever. css ·...
Read more >Solved: Re: Consistent style for pages with embedded text
When we use the Canvas editor to write text-based content into a page, the style of the text displayed when the page is...
Read more >325697 - Font size changing on iframe reload - chromium
Hitting "run" within jsfiddle sometimes causes the "result" iframe to occasionally use an incorrect font size (compare good.png and bad.png)
Read more >Using inline frames (iframe elements) to embed documents ...
A detailed description of inline frames ("floating frames", iframe elements), as by the HTML specifications and the IE implementation.
Read more >How to put an iFrame into Confluence
Purpose. An IFrame (Inline Frame) is an HTML document embedded inside another HTML document on a website. The IFrame HTML element is often ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, if somebody visits this issue again, here is a workarround. I followed the steps @Rob--W provided above and can confirm it works on all browsers (Safari, Chrome, Firefox and Edge).
Here is the code snippet (Renders all pages at once):
can I work on this bug ?