-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: cleanup & add PDF QR code extractor example
- Loading branch information
Showing
8 changed files
with
118 additions
and
2,120 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>PDF QR code extract</title> | ||
</head> | ||
<body> | ||
<h1>PDF QR code extract</h1> | ||
<div id="app"></div> | ||
|
||
<script type="module" src="index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { decode } from "https://esm.sh/bysquare@latest"; | ||
import jsQR from "https://esm.sh/jsqr@latest"; | ||
import { | ||
getDocument, | ||
GlobalWorkerOptions, | ||
} from "https://esm.sh/pdfjs-dist@latest"; | ||
|
||
GlobalWorkerOptions.workerSrc = "https://esm.sh/pdfjs-dist@latest/build/pdf.worker.js"; | ||
|
||
class PDFQRExtractor { | ||
constructor(element) { | ||
this.appElement = element; | ||
this._createUI(); | ||
this.fileInput.addEventListener("change", this._handleFileChange); | ||
} | ||
|
||
_createUI() { | ||
this.fileInput = document.createElement("input"); | ||
this.fileInput.type = "file"; | ||
this.fileInput.id = "fileInput"; | ||
this.fileInput.accept = "application/pdf"; | ||
|
||
this.outputElement = document.createElement("div"); | ||
this.outputElement.id = "output"; | ||
|
||
this.appElement.appendChild(this.fileInput); | ||
this.appElement.appendChild(this.outputElement); | ||
} | ||
|
||
_handleFileChange = async (event) => { | ||
const file = event.target.files[0]; | ||
|
||
if (!file) { | ||
this._renderOutputData({ error: "No file selected." }); | ||
return; | ||
} | ||
|
||
const pdfBuffer = await this._readFileToArrayBuffer(file); | ||
const pdfDocument = await getDocument(pdfBuffer).promise; | ||
const page = await pdfDocument.getPage(1); | ||
|
||
const viewport = page.getViewport({ scale: 1.5 }); | ||
const canvas = document.createElement("canvas"); | ||
const context = canvas.getContext("2d"); | ||
|
||
canvas.width = viewport.width; | ||
canvas.height = viewport.height; | ||
|
||
await page.render({ | ||
canvasContext: context, | ||
viewport: viewport, | ||
}).promise; | ||
|
||
const imageData = context.getImageData(0, 0, canvas.width, canvas.height); | ||
const qrCode = jsQR(imageData.data, canvas.width, canvas.height); | ||
|
||
if (!qrCode) { | ||
this._renderOutputData({ error: "No QR Code found." }); | ||
return; | ||
} | ||
|
||
console.log("QR Code data:", qrCode.data); | ||
|
||
const parsedData = decode(qrCode.data); | ||
console.log("Parsed data:", parsedData); | ||
|
||
this._renderOutputData(parsedData); | ||
}; | ||
|
||
_readFileToArrayBuffer(file) { | ||
return new Promise((resolve, reject) => { | ||
const fileReader = new FileReader(); | ||
fileReader.onload = () => resolve(new Uint8Array(fileReader.result)); | ||
fileReader.onerror = reject; | ||
fileReader.readAsArrayBuffer(file); | ||
}); | ||
} | ||
|
||
_renderOutputData(data) { | ||
this.outputElement.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`; | ||
} | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
const appElement = document.querySelector("#app"); | ||
new PDFQRExtractor(appElement); | ||
}); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.