使用 js 框架将 json 转换为 pdf
Converting json to pdf using js frameworks
我想通过客户端Javascript将json数据转换为pdf文件。你能指出我一个有用的方向吗?
例如,我想转换这个 json
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
成 pdf...
Employees
FirstName: John LastName :Doe
FirstName: Anna LastName :Smith
FirstName: Peter LastName :Jones
您可以使用jsPDF在客户端上生成PDF。
var employees = [
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
];
var doc = new jsPDF();
employees.forEach(function(employee, i){
doc.text(20, 10 + (i * 10),
"First Name: " + employee.firstName +
"Last Name: " + employee.lastName);
});
doc.save('Test.pdf');
您可以使用同时支持客户端和服务器端渲染的pdfmake
//import pdfmake
import pdfMake from 'pdfmake/build/pdfmake.js';
import pdfFonts from 'pdfmake/build/vfs_fonts.js';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
const employees = [
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
];
const document = { content: [{text: 'Employees', fontStyle: 15, lineHeight: 2}] }
employees.forEach(employee => {
document.content.push({
columns: [
{ text: 'firstname', width: 60 },
{ text: ':', width: 10 },
{ text:employee.firstName, width: 50 },
{ text: 'lastName', width: 60 },
{text: ':', width: 10 }, { text: employee.lastName, width: 50}
],
lineHeight: 2
});
});
pdfMake.createPdf(document).download();
相关文章:
- 将画布转换为pdf:黑色背景
- 如何在不在本地下载的情况下将url中提供的文件(pdf/doc)转换为json/string/base64格式
- 将网页转换为具有样式的pdf
- 如何在使用jsPDF将html转换为pdf的同时,在pdf中的某个点进行分页
- 如何将带有大量svg图表的html转换为pdf
- PDF到JPG的转换器
- 如何使用javascript或jquery将整个html页面转换为pdf
- c# mvc nreco HtmlToPdfConverter 问题将 html 文档转换为 pdf
- 使用 php 将 pdf 文件转换为 html 文件
- HTML到PDF的转换仅在客户端使用JavaScript
- 我们如何将URL的PDF转换为convert.javascript中的ToBase64String
- 将数据库中的数据转换为可下载的PDF文件
- HTML和CSS转换为JavaScript中的PDF
- 将Markdown文件转换为PDF,同时剥离Jekyll Front Matter
- 谷歌应用程序脚本:将文本文档转换为PDF的代码
- 谷歌应用程序将电子邮件表单脚本转换为PDF格式,但希望清除一些单元格
- Java使用css+js将html转换为pdf
- 如何使用javascript将Html页面转换为PDF
- 如何使用pdfmake将html表格转换为pdf
- JSPDF添加黑色背景转换PDF