2.0.0 • Published 1 year ago

publicfortest v2.0.0

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

PDF 报告生成器

API

import PDFGenerator from '@cc/pdf-generator';
import opsReportDoc from '@cc/pdf-generator/lib/template/ops-report';

import SourceHanSansSCNormal from './assets/fonts/SourceHanSansSC-Normal.ttf';
import SourceHanSansSCMedium from './assets/fonts/SourceHanSansSC-Medium.ttf';

const generatePDF = (state, language) => {
  const opsReportPDF = new PDFGenerator(opsReportDoc, { language });

  const { info, resources } = await prepareData(state, language);
  /*
    info (数据结构: src/template/ops-report/types.ts)
    {
      "reportDate": "2020/09/02",
      "reportId": "case0001",
      "patientId": "001",
      "checkDate": "2020/08/08",
      "patientName": "张三李四",
      "gender": "男",
      "age": "66",
      "hospital": "科亚医疗",
      "remark": "AAA\nBBBBBB\nCCCCCCCCCC",
      "isShowDisclaimer": true,
      "attentions": [...],
      "branches": [...]
    }

    resources:
    [
      {type: "image", name: "report_t8hxi3igmdh.png", data: Uint8Array},
      {type: "image", name: "report_6hccxly14xk.png", data: Uint8Array},
      {type: "image", name: "report_8q8uo131gh2.png", data: Uint8Array},
    ]
  */

  opsReportPDF.setData(info);
  resources
    .filter((d) => d.type === 'image')
    .forEach(({ name, data }) => {
      opsReportPDF.addImage(name, data);
    });
  opsReportPDF.addFont('SourceHanSansSCMedium', SourceHanSansSCMedium);
  opsReportPDF.addFont('SourceHanSansSCNormal', SourceHanSansSCNormal);

  const save = async (filename) => opsReportPDF.save(filename);

  const toBuffer = opsReportPDF.toBuffer;

  return {
    save,
    toBuffer,
  };
}