html-to-image icon indicating copy to clipboard operation
html-to-image copied to clipboard

Fixes issue fetching font css when cssRules are inaccessilbe

Open dancrevier opened this issue 2 years ago • 19 comments

Fixes the handling of font css when cssRules are inaccessible.

Description

When cssRules are inaccessible in a style sheet, getCSSRules will throw an exception trying to read them, and then the catch handler will fetch the css. But, when it goes to insert the rule, it has a bug where it uses sheet.cssRules.length, which causes an exception again and the rules are lost.

This results in the error:

Error inlining remote css file SecurityError: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules

This is referenced in a few issues such as issue #49.

Motivation and Context

This fixes issues when fonts are referenced via external style sheets and the css cannot be read. The resulting image will be missing the fonts that failed. This error is mentioned in a few issues such as https://github.com/bubkoo/html-to-image/issues/49.

Types of changes

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Enhancement (changes that improvement of current feature or performance)
  • [ ] Refactoring (changes that neither fixes a bug nor adds a feature)
  • [ ] Test Case (changes that add missing tests or correct existing tests)
  • [ ] Code style optimization (changes that do not affect the meaning of the code)
  • [ ] Docs (changes that only update documentation)
  • [ ] Chore (changes that don't modify src or test files)

Self Check before Merge

  • [x] My code follows the code style of this project.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [x] I have read the CONTRIBUTING document.
  • [ ] I have added tests to cover my changes.
  • [x] All new and existing tests passed.

dancrevier avatar Mar 12 '23 23:03 dancrevier

👋 @dancrevier

💖 Thanks for opening this pull request! 💖

Please follow the contributing guidelines. And we use semantic commit messages to streamline the release process.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add graph.scale() method
  • docs: graph.getShortestPath is now available

Things that will help get your PR across the finish line:

  • Follow the TypeScript coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

biiibooo[bot] avatar Mar 13 '23 00:03 biiibooo[bot]

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 62.93%. Comparing base (b751cbf) to head (38abea8). Report is 13 commits behind head on master.

Files Patch % Lines
src/embed-webfonts.ts 0.00% 1 Missing :warning:
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #384   +/-   ##
=======================================
  Coverage   62.93%   62.93%           
=======================================
  Files          10       10           
  Lines         580      580           
  Branches      143      143           
=======================================
  Hits          365      365           
  Misses        151      151           
  Partials       64       64           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Mar 13 '23 00:03 codecov[bot]

I ran this branch locally, and it fixed my issue reported here for the most part. Google fonts are loading now.

The part that's not fixed is the icons, but that appears to be a separate issue.

petrgazarov avatar Mar 28 '23 01:03 petrgazarov

When can we get this PR reviewed and merged in? This bug is blocking us from adopting this package.

stumpykilo avatar Mar 29 '23 20:03 stumpykilo

+1

homanp avatar Apr 05 '23 08:04 homanp

+1

robertjmschmidt avatar Apr 06 '23 00:04 robertjmschmidt

+1

morjanmihail avatar Apr 25 '23 19:04 morjanmihail

+1! May we have this merged? Thank you so much!

sherryysj avatar May 10 '23 00:05 sherryysj

+1! May we have this merged? Thank you so much!

panditlakshya avatar May 16 '23 07:05 panditlakshya

pls merge

plarner30 avatar Aug 07 '23 17:08 plarner30

+1

RolandoAndrade avatar Oct 13 '23 12:10 RolandoAndrade

+1 needs this issue fixed

Kitenite avatar Oct 26 '23 19:10 Kitenite

We need this to be merged in.

incuedAA avatar Nov 14 '23 20:11 incuedAA

+1 pls

petrmiko avatar Nov 15 '23 10:11 petrmiko

Come on, we need this.

cliveportman avatar Jan 10 '24 14:01 cliveportman

Why is this not merged yet?

arielzao150 avatar Feb 01 '24 12:02 arielzao150

there is a default fix for this first get the fonts fontEmbedCss and then skipFonts

async function useHtml2Img(el: HTMLCanvasElement) {
  try {
    const fontEmbedCss = await getFontEmbedCSS(el);
    const blob = <Blob>(
      await toBlob(el, { skipFonts: true, fontEmbedCSS: fontEmbedCss })
    );
  
    if (window.saveAs) {
      window.saveAs(blob, 'new.png');
    } else {
      saveAs(blob, 'new.png');
    }

  } catch (error) {
    console.log('error: ', error);
  }
}

diadal avatar Feb 09 '24 21:02 diadal

there is a default fix for this first get the fonts fontEmbedCss and then skipFonts

async function useHtml2Img(el: HTMLCanvasElement) {
  try {
    const fontEmbedCss = await getFontEmbedCSS(el);
    const blob = <Blob>(
      await toBlob(el, { skipFonts: true, fontEmbedCSS: fontEmbedCss })
    );
  
    if (window.saveAs) {
      window.saveAs(blob, 'new.png');
    } else {
      saveAs(blob, 'new.png');
    }

  } catch (error) {
    console.log('error: ', error);
  }
}

This doesn't work. getFontEmbedCSS throws the same exception.

wendellavila avatar Jul 08 '24 17:07 wendellavila

@diadal Please can you share with us your reluctance to merge? There seems some hesitation on your part but without knowing why, we're just left not knowing what is happening and whether to persevere with this library or not.

cliveportman avatar Jul 09 '24 08:07 cliveportman