1 import type { Template } from '@pdfme/common';
2 import { generate } from '@pdfme/generator';
4 import chunk from '@proton/utils/chunk';
6 import interBold from './fonts/Inter-Bold.ttf';
7 import interRegular from './fonts/Inter-Regular.ttf';
8 import interSemiBold from './fonts/Inter-SemiBold.ttf';
9 import emptyTemplate from './templates/empty_template.pdf';
11 const getFonts = async () => {
12 const [interRegularData, interBoldData, interSemiBoldData] = await Promise.all(
13 [interRegular, interBold, interSemiBold].map((asset) => {
14 return fetch(asset).then((res) => res.arrayBuffer());
17 return [interRegularData, interBoldData, interSemiBoldData] as const;
20 const getPreload = (asset: string, as?: 'font') => {
21 const link = document.createElement('link');
22 link.setAttribute('rel', 'prefetch');
23 link.setAttribute('href', asset);
25 link.setAttribute('as', 'font');
30 export const getPrefetch = () => {
32 getPreload(emptyTemplate),
33 ...[interRegular, interBold, interSemiBold].map((asset) => {
34 return getPreload(asset, 'font');
39 export const getFont = async () => {
40 const [interRegularData, interBoldData, interSemiBoldData] = await getFonts();
44 data: interRegularData,
51 data: interSemiBoldData,
56 // Copy exported template here
57 const exportedTemplate = {
72 fontName: 'interBold',
89 'recovery phrase line 1': {
98 characterSpacing: 0.5,
99 fontName: 'interBold',
102 'recovery phrase line 2': {
111 characterSpacing: 0.5,
112 fontName: 'interBold',
117 columns: ['email title', 'date', 'recovery phrase line 1', 'recovery phrase line 2'],
120 'email title': 'eric.norbert@gmail.com',
121 date: 'Created on May 30, 2023',
122 'recovery phrase line 1': 'auto pottery age relief turkey face',
123 'recovery phrase line 2': 'tide useful near lottery alley wolf',
128 export function getTemplate(basePdf: any): Template {
131 ...(exportedTemplate as Omit<Template, 'basePdf'>),
135 type InputKeys = keyof (typeof exportedTemplate.schemas)[0];
136 type Input = { [key in InputKeys]: string };
138 export async function generatePDFKit({
143 emailAddress: string;
145 recoveryPhrase: string;
147 const phraseArray = recoveryPhrase.split(' ').map((s) => s.trim());
148 const [phraseLine1 = [], phraseLine2 = []] = chunk(phraseArray, 6);
150 const inputs: Input[] = [
152 'recovery phrase line 1': phraseLine1.join(' '),
153 'recovery phrase line 2': phraseLine2.join(' '),
155 'email title': emailAddress,
159 const pdf = (await generate({
160 template: getTemplate(emptyTemplate),
162 options: { font: await getFont() },
163 })) as Uint8Array<ArrayBuffer>; // TODO: Improve this type