Skip to content

latifbaihaki/qr-business-card-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

QR Business Card Generator

πŸ“± A simple and powerful library for generating QR code business cards with vCard support, custom design options, and multiple export formats.

Perfect for networking events, digital business cards, and contact sharing. When someone scans your QR code, their phone automatically recognizes it as a contact card and prompts them to save your information.

✨ Features

  • vCard 3.0 Support - Standard format that works with all smartphones (iPhone, Android, etc.)
  • Custom Design - Add your logo, choose custom colors, and adjust QR code appearance
  • Multiple Export Formats - Download as PNG, SVG, or PDF
  • TypeScript Support - Full type definitions for better development experience
  • Works Everywhere - Use in Node.js or browser environments
  • Easy to Use - Simple API that gets you started in minutes

πŸ“¦ Installation

Installation via package managers (npm, pip, or others) may be added in the future. For now, please clone this repository and use it directly from the source code.

πŸš€ Quick Start

Here's a simple example to get you started:

import { QRBusinessCard } from 'qr-business-card-generator';

// Create a generator instance
const generator = new QRBusinessCard();

// Prepare your business card data
const myCard = {
  firstName: 'John',
  lastName: 'Doe',
  email: 'john@example.com',
  phone: '+1234567890',
  organization: 'Acme Inc',
  jobTitle: 'Software Developer',
  website: 'https://johndoe.com',
  social: {
    linkedin: 'linkedin.com/in/johndoe',
    github: 'github.com/johndoe'
  }
};

// Generate the QR code
const qrCode = await generator.generate(myCard, {
  size: 400
});

// The QR code is now a data URI (data:image/png;base64,...)
// You can use it directly in an <img> tag or export it

That's it! You now have a QR code that contains all your contact information in vCard format.

πŸ“– Detailed Usage Guide

Basic Usage

The most basic usage requires just a first name:

const generator = new QRBusinessCard();

const simpleCard = {
  firstName: 'Jane',
  email: 'jane@example.com'
};

const qrCode = await generator.generate(simpleCard);
// Use qrCode in your application

Complete Business Card Data

You can include as much or as little information as you want:

const completeCard = {
  // Required
  firstName: 'John',
  
  // Optional personal info
  lastName: 'Doe',
  jobTitle: 'Senior Developer',
  organization: 'Tech Corp',
  
  // Contact information
  email: 'john@example.com',
  phone: '+1234567890',      // Work phone
  mobile: '+0987654321',     // Mobile phone
  website: 'https://johndoe.com',
  
  // Address (optional)
  address: '123 Main Street',
  city: 'New York',
  state: 'NY',
  zipCode: '10001',
  country: 'USA',
  
  // Social media links
  social: {
    linkedin: 'linkedin.com/in/johndoe',
    github: 'github.com/johndoe',
    twitter: 'twitter.com/johndoe',
    instagram: 'instagram.com/johndoe',
    facebook: 'facebook.com/johndoe',
    youtube: 'youtube.com/@johndoe'
  },
  
  // Additional notes
  notes: 'Available for freelance projects'
};

const qrCode = await generator.generate(completeCard);

Customizing QR Code Appearance

Custom Colors

Make your QR code match your brand:

const qrCode = await generator.generate(myCard, {
  size: 400,
  customColors: {
    foreground: '#FF5733',  // QR code color (orange)
    background: '#FFFFFF'    // Background color (white)
  }
});

Adding a Logo

Add your company logo or profile picture to the center of the QR code:

const qrCode = await generator.generate(myCard, {
  size: 400,
  logo: {
    url: 'https://example.com/logo.png',  // URL or data URI
    size: 80,                              // Size in pixels
    margin: 10                             // Margin around logo
  },
  errorCorrectionLevel: 'H'  // High error correction (recommended with logo)
});

Important: When using a logo, always use errorCorrectionLevel: 'H' to ensure the QR code remains scannable even with the logo covering part of it.

Adjusting Size and Margins

const qrCode = await generator.generate(myCard, {
  size: 500,           // Larger QR code (default: 400)
  margin: 2,           // Smaller margin (default: 4)
  errorCorrectionLevel: 'M'  // Lower error correction (faster scanning, less data capacity)
});

Export Options

Export as PNG (Browser Only)

// Generate the QR code
const qrCode = await generator.generate(myCard);

// Download as PNG file
generator.exportAsPNG(qrCode, 'my-business-card.png');

