网站搜索

typescript - PdfMake 中带有换行符 (\n) 的变量


我正在尝试将包含换行符(\n)的变量传递给 PdfMake 中的表格(在一个 Ionic 3 项目中),但生成的 PDF 并没有将换行符视为新的一行。

table: {
        
        headerRows: 1,
        widths: ["80%", "20%"],

        body: [
          [
            { text: "Job Description", style: "center" },
            { text: "Total", style: "center" },
          ],
          [
            // JobDescriotiob is a string variable that contains \n character
            { text: JobDescription, style: "center" },
            { text: Price, style: "center" },
          ],
        ],
      },

结果是

然而,当我直接在文本中使用它时,(\n) 工作得很好

table: {
        
        headerRows: 1,
        widths: ["80%", "20%"],

        body: [
          [
            { text: "Job Description", style: "center" },
            { text: "Total", style: "center" },
          ],
          [
            { text: "test\nfff", style: "center" },
            { text: Price, style: "center" },
          ],
        ],
      },

结果是

有没有办法通过变量传递换行符?

答案#1

我通过将 jobdescription 中的任何字符替换为 "\n" 解决了这个问题

    JobDescription = JobDescription.split("--").join("\n");

答案#2

这对我有用:

JobDescription = JobDescription.concat("\n");