Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Fallstop/react-pdf-image-qr-scanner

Repository files navigation

react-pdf-image-qr-scanner

It was made framework agnostic, and moved to a new organisation. The new version has massive breaking changes, but has much better scanning abilites.






This is a component to scan user uploaded PDF's and Images for QR-Codes

NPM

Demo

There is a demo available here. The source of the demo is in the /example folder.

Install

yarn add react-pdf-image-qr-scanner
npm install --save react-pdf-image-qr-scanner

Usage

import React from 'react';

import ScanCanvasQR from 'react-pdf-image-qr-scanner';

function App() {
	const canvasScannerRef = useRef();
	const [resultText, setResultText] = useState("");

	async function scanFile(selectedFile) {
		setResultText("");
		try {
			const qrCode = await canvasScannerRef.current.scanFile(selectedFile);
			// It returns null if no QR code is found
			setResultText(qrCode || "No QR code found");
		} catch (e) {
			// Example Error Handling
			if (e?.name === "InvalidPDFException") {
				setResultText("Invalid PDF");
			} else if (e instanceof Event) {
				setResultText("Invalid Image");
			} else {
				console.log(e);
				setResultText("Unknown error");
			}
		}
	}

	return (
		<div>
			<ScanCanvasQR ref={canvasScannerRef} />
			<input type="file" onChange={(e) => { scanFile(e.target.files[0]); }} />
		</div>
	);
}