【问题标题】:Align text left using jsPDF,jsPDF auto-table使用jsPDF,jsPDF自动表格左对齐文本
【发布时间】:2021-02-06 14:00:14
【问题描述】:

如何设置页眉左对齐,我用的是jspdf和jspdf-auto table javascript库

代码

const doc = new jsPDF("p", "pt", "a4");
doc.autoTable({
  startY: 130,
  margin: { top: 80, bottom: 20 },
  didDrawPage: function (data) {
    data.settings.margin.top = 40;
  },
  bodyStyles: { 2: { halign: 'right' }, 4: { halign: 'right' }, 5: { halign: 'right' }, 6: { halign: 'right' }, 7: { halign: 'right' } },
  columnStyles: { 2: { halign: 'right' }, 4: { halign: 'right' }, 5: { halign: 'right' }, 6: { halign: 'right' }, 7: { halign: 'right' } },
  headStyles: { halign: 'right' }, // Purple
  body: this.PDFArray,
  columns: Object.keys(this.PDFArray[0]).map((column) => ({
    header: column,
    dataKey: column,
  })),
  theme: "plain",
  tableWidth: "auto",
});

【问题讨论】:

    标签: angular pdf jspdf jspdf-autotable


    【解决方案1】:

    headStyles: { halign: 'right' } 会将标题行中的所有单元格设置为右对齐。

    如果您想设置单个标题单元格的样式,请将headStyles 替换为didParseCell

        didParseCell: data => {
          if (data.cell && data.cell.section === 'head') {
            switch (data.cell.raw) {
              case "SI.":
              case "Particulars":
              case "Unit":
                  data.cell.styles.halign = 'left'
                break;
              default:
                data.cell.styles.halign = 'right'
                break;
            }
          }
        }
    

    您还可以按照相同的方法设置其他样式属性,例如 fontfontSizefontStylefillColor 等。

    注意标题列名称的大小写,否则单元格的格式可能不是您想要的。

    【讨论】:

      猜你喜欢
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2017-06-15
      相关资源
      最近更新 更多