Converting json to pdf using js frameworks

前端 未结 2 2072
走了就别回头了
走了就别回头了 2020-12-13 16:38

I want to convert json data into a pdf file via client-side Javascript. Can you please point me in a helpful direction?

For example, I\'d like to convert this json

相关标签:
2条回答
  • 2020-12-13 16:59

    You can use pdfmake which support both client side and server side rendering

    //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();
    
    0 讨论(0)
  • 2020-12-13 17:17

    You can generate PDF's on the client using jsPDF .

    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');
    
    0 讨论(0)
提交回复
热议问题