Export as PDF (Browser Only)

const qrCode = await generator.generate(myCard);

// Download as PDF file
await generator.exportAsPDF(qrCode, 'my-business-card.pdf');

Export as SVG

// Generate SVG directly
const svgString = await generator.generateSVG(myCard, {
  size: 400,
  customColors: {
    foreground: '#000000',
    background: '#FFFFFF'
  }
});

// Export SVG file (browser only)
generator.exportAsSVG(svgString, 'my-business-card.svg');

Note: Export functions only work in browser environments. In Node.js, you'll get the data URI or SVG string, which you can then save using Node.js file system operations.

Getting vCard String

If you just need the vCard string without generating a QR code:

const vCardString = generator.generateVCard(myCard);
console.log(vCardString);
// Output:
// BEGIN:VCARD
// VERSION:3.0
// N:Doe;John;;;
// FN:John Doe
// ...

πŸ’» Usage in Different Environments

Node.js

const { QRBusinessCard } = require('qr-business-card-generator');

const generator = new QRBusinessCard();
const data = {
  firstName: 'John',
  email: 'john@example.com',
  phone: '+1234567890'
};

const qrCode = await generator.generate(data);
// qrCode is a data URI string
// You can save it using fs.writeFile or use it in other ways

React

import React, { useState } from 'react';
import { QRBusinessCard, BusinessCardData } from 'qr-business-card-generator';

function BusinessCardQR() {
  const [qrCode, setQrCode] = useState<string | null>(null);
  const generator = new QRBusinessCard();

  const cardData: BusinessCardData = {
    firstName: 'Jane',
    lastName: 'Smith',
    email: 'jane@example.com',
    phone: '+1234567890',
    organization: 'Tech Startup',
    jobTitle: 'Frontend Developer'
  };

  const handleGenerate = async () => {
    const qr = await generator.generate(cardData, {
      size: 400,
      logo: {
        url: '/logo.png',
        size: 80
      }
    });
    setQrCode(qr);
  };

  const handleDownload = () => {
    if (qrCode) {
      generator.exportAsPNG(qrCode, 'my-card.png');
    }
  };

  return (
    <div>
      <button onClick={handleGenerate}>Generate QR Code</button>
      {qrCode && (
        <>
          <img src={qrCode} alt="Business Card QR Code" />
          <button onClick={handleDownload}>Download PNG</button>
        </>
      )}
    </div>
  );
}

Vanilla JavaScript (Browser)

<!DOCTYPE html>
<html>
<head>
  <title>QR Business Card</title>
</head>
<body>
  <div id="qr-container"></div>
  
  <script type="module">
    import { QRBusinessCard } from './node_modules/qr-business-card-generator/dist/esm/index.js';
    
    const generator = new QRBusinessCard();
    
    const data = {
      firstName: 'John',
      email: 'john@example.com',
      phone: '+1234567890'
    };
    
    generator.generate(data, { size: 400 })
      .then(qrCode => {
        const img = document.createElement('img');
        img.src = qrCode;
        document.getElementById('qr-container').appendChild(img);
      });
  </script>
</body>
</html>

Note: For browser usage, you may need to bundle the library with a bundler like webpack, rollup, or esbuild since the library uses Node.js dependencies.

🎨 Advanced Customization

Error Correction Levels

QR codes have different error correction levels that determine how much of the code can be damaged while still remaining scannable:

  • L (Low) - ~7% error correction, smallest QR code
  • M (Medium) - ~15% error correction (default)
  • Q (Quartile) - ~25% error correction
  • H (High) - ~30% error correction, largest QR code (recommended when using logos)
const qrCode = await generator.generate(data, {
  errorCorrectionLevel: 'H'  // Use high error correction with logos
});

Logo Best Practices

  1. Size: Keep logo size between 15-25% of QR code size
  2. Error Correction: Always use 'H' (high) when adding logos
  3. Format: Use PNG or JPG images with transparent or white backgrounds
  4. Accessibility: Ensure logo URL is accessible (CORS enabled for external URLs)
const qrCode = await generator.generate(data, {
  size: 400,
  logo: {
    url: 'https://example.com/logo.png',  // Must be accessible
    size: 80,                              // 20% of 400px
    margin: 10
  },
  errorCorrectionLevel: 'H'  // Essential for logo
});

πŸ“± How vCard Works

When someone scans your QR code:

  1. Recognition: Their phone recognizes the vCard format
  2. Prompt: They get a prompt asking if they want to save the contact
  3. Save: All your information is automatically added to their contacts

The vCard format is a standard that works with:

  • βœ… iPhone Contacts
  • βœ… Android Contacts
  • βœ… Google Contacts
  • βœ… Outlook
  • βœ… Most contact management systems

πŸ”§ API Reference

QRBusinessCard Class

Constructor

const generator = new QRBusinessCard();

Methods

generate(data, options?)

Generates a QR code from business card data.

Parameters:

  • data: BusinessCardData - Your business card information
  • options?: QRCodeOptions - Optional customization options

Returns: Promise<string> - QR code as data URI (PNG) or SVG string

Example:

const qrCode = await generator.generate(data, {
  size: 400,
  customColors: { foreground: '#000000', background: '#FFFFFF' }
});
generateVCard(data)

Generates vCard format string without creating a QR code.

Parameters:

  • data: BusinessCardData - Business card information

Returns: string - vCard 3.0 format string

generatePNG(data, options?)

Convenience method to generate QR code as PNG format.

Returns: Promise<string> - PNG data URI

generateSVG(data, options?)

Generates QR code as SVG format.

Returns: Promise<string> - SVG string

exportAsPNG(dataUri, filename?)

Downloads QR code as PNG file (browser only).

Parameters:

  • dataUri: string - QR code data URI
  • filename?: string - Optional filename (default: 'qr-business-card.png')
exportAsSVG(svgString, filename?)

Downloads QR code as SVG file (browser only).

exportAsPDF(dataUri, filename?)

Downloads QR code as PDF file (browser only).

Returns: Promise<void>

Type Definitions

BusinessCardData

interface BusinessCardData {
  firstName: string;           // Required
  lastName?: string;
  organization?: string;
  jobTitle?: string;
  email?: string;
  phone?: string;
  mobile?: string;
  website?: string;
  address?: string;
  city?: string;
  state?: string;
  zipCode?: string;
  country?: string;
  social?: {
    linkedin?: string;
    twitter?: string;
    instagram?: string;
    github?: string;
    facebook?: string;
    youtube?: string;
  };
  notes?: string;
}

QRCodeOptions

interface QRCodeOptions {
  size?: number;                    // QR code size in pixels (default: 400)
  margin?: number;                  // Margin in modules (default: 4)
  errorCorrectionLevel?: 'L' | 'M' | 'Q' | 'H'; // Error correction (default: 'H')
  customColors?: {
    foreground?: string;            // QR code color (default: '#000000')
    background?: string;            // Background color (default: '#FFFFFF')
  };
  logo?: {
    url: string;                    // Logo image URL or data URI
    size?: number;                  // Logo size in pixels (default: 20% of QR size)
    margin?: number;                // Logo margin (default: 10)
  };
  type?: 'image/png' | 'image/svg+xml'; // Output format (default: 'image/png')
}

πŸ› Troubleshooting

QR Code Not Scannable

  • Size too small: Use at least 200px, preferably 400px or larger
  • Low error correction: Use 'H' (high) error correction level
  • Logo too large: Keep logo under 25% of QR code size
  • Poor contrast: Ensure good contrast between foreground and background colors

Logo Not Showing

  • CORS issues: Make sure the logo URL is accessible and CORS-enabled
  • Invalid URL: Check that the logo URL is correct and the image exists
  • Format issues: Use PNG or JPG formats

Export Functions Not Working

  • Browser only: Export functions only work in browser environments, not Node.js
  • Generate first: Make sure you've generated a QR code before trying to export

Module Resolution Errors (Browser)

If you see errors about modules not being found in the browser:

  • Use a bundler: The library needs to be bundled with webpack, rollup, or esbuild for browser use
  • Check examples: See the examples/ folder for working browser examples

πŸ“š Examples

Check out the examples/ folder for complete working examples:

  • basic-usage/ - Simple HTML/JavaScript example
  • react-example/ - React component example

🀝 Contributing

Contributions are welcome! Feel free to:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

πŸ“ License

MIT License - feel free to use this library in personal or commercial projects.

πŸ™ Acknowledgments

Built with:


Made with ❀️ for the developer community. Happy coding!

About

πŸ“± Generate QR code business cards with vCard support, custom design, and multiple export formats. Perfect for networking and digital contact sharing.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors