以表格格式将JSON数据转换为PDF?

时间:2017-05-22 18:19:32

标签: jquery pdf

我们如何使用 Jquery 将以下JSON data转换为PDF tabular format?我们提供了按钮,它将以 PDF

呈现数据

将此转换为 PDF 的任何帮助?我是jquery的新手,不知道我们怎么能这样做?

[
    {
        "file": "C:\\FI\\POC_FI\\LightningExamples\\Circular Progress Bar\\CircularProgress\\CircularProgressHelper.js",
        "result": [
            {
                "ruleId": "radix",
                "severity": 2,
                "message": "Missing radix parameter.",
                "line": 15,
                "column": 44,
                "nodeType": "CallExpression",
                "source": "        if(totalVal && actualVal && !isNaN(parseInt(totalVal)) && isFinite(totalVal) && !isNaN(parseInt(actualVal)) && isFinite(actualVal)){"
            },
            {
                "ruleId": "radix",
                "severity": 2,
                "message": "Missing radix parameter.",
                "line": 15,
                "column": 96,
                "nodeType": "CallExpression",
                "source": "        if(totalVal && actualVal && !isNaN(parseInt(totalVal)) && isFinite(totalVal) && !isNaN(parseInt(actualVal)) && isFinite(actualVal)){"
            },
            {
                "ruleId": "radix",
                "severity": 2,
                "message": "Missing radix parameter.",
                "line": 17,
                "column": 27,
                "nodeType": "CallExpression",
                "source": "            var percVal = parseInt(actualVal) / parseInt(totalVal) ;"
            }
        ]
    },
    {
        "file": "C:\\FI\\POC_FI\\LightningExamples\\Responsive Grid\\DataGrid\\DataGridHelper.js",
        "result": [
            {
                "ruleId": "default-case",
                "severity": 2,
                "message": "Expected a default case.",
                "line": 21,
                "column": 13,
                "nodeType": "SwitchStatement",
                "source": "            switch(currentTag.getDef().getDescriptor().getName()){"
            },
            {
                "ruleId": "no-console",
                "severity": 2,
                "message": "Unexpected console statement.",
                "line": 48,
                "column": 21,
                "nodeType": "MemberExpression",
                "source": "                    console.log('Detected Iteration tag');"
            }
        ]
    },
    {
        "file": "C:\\FI\\POC_FI\\LightningExamples\\Responsive Grid\\ResponsiveDatagridWrapper\\ResponsiveDatagridWrapperController.js",
        "result": []
    },
    {
        "file": "C:\\FI\\POC_FI\\LightningExamples\\Responsive Grid\\ResponsiveDatagridWrapper\\ResponsiveDatagridWrapperHelper.js",
        "result": [
            {
                "ruleId": "no-alert",
                "severity": 2,
                "message": "Unexpected alert.",
                "line": 34,
                "column": 21,
                "nodeType": "CallExpression",
                "source": "                    alert('Error in Component - '+errors[0].message);"
            }
        ]
    },
]

1 个答案:

答案 0 :(得分:-1)

你可能想看看pdf.js.还有一个额外的包,它将添加表支持。

看这里

https://github.com/simonbengtsson/jsPDF-AutoTable

我过去曾将它用于一个项目并且渲染得非常好。你仍然需要在代码中加入一些工作来从json中获取数据。

[编辑]

如果是一个字符串,需要先解析。 JSON.parse(yourString)。如果它已经是JSON,你可以直接启动。使用jsPDFAutotable,它看起来与此类似

var columns = ['RuleID', 'Severity', 'Message'....];
var rows = [];
$.each(yourJSON, function(){
   $.each(this.result, function(){
      rows.push([this.ruleId, this.severity, this.message,...]);
   });
});

var doc = new jsPDF();
doc.autoTable(columns, rows);
doc.save('table.pdf');

[/编辑]