Linh Duong
Linh Duong

Reputation: 169

How to create vCard using javascript?

I found an extension to resolve my problem. But when i clone it to local, it doesn't have any example.

I am confused about how to use it. I try some method, but it not work for me. Please show me how to use it or any extension to solve my problem?

Upvotes: 10

Views: 24055

Answers (3)

Yosvel Quintero
Yosvel Quintero

Reputation: 19090

As you wrote in the comment yes: vCards JS uses NodeJS.

According to the vCards-js/README.md:

Install:

npm install vcards-js --save

Usage:

Simple example of how to create a basic vCard and how to save it to a file, or view its contents from the console:

const vCard = require('vcards-js');

//create a new vCard
vCard = vCard();

//set properties
vCard.firstName = 'Eric';
vCard.middleName = 'J';
vCard.lastName = 'Nesser';
vCard.organization = 'ACME Corporation';
vCard.photo.attachFromUrl('https://avatars2.githubusercontent.com/u/5659221?v=3&s=460', 'JPEG');
vCard.workPhone = '312-555-1212';
vCard.birthday = new Date('01-01-1985');
vCard.title = 'Software Developer';
vCard.url = 'https://github.com/enesser';
vCard.note = 'Notes on Eric';

//save to file
vCard.saveToFile('./eric-nesser.vcf');

//get as formatted string
console.log(vCard.getFormattedString());

Also you can use vCards JS on your website. Below is an example of how to get it working on Express 4:

const express = require('express');
const router = express.Router();

module.exports = function(app) {
  app.use('/', router);
};

router.get('/', function(req, res, next) {

  const vCard = require('vcards-js');

  //create a new vCard
  vCard = vCard();

  //set properties
  vCard.firstName = 'Eric';
  vCard.middleName = 'J';
  vCard.lastName = 'Nesser';
  vCard.organization = 'ACME Corporation';

  //set content-type and disposition including desired filename
  res.set('Content-Type', 'text/vcard; name="enesser.vcf"');
  res.set('Content-Disposition', 'inline; filename="enesser.vcf"');

  //send the response
  res.send(vCard.getFormattedString());
});

Upvotes: 9

Maxim Mazurok
Maxim Mazurok

Reputation: 4162

One of the best libraries for creating vCards is ez-vcard, it is written in Java, but I created a NodeJS / TS wrapper, so now you can use it from server-side JavaScript.

npm; GitHub

npm i ez-vcard
import path from 'path';
import ezVcard, {Config, VcardData} from '../src/index';

(async () => {
  const data: VcardData = {
    name: {
      first: 'Maxim',
      last: 'Mazurok',
      full: 'Maxim Mazurok',
    },
    job: {
      title: 'Software Engineer',
      org: 'WiseTech Global',
    },
    online: {
      links: [
        'https://maxim.mazurok.com',
        'https://www.linkedin.com/in/maximmazurok',
      ],
      email: '[email protected]',
    },
    geo: {
      timezone: 'Australia/NSW',
      coordinates: {
        lat: -33.9242491,
        lon: 151.1918779,
      },
      addresses: [
        {
          street: '28 Ebsworth Street',
          city: 'Sydney',
          region: 'NSW',
          postCode: '2017',
          country: 'Australia',
          types: ['HOME'],
        },
      ],
    },
    phoneNumbers: [
      {
        number: '+61402282326',
        type: 'CELL',
      },
      {
        number: '+380984877707',
        type: 'CELL',
      },
    ],
    misc: {
      note: 'Professional web developer',
      birthday: {
        year: 1997,
        month: 'FEBRUARY',
        day: 14,
      },
      uid: '9aff8498-14c4-43ab-9ac6-40c2953c33ff',
      photo: {
        path: path.join(__dirname, 'contact_picture.jpg'),
        type: 'JPEG',
      },
      vcardUrl: 'https://maxim.mazurok.com/vcard.vcf',
      setRevision: true,
    },
  };

  const config: Config = {
    version: 'V2_1', // "V2_1" | "V3_0" | "V4_0"
    prodId: false, // remove `X-PRODID:ez-vcard 0.10.6`
  };

  const vcard = await ezVcard(data, config);

  console.log(vcard);
})();

Also, see readme and example

Upvotes: 1

Linh Duong
Linh Duong

Reputation: 169

var vCard = (function () {
    var start = "BEGIN:VCARD\nVERSION:3.0";
    var end = "END:VCARD";
    var data = "";

    var init = function() {
        data = "";
    };

    var name = function (surname, lastname) {
        data += "N:" + lastname + ';' + surname;
        data += "\n";
    };

    var cell = function (cell) {
        data += "TEL;TYPE=CELL:" + cell;
        data += "\n";
    };

    var work = function (work) {
        data += "TEL;TYPE=WORK,VOICE:" + work;
        data += "\n";
     };

    var other = function (work) {
        data += "TEL;TYPE=OTHER:" + work;
        data += "\n";
    };

    var email = function (email) {
        data += "EMAIL;TYPE=PREF,INTERNET:" + email;
        data += "\n";
    };

    var get = function () {
        return start + '\n' + data + end;
    };

    return {
        init:init,
        name:name,
        cell:cell,
        work:work,
        other:other,
        email:email,
        get:get
    }
});

Upvotes: 6

Related Questions