18

I'm building a website for a client in Node.js and I need to generate QR-codes or Barcodes for the ticket system.

I've found a few modules, but all of them need an install like canvas, one way or another.

I'm on a shared hosting package and my host does not allow me to install any such packages, unless I upgrade to a VPS or dedicated server (which I do not have the money for).

Does any of you know how I can pull this off in Node.js or do I need to put up a subdomain for generating the QR in PHP or front-end generating (which I do not prefer AT ALL)?

Currently using:

  • Node.js
  • Express.js
  • Angular.js

Modules found:

CherryNerd
  • 1,258
  • 1
  • 16
  • 38
  • 1
    you can just add canvas to node.js and use the stuff you already found. if you already have php installed, you can just use "ajax" from node.js's _http_ module... – dandavis Mar 15 '15 at 23:37
  • @dandavis the OP has said he can't install canvas because he can't install (probably) libcairo. Otherwise, good suggestion. – Zlatko Mar 17 '15 at 07:26
  • 99% of packages don't need to be installed; you can unzip them, copy into your project folder, and require them with "./" in front of the normal require call. you can also get a vps for $15/year, so i don't quite buy the affordable characterization, unless it refers to a specific companies offering, but even then, a vps is a vps and competition is fierce. all that said, using http to talk to php from node.js might just be the path of least resistance. – dandavis Mar 17 '15 at 19:11

5 Answers5

14

Have a look over this code:

var qr = require('qr-image');  
var express = require('express');

var app = express();

app.get('/', function(req, res) {  
  var code = qr.image(new Date().toString(), { type: 'svg' });
  res.type('svg');
  code.pipe(res);
});

app.listen(3000);

visit npm-qr-image

Balaji L
  • 141
  • 1
  • 3
  • Nice tip, i'e used it and it's great! – Marco Jun 28 '19 at 12:52
  • I understood the generating the QR code but can you please mention how to read the QR code and extract the text from it. Is it just any random qr scanner works? – Vikranth Jul 01 '19 at 20:42
9

You could use qr-image it is a full JavaScript solution using Buffers that generates QR codes in following formats: png, svg, eps and pdf formats

Luscus
  • 211
  • 2
  • 5
4

In my case i did following please refer qrcode for more information.

const qrcode = require('qrcode');
const qrOption = { 
  margin : 7,
  width : 175
};
const qrString = 'QR_STRING';
const bufferImage = await qrcode.toDataURL(qrString,qrOption);
console.log(bufferImage);
Renish Gotecha
  • 2,232
  • 22
  • 21
1

Here is another suggestion about how you can achieve this. You just need to grab the url of the QRCode and assign it to an img tag source in html. It's possible to do that with the toDataUrl() api of node-qrcode.

Here is the definition :

toDataURL(text, [options], [cb(error, url)])

Returns a Data URI containing a representation of the QR Code image. If provided, canvasElement will be used as canvas to generate the data URI.

canvasElement

Type: DOMElement Canvas where to draw QR Code.

text

Type: String|Array Text to encode or a list of objects describing segments.

options

type

Type: String Default: image/png Data URI format. Possible values are: image/png, image/jpeg, image/webp.

rendererOpts.quality

Type: Number Default: 0.92 A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.

cb Type: Function Callback function called on finish.

So in order to display the neo QRCode, this is how I suggest you to do it based on the documentation:

const fs = require('fs');
const path = require('path');
const express = require('express');
const router = module.exports = express.Router()
const QRCode = require('qrcode')

router.get('/', (req, res) => {
    QRCode.toDataURL('Hello World !').then(url => {
        res.send(`
        <h2>QRCode Generated</h2>
        <div><img src='${url}'/></div>
      `)
    }).catch(err => {
        console.debug(err)
    })
});

Coded

Yohan W. Dunon
  • 502
  • 10
  • 18
0

try it

1)npm i qrcode
2)var QRCode = require('qrcode')
3)QRCode.toString('I am a pony!',{type:'terminal'}, function (err, 
base64image) {
 console.log(base64image)
  })
hamedkke
  • 47
  • 1
  • 3
  • From Review:  Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Dec 15 '19 at 13:50