hand-dot / labelmake

labelmake has moved and now available at pdfme / https://github.com/pdfme/pdfme

Home Page:https://labelmake.jp/javascript-pdf-generator-library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`TemplateSchema`をexportしてほしい

rktyt opened this issue · comments

commented

https://github.com/hand-dot/labelmake/blob/v2.0.15/src/type.ts#L17-L30
↑をexportしてもらいたいです。

理由は
typescriptで下記のような形で定義する場合にエラーとなるため。

import labelmake from 'labelmake'
import type { Template } from 'labelmake/dist/types/type'
import opentype from 'opentype.js'

const generatePdf = async (): Promise<Blob> => {
  const fontFamilyName = 'NotoSansCJKjp'
  const fontStyleName = 'Medium'

  const [font, basePdf] = await Promise.all([
    fetch(`/fonts/otf/${fontFamilyName}-${fontStyleName}.otf`).then((res) =>
      res.arrayBuffer(),
    ),
    fetch('/base.pdf').then((res) => res.arrayBuffer()),
  ])

  const schemas = [
    {
      顧客名: {
        type: 'text',
        position: {
          x: 17,
          y: 32.49,
        },
        width: 82.75,
        height: 7.55,
        alignment: 'center',
        fontSize: 11,
        characterSpacing: 0,
        lineHeight: 1,
      },
      見積No: {
        type: 'text',
        position: {
          x: 37,
          y: 62.5,
        },
        width: 75.13,
        height: 5.58,
        alignment: 'left',
        fontSize: 10,
        characterSpacing: 0,
        lineHeight: 1,
      },
      納期: {
        type: 'text',
        position: {
          x: 37,
          y: 68.5,
        },
        width: 75.13,
        height: 5.58,
        alignment: 'left',
        fontSize: 10,
        characterSpacing: 0,
        lineHeight: 1,
      },
      納品場所: {
        type: 'text',
        position: {
          x: 37,
          y: 74.5,
        },
        width: 75.13,
        height: 5.58,
        alignment: 'left',
        fontSize: 10,
        characterSpacing: 0,
        lineHeight: 1,
      },
      取引方法: {
        type: 'text',
        position: {
          x: 37,
          y: 80.5,
        },
        width: 75.13,
        height: 5.58,
        alignment: 'left',
        fontSize: 10,
        characterSpacing: 0,
        lineHeight: 1,
      },
      有効期限: {
        type: 'text',
        position: {
          x: 37,
          y: 86.5,
        },
        width: 75.13,
        height: 5.58,
        alignment: 'left',
        fontSize: 10,
        characterSpacing: 0,
        lineHeight: 1,
      },
    },
  ]

  const inputs: Record<string, string>[] = [
    {
      見積No: '1234567890',
      顧客名: 'テスト会社',
      納期: 'データ入稿日より3営業日',
      納品場所: '北海道',
      取引方法: '原則としてご⼊⾦確認後の納品となります。',
      有効期限: '2020年 2⽉ 29⽇',
    },
  ]

  const allStrings = inputs.reduce((acc, cur) => {
    Object.keys(cur).forEach((key) => {
      cur[key].split('').forEach((str) => {
        acc.add(str)
      })
      return acc
    }, acc)
    return acc
  }, new Set<string>())

  const subset = generateSubsetFont(
    { font, familyName: fontFamilyName, styleName: fontStyleName },
    allStrings,
  )

  const template: Template = {
    schemas,
    basePdf,
    fontName: fontFamilyName,
  }

  const { buffer } = await labelmake({
    template,
    inputs,
    font: {
      [`${fontFamilyName}`]: {
        data: subset,
        subset: false,
      },
    },
  })

  const blob = new Blob([buffer], { type: 'application/pdf' })

  return blob
}

const generateSubsetFont = (
  fontObj: { font: ArrayBuffer; familyName: string; styleName: string },
  useStrings: Set<string>,
): ArrayBuffer => {
  const font = opentype.parse(fontObj.font)
  const subsetGlyphs = font.stringToGlyphs([...useStrings].join(''))
  const subset = new opentype.Font({
    familyName: fontObj.familyName,
    styleName: fontObj.styleName,
    unitsPerEm: font.unitsPerEm,
    ascender: font.ascender,
    descender: font.descender,
    glyphs: subsetGlyphs,
  })
  return subset.toArrayBuffer()
}

上記で発生するエラー

  const template: Template = {
    schemas, // <- Type Error: The expected type comes from property 'schemas' which is declared here on type 'Template'
    basePdf,
    fontName: fontFamilyName,
  }

該当部分のコードを下記のようにすればエラーは回避出来るけれども、

  const template: Template = {
    schemas,
    basePdf,
    fontName: fontFamilyName,
  } as Template

schemasの定義に型追加して、下記のように出来たほうが好ましいので。

import type { Template, TemplateSchema } from 'labelmake/dist/types/type'
...
  const schemas: Record<string, TemplateSchema>[] = [
  ...
  ]
...
  const template: Template = {
    schemas,
    basePdf,
    fontName: fontFamilyName,
  }
...

@rktyt
了解です 次回リリースで回収します

@rktyt
labelmake (2.0.16) でTemplateSchemaをexportしています。
ご確認よろしくお願いします

commented

確認しました
ありがとうございます