Having problem in creating a barcode and printing it in PDF using PDFKIT and bwip-js

649 views Asked by At

I am trying to generate a barcode in a PDF with the below code, I've tried everything but it doesn't seems to work. :


    const PDFDocument = require('pdfkit');
    const fs = require('fs');
    const bwipjs = require('bwip-js');
    
    // Create a document
    const doc = new PDFDocument();
    
    // Pipe its output somewhere, like to a file or HTTP response
    // See below for browser usage
    doc.pipe(fs.createWriteStream('output.pdf'));
    
    // Fit the image within the dimensions
    doc.image('logo.jpg', 585, 5, {fit: [30, 30]})
      
    //text containing name of the corp
    doc
    .fontSize(7)
    .text("SpaceCode Solutions", 200, 40, {
      width: 410,
      align: 'right'
    }
    );
    
    async function generateBarcode(text) {
      return new Promise((resolve, reject) => {
          bwipjs.toBuffer({
              bcid: 'code128',
              text,
              scale: 5,
              height: 10,
              includetext: false,
              textxalign: 'center'
          }, (err, png) => {
              if (err) reject(err);
              else resolve(png);
          });
      });
    }
    
    let unitId = 5;
    
    let unitIdCheck = "Aman";
    
    let x = 50;
    let y = 90;
    
    function checkTextSum(data) {
      let mChar = require('cdigit').mod37_2.generate(data);
      return mChar.charAt(mChar.length - 1);
    }
    
    
    async function roshan(){
      await generateBarcode("=" + unitId).then(barcode => {
      doc.image(barcode, x, y, {width: 200});
    });
    }
    
    roshan();
    doc.end();

But, it throws this error :

**events.js:174
      throw er; // Unhandled 'error' event
      ^
Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF
    at readableAddChunk (_stream_readable.js:257:32)
    at PDFDocument.Readable.push (_stream_readable.js:224:10)
    at PDFDocument._write (C:\Roshan Singh\spaceCode\node_modules\pdfkit\js\pdfkit.js:6180:10)
    at PDFReference.finalize (C:\Roshan Singh\spaceCode\node_modules\pdfkit\js\pdfkit.js:258:19)
    at PDFReference.end (C:\Roshan Singh\spaceCode\node_modules\pdfkit\js\pdfkit.js:237:17)
    at PNGImage.finalize (C:\Roshan Singh\spaceCode\node_modules\pdfkit\js\pdfkit.js:4433:13)
    at image.decodePixels.pixels (C:\Roshan Singh\spaceCode\node_modules\pdfkit\js\pdfkit.js:4468:19)
    at Inflate.zlib.inflate [as cb] (C:\Roshan Singh\spaceCode\node_modules\png-js\png-node.js:332:14)
    at Inflate.zlibBufferOnEnd (zlib.js:133:10)
    at Inflate.emit (events.js:203:15)
Emitted 'error' event at:
    at errorOrDestroy (internal/streams/destroy.js:107:12)
    at readableAddChunk (_stream_readable.js:257:9)
    at PDFDocument.Readable.push (_stream_readable.js:224:10)
    [... lines matching original stack trace ...]
    at Inflate.zlibBufferOnEnd (zlib.js:133:10)**

As far as I know, I am doing everything right. Can anyone please suggest me a solution to this problem. I've tried removing doc.end(); The error goes away but the PDF file doesn't opens up then.

1

There are 1 answers

1
Eirik Rindal On

You are closing the PDF document before the barcode is produce, this works for me:

const PDFDocument = require('pdfkit');
const fs = require('fs');
const bwipjs = require('bwip-js');

// Create a document
const doc = new PDFDocument();

// Pipe its output somewhere, like to a file or HTTP response
// See below for browser usage
doc.pipe(fs.createWriteStream('output.pdf'));

// Fit the image within the dimensions
doc.image('logo.jpg', 585, 5, {fit: [30, 30]})
  
//text containing name of the corp
doc
.fontSize(7)
.text("SpaceCode Solutions", 200, 40, {
  width: 410,
  align: 'right'
}
);

async function generateBarcode(text) {
  return new Promise((resolve, reject) => {
      bwipjs.toBuffer({
          bcid: 'code128',
          text: 'hello World',
          scale: 5,
          height: 10,
          includetext: false,
          textxalign: 'center'
      }, (err, png) => {
          if (err) reject(err);
          else resolve(png);
      });
  });
}

let unitId = 5;

let unitIdCheck = "Aman";

let x = 50;
let y = 90;

function checkTextSum(data) {
  let mChar = require('cdigit').mod37_2.generate(data);
  return mChar.charAt(mChar.length - 1);
}


async function roshan(){
  await generateBarcode("=" + unitId).then(barcode => {
  doc.image(barcode, x, y, {width: 200});
});
}

async function main () {
    await roshan();
    doc.end();
}

main()