我正在尝试在Firebase Cloud Functions上使用puppeteer截取带有表情符号的页面的屏幕截图,但是表情符号在保存的屏幕截图中无法正确显示。
Cloud Functions上的Puppeteer似乎没有表情符号字体,因此我尝试通过@ font-face设置表情符号字体,但是我没有。
这是我的云功能代码。 我在一些HTTPS Cloud Functions中调用屏幕快照功能。
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const storage = admin.storage();
const puppeteer = require('puppeteer');
function screenshot() {
(async () => {
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36');
await page.evaluate(() => {
const style = document.createElement('style')
style.textContent = `
@font-face {
font-family: "emoji";
src:local("Noto Color Emoji"),
url("./NotoColorEmoji.ttf");
};
@font-face {
font-family: 'Noto Sans JP';
font-style: normal;
font-weight: 400;
src: local("Noto Sans CJK JP"),
url(//fonts.gstatic.com/ea/notosansjp/v5/NotoSansJP-Regular.woff2) format('woff2'),
url(//fonts.gstatic.com/ea/notosansjp/v5/NotoSansJP-Regular.woff) format('woff'),
url(//fonts.gstatic.com/ea/notosansjp/v5/NotoSansJP-Regular.otf) format('opentype');
};
body, svg{ font-family: "Noto Sans JP", "emoji", sans-serif; };`
document.head.appendChild(style);
document.body.style.fontFamily = "'Noto Sans JP', 'emoji' , sans-serif";
})
await page.waitFor(5000); // waiting time to load font
await page.goto("https://getemoji.com/"); //go to emoji page
await page.screenshot({
path: '/tmp/screenshot.png',
omitBackground: true,
})
.then(() => {
// upload screenshot to firebase storage
storage.bucket('bucketName').upload('/tmp/screenshot.png')
.then(() => {
console.log("success");
})
.catch(error => {
console.log(error);
});
})
.catch(e => {
console.log(e);
});
})();
}
,这是结果屏幕截图: screenshot.png