js 生成ppt文件

483 阅读1分钟
import moment from 'moment'
import pptxgen from 'pptxgenjs'
import xxheader from '@/assets/images/t3-header.png'
import xxheader from '@/assets/images/t4-header.png'
import xxfooter from '@/assets/images/t3-footer.png'

function creatPdf (type = 't3', list = [], notceDate, pptName) {
  var pres = new pptxgen()
  const length = list.length
  // 判断数据是否大于15条
  const flag = length > 15
  pres.defineLayout({ name: 'A3', width: 10, height: !flag ? 15.5 : 13.5 })
  pres.layout = 'A3'
  // 模板
  let objects = [
    {
      image: {
        // data: xx_header,
        path: type === 'xx' ? xxheader : xxxheader,
        x: 0,
        y: 0,
        w: '100%',
        h: 2.8,
        sizing: { type: 'contain' }
      }
    }
  ]
  if (!flag) {
    objects.push({
      image: {
        // data: t3_header,
        path: t3footer,
        x: 0,
        y: 12.5,
        w: '100%',
        h: 3,
        sizing: { type: 'contain' }
      }
    })
  }
  pres.defineSlideMaster({
    title: 'MASTER_SLIDE',
    background: { color: '1f1d90' },
    objects
    // slideNumber: { x: 6.5, y: 12, fontSize: 10 }
  })
  // 第一张幻灯片
  // 日期
  let slide = pres.addSlide({ masterName: 'MASTER_SLIDE' })
  slide.addText(`公告日期:${moment().format('YYYY/MM/DD')}`, {
    x: 0,
    y: 2.5,
    w: 9.5,
    h: 0.5,
    fontSize: 14,
    color: 'ffffff',
    align: 'right',
    fontFace: '微软雅黑'
  })
  // 白色框框
  slide.addShape(pres.shapes.ROUNDED_RECTANGLE, {
    x: 0.5,
    y: 3,
    w: 9,
    h: 9.5,
    fill: { color: 'ffffff' },
    rectRadius: 0.2
  })
  slide.addText(
    [
      {
        text: '大家好!',
        options: {
          // bullet: { code: '25BA' },
          color: '000000',
          fontFace: '微软雅黑',
          bold: true,
          breakLine: true
        }
      },
      {
        text: '\t在公司各级领导及各单位的支持下, 系统稳定运行中, 为帮助工厂提升生产管理效率,  提升设备稼动率, 异常及时预防和拦截, 需要持续进行系统功能扩展,现将本次更新与改善事项公告如下:',
        options: {
          // bullet: { code: '25BA' },
          // bullet: { numberStartAt: '5' },
          // paraSpaceBefore: '12',
          color: '000000',
          fontFace: '微软雅黑',
          bold: true,
          // indentLevel: 2
          tabStops: [
            {
              position: 0.5
            }
          ]
        }
      }
    ],
    { x: 0.6, y: 3.05, w: 8.8, h: 1.2, color: 'f77f00', fontSize: 14, lineSpacingMultiple: 1.3 }
  )
  slide.addText(
    [
      {
        text: '【发布内容】',
        options: {
          // bullet: { code: '25BA' },
          color: '0d6efd',
          fontFace: '微软雅黑',
          fontSize: 16,
          bold: true,
          breakLine: true
        }
      },
      {
        text: 'MES System update content (',
        options: {
          bullet: { code: '25C6' },
          color: '000000',
          fontFace: '微软雅黑',
          fontSize: 14,
          bold: true
        }
      },
      {
        text: `此次有 MES:${length}项 更新功能`,
        options: {
          color: 'ff4d4f',
          fontFace: '微软雅黑',
          fontSize: 14,
          bold: true
        }
      },
      {
        text: ')',
        options: {
          color: '000000',
          fontFace: '微软雅黑',
          fontSize: 14,
          bold: true,
          breakLine: true
        }
      }
    ],
    { x: 0.6, y: 4.2, w: 8.8, h: 0.8, color: 'f77f00', lineSpacingMultiple: 1.5 }
  )
  // 动态文字内容
  let firstArr = []
  if (flag) {
    firstArr = list.slice(0, 15)
  } else {
    firstArr = list
  }
  const textArr = firstArr.map((item) => {
    return {
      text: item.notice,
      options: {
        bullet: { code: '27A2' },
        color: '0d6efd',
        fontFace: '微软雅黑',
        fontSize: 14,
        bold: true
      }
    }
  })
  slide.addText(textArr, {
    x: 1,
    y: 5.1,
    w: 8.4,
    h: 7,
    color: '0d6efd',
    valign: 'top',
    lineSpacingMultiple: 1.6
  })
  if (!flag) {
    slide.addText(
      [
        {
          text: '【更新时间】:',
          options: {
            color: '0d6efd',
            fontFace: '微软雅黑',
            fontSize: 16,
            bold: true,
            lineSpacingMultiple: 1.5
          }
        },
        {
          text: moment().format('YYYY/MM/DD hh:mm'),
          options: {
            color: 'ff4d4f',
            fontFace: '微软雅黑',
            fontSize: 14,
            bold: true
          }
        }
      ],
      { x: 1, y: 11.5, w: 8.4, h: 0.7, color: '0d6efd' }
    )
  }
  // 创建第二张图
  if (flag) {
    let slide2 = pres.addSlide({ masterName: 'MASTER_SLIDE' })
    slide2.addShape(pres.shapes.ROUNDED_RECTANGLE, {
      x: 0.5,
      y: 1.8,
      w: 9,
      h: 8.5,
      fill: { color: 'ffffff' },
      rectRadius: 0.2
    })
    // 动态文字内容
    let secondArr = []
    secondArr = list.slice(15)
    const textArr = secondArr.map((item) => {
      return {
        text: item.notice,
        options: {
          bullet: { code: '27A2' },
          color: '0d6efd',
          fontFace: '微软雅黑',
          fontSize: 14,
          bold: true
        }
      }
    })
    slide2.addText(textArr, {
      x: 1,
      y: 2.2,
      w: 8.4,
      h: 7,
      color: '0d6efd',
      valign: 'top',
      lineSpacingMultiple: 1.6
    })
    slide2.addText(
      [
        {
          text: '【更新时间】:',
          options: {
            color: '0d6efd',
            fontFace: '微软雅黑',
            fontSize: 16,
            bold: true,
            lineSpacingMultiple: 1.5
          }
        },
        {
          text: moment().format('YYYY/MM/DD hh:mm'),
          options: {
            color: 'ff4d4f',
            fontFace: '微软雅黑',
            fontSize: 14,
            bold: true
          }
        }
      ],
      { x: 1, y: 9.5, w: 8.4, h: 0.7, color: '0d6efd' }
    )
    slide2.addImage({
      path: xxfooter,
      x: 0,
      y: 10.5,
      w: '100%',
      h: 3,
      sizing: { type: 'contain' }
    })
  }
  pres.writeFile({ fileName: pptName || `${type}_系统上线公告.pptx` })
}

export default creatPdf