kunal deep reddy
kunal deep reddy

Reputation: 33

How to resolve the error "Cannot read property 'write' of null" in node js while working with pdfkit

i get the above error when i use this code below.

 var pdf = new PDFDocument({
        autoFirstPage: false
    });
    pdf.fontSize(10);


var table = new PdfTable(pdf, {
        bottomMargin: 30,
    });
for (var l = 0; l < attendance.length; l++) {

        pdf 
        .text("Employee Name : "+attendance[l].employee_name, 
            {
                width: 100,
                height: 50,
                align: 'left',
            }
        );




        pdf
        .fontSize(12)
        .text("Employee Role", {
            width: 100,
            height: 50,
            align: 'right'
        }
        );

        pdf.moveDown();

        table.addPlugin(new (require('voilab-pdf-table/plugins/fitcolumn'))({
            column: 'purpose',
            align: 'left'
        }))
            .setColumnsDefaults({
                headerBorder: 'B',
                align: 'center',
                padding: [8, 5, 5, 5],

            })
            .addColumns([
                {
                    id: 'sno',
                    header: '#',
                    width: 40
                },
                {
                    id: 'DATE',
                    header: 'DATE',
                    width: 70
                },
                {
                    id: 'startTime',
                    header: 'Start Time',
                    width: 70
                },
                {
                    id: 'endTime',
                    header: 'End Time',
                    width: 70,
                },
                {
                    id: 'workTime',
                    header: 'Work Time',
                    width: 100
                },
                {
                    id: 'breakTime',
                    header: 'Break Time',
                    width: 100
                },
                {
                    id: 'payable',
                    header: 'Payable',
                    width: 90,
                }
            ])
            .onPageAdded(function (tb) {
                tb.addHeader();
            });
            pdf.moveDown();
        pdf.addPage();

and i get the below error when i execute the code

TypeError: Cannot read property 'write' of null
    at PDFDocument.addContent (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:4365:15)
    at PDFDocument.save (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:1702:17)
    at PDFDocument._fragment (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:3422:10)
    at PDFDocument._line (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:3314:10)
    at LineWrapper.emit (events.js:203:15)
    at LineWrapper.EventEmitter.emit (domain.js:448:20)
    at emitLine (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:2937:12)
    at eachWord (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:2991:9)
    at LineWrapper.eachWord (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:2893:26)
    at LineWrapper.wrap (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:2943:10)
    at PDFDocument._text (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:3128:15)
    at PDFDocument.text (A:\executeapp\ppm-backend\node_modules\pdfkit\js\pdfkit.js:3141:17)
    at AttendancePdf (A:\executeapp\ppm-backend\utility\recurringTask.js:429:10)
    at A:\executeapp\ppm-backend\utility\recurringTask.js:365:48
    at A:\executeapp\ppm-backend\node_modules\mongoose\lib\model.js:4599:16
    at A:\executeapp\ppm-backend\node_modules\mongoose\lib\query.js:4351:12
    at process.nextTick (A:\executeapp\ppm-backend\node_modules\mongoose\lib\query.js:2849:28)
    at process._tickCallback (internal/process/next_tick.js:61:11)
Emitted 'error' event at:

the error occurs in the code block

     pdf
    .fontSize(12)
    .text("Employee Role", {
        width: 100,
        height: 50,
        align: 'right'
    }
    );

I have tried in many ways to get this resolved and I am pretty sure the code which i am using is correct. But i don't know where i am going wrong.

Please help me in resolving this error thanks in advance

Upvotes: 1

Views: 3196

Answers (1)

ChadJPetersen
ChadJPetersen

Reputation: 393

Ran into the same issue. What the issue was for me was that you had to add at least one page otherwise it would throw this error.

Upvotes: 1

Related Questions