如何将Google Web字体与phantomjs一起使用 [英] How can I use google web fonts with phantomjs

查看:148
本文介绍了如何将Google Web字体与phantomjs一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用phantomjs版本1.9.7,我认为它应该支持网络字体.我已经使用Google Web字体插入了字体,但是当我自动抓屏时,它只是显示我的后备字体.网络字体在我所有的浏览器中都可以正常显示.有任何解决方法吗?

I'm using phantomjs version 1.9.7 which I believe is supposed to support web fonts. I have inserted the font with google web fonts however it is just displaying my fallback font when I automate a screengrab. The webfont is displaying fine in all my browsers. Are there any workarounds for this?

推荐答案

我已经为这个问题苦苦挣扎了好几个小时.嗯,引起争议的原因很简单:用户代理

I have struggled with this issue for several hours. Well, there is a simple reason for this controversy: user-agent!

某些服务(例如Google字体)正在根据用户代理返回不同的CSS内容.当您呼叫包含带有默认PhantomJs用户代理的Google字体的网页时,Google会返回PhantomJs支持的TTF版本.

Some services such as Google Fonts are returning different CSS content based on user-agent. When you call a webpage which includes Google Fonts with default PhantomJs user-agent, Google would return TTF version which is supported in PhantomJs.

但是,如果您设置了自定义用户代理(Chrome,FF等),则Google字体会返回.woff2版本. PhantomJs 2.x不支持.woff2.显然,字体不会被加载.

However, if you set a custom user agent (Chrome, FF, etc.), Google Fonts would return .woff2 version. .woff2 is not supported in PhantomJs 2.x. Obviously, fonts are not going to be loaded.

因此,对于在不设置通用用户代理的情况下测试PhantomJ的用户而言,Google字体可以使用.如果他们设置了例如Google Chrome浏览器用户代理,它将无法正常工作.

So, for users who are testing PhantomJs without setting a generic user-agent, Google Fonts is working. If they set for example Google Chrome user-agent, it does not work.

因此,您有两个选择:

  1. 尽可能避免设置自定义用户代理.
  2. 避免使用诸如Google Fonts之类的笨拙的智能字体提供程序,该字体提供程序不会在CSS中输出所有字体版本,而应让浏览器确定所需的内容.

这篇关于如何将Google Web字体与phantomjs一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