question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Embedded fonts don't reflect in the generated png in node example

See original GitHub issue

Attach (recommended) or Link to PDF file here:

There is no link to the pdf as i’m running an example located in pdf-dist repo. Example link : https://github.com/mozilla/pdf.js/tree/master/examples/node/pdf2png

Configuration:

Steps to reproduce the problem:

  1. Open node example located in https://github.com/mozilla/pdf.js/tree/master/examples/node/pdf2png

  2. Install the dependencies and build the PDF.js library: npm install gulp dist-install

    Install the Node canvas library and run the example to convert the first page of a PDF file to a PNG image: npm install canvas

cd examples/node/pdf2png node pdf2png.js

What is the expected behavior? (add screenshot)

  • I was expecting the fonts to be applied to text in output.png but it is only being applied to the sample text i added to the canvas context.
  • For your reference the sample text is Everyone hates this font and it’s in orange color. I have titled the output image so we could see this text and since it has a big font size only the word every is visble

What went wrong? (add screenshot)

  1. I’m running the node example located in https://github.com/mozilla/pdf.js/tree/master/examples/node/pdf2png
  2. I added a sample text to the canvas like context.fillText('Everyone hates this font :(', 250, 10) . The text within the output that gets generated (ie output.png) remains unaffected where the text i added which is in orange colour in context appears in the newly registered font.
Screenshot 2020-12-06 at 10 22 17 AM

Here is the whole code :

Canvas.registerFont(path.resolve('assets', 'fonts', 'Anton-Regular.ttf'), { family: 'Anton' });

    var canvas = Canvas.createCanvas(width, height);
    var context = canvas.getContext("2d");

    context.font = '200px "Anton"'
    context.rotate(0.2);
    context.fillStyle="orange";
    context.fillText('Everyone hates this font :(', 250, 10)

Link to a viewer (if hosted on a site other than mozilla.github.io/pdf.js or as Firefox/Chrome extension):

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
timvandermeijcommented, Dec 6, 2020

Closing since this is in fact a duplicate of issue #4244 and maybe even issue #11637 which is tracking the standard fonts addition, so everything related to this issue is already tracked in there. To keep the discussions in one place, it’s common to close duplicates.

In general fonts need to be embedded in the PDF file to ensure a consistent display across viewers and devices. If you don’t do this you’re taking a risk, and there is no guarantee at all that viewers will be able to display the PDF file properly.

1reaction
Snuffleupaguscommented, Dec 6, 2020

All i’m asking you is add support for all the standard fonts in your pdf-dist js library […]

Again, that’s already being tracked elsewhere and simply repeating this (and opening duplicate issues) isn’t really helpful in general.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting OpenType / TrueType format to png images using ...
I need to generate images from a font file otf/ttf using the following code application: let font = loadFont("Verdana.ttf"); font.Generate(" ...
Read more >
Using Custom Fonts With SVG in an Image Tag | CSS-Tricks
When we produce a PNG image, we use an <img> tag or a CSS background, and that's about it. It is dead simple...
Read more >
node-html-to-image - npm
Description. This module exposes a function that generates images (png, jpeg) from HTML. It uses puppeteer in headless mode to achieve it.
Read more >
Puppeteer HTML to PDF Generation with Node.js
Learn to generate a Puppeteer PDF document from a heavily styled React ... with an embedded font!', 100, 100) doc.image('path/to/image.png', ...
Read more >
Render HTML file in Node.js and Express.js framework
In this short tutorial, I am going to explain to you how to render HTML files in a custom NodeJS and ExpressJS Server....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found