Skip to content

Instantly share code, notes, and snippets.

@trpfrog
Created October 3, 2023 13:32
Show Gist options
  • Save trpfrog/70c28ad13ccb951337b81e55054b70f8 to your computer and use it in GitHub Desktop.
Save trpfrog/70c28ad13ccb951337b81e55054b70f8 to your computer and use it in GitHub Desktop.
Satori で Google Fonts を使う
import satori from 'satori'
import fs from 'fs'
// fetch font information
const endpoint = new URL('https://www.googleapis.com/webfonts/v1/webfonts');
endpoint.searchParams.set('family', 'M PLUS Rounded 1c');
endpoint.searchParams.set('key', process.env.GOOGLE_FONTS_API_KEY);
const fontInfo = await fetch(endpoint).then(res => res.json());
// fetch font
const fontResponse = await fetch(fontInfo.items[0].files['regular']);
const fontBuffer = await fontResponse.arrayBuffer();
const svg = await satori(
<div style={{
alignItems: 'center',
justifyContent: 'center',
background: 'lightgray',
width: '100%',
height: '100%',
fontSize: 32,
}}>
Hello, Google Fonts!
</div>,
{
width: 600,
height: 400,
fonts: [
{
name: 'M PLUS Rounded 1c',
data: fontBuffer,
},
],
},
)
fs.writeFileSync('output.svg', svg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment