22 lines
760 B
TypeScript
22 lines
760 B
TypeScript
import { PDFDocument } from "pdf-lib";
|
|
import { Font, GlyphVariant, glyphVariantToUrl } from "./glyphs.ts";
|
|
|
|
export const glyphsToPdf = async (font: Font, glyphs: GlyphVariant[]) => {
|
|
const pdf = await PDFDocument.create();
|
|
|
|
for (const glyph of glyphs) {
|
|
const url = glyphVariantToUrl(glyph, font);
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
throw `Failed to fetch glyph "${glyph[0]}", variant ${glyph[1]}. HTTP ${res.status}: ${res.statusText}; URL: ${url}`;
|
|
}
|
|
const bytes = await res.arrayBuffer();
|
|
const image = await pdf.embedPng(bytes);
|
|
const { width, height } = image.scaleToFit(210, 297);
|
|
|
|
const page = pdf.addPage([210, 297]);
|
|
page.drawImage(image, { width, height });
|
|
}
|
|
|
|
return await pdf.save();
|
|
};
|