Initializing the Web Document Scanner SDK
We provide free technical support for the implementation and testing of the Scanbot SDK. If you encounter technical issues with integrating the Scanbot SDK or need advice on choosing the appropriate framework or features, please send us an email.
To run the Scanbot SDK plugin functionality within your production app, you have to purchase and use a valid Scanbot SDK license. The SDK will run for one minute in trial mode without a license.
const myLicenseKey = "";
scanbotSDK = await ScanbotSDK.initialize({
licenseKey: myLicenseKey
});
Leave the license key empty to enter the trial mode of one minute.
To get a free 7-day trial license, please submit the trial license form.
The initializer also serves as the main entry point of the SDK,
meaning that the scanbotSDK object returned upon initialize
serves as your entry point to use any other SDK features.
Initialization location & timing
React
In your class-based client-side React project, you should initialize the SDK
in the componentDidMount lifecycle method of your App.tsx component:
override async componentDidMount(): Promise<void> {
const sdk = await ScanbotSDK.initialize({ licenseKey: myLicenseKey, enginePath: "see below" });
}
If you're using functional components, make use of the useEffect hook:
useEffect(() => {
(async () => {
const sdk = await ScanbotSDK.initialize({ licenseKey: myLicenseKey, enginePath: "see below" });
})();
}, []);
Angular
In your Angular project, you have the option to initialize Scanbot SDK in the ngOnInit
lifecycle method of your AppComponent:
async ngOnInit(): Promise<void> {
await ScanbotSDK.initialize({ licenseKey: myLicenseKey, enginePath: "see below" });
}
However, in keeping more with the Angular way of doing things, you could also create an injectable service:
@Injectable()
export class ScanbotSdkService {
public sdk: ScanbotSDK;
constructor() {
ScanbotSDK.initialize({ licenseKey: myLicenseKey, enginePath: "see below" }).then((result) => {
this.sdk = result;
});
}
}
Then, you can inject this service into any component you need to use the SDK via:
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"],
})
export class AppComponent {
SDK: ScanbotSdkService;
router: Router;
constructor(_router: Router, _sdk: ScanbotSdkService) {
this.router = _router;
this.SDK = _sdk;
}
...
}
Vanilla JavaScript
If you're using vanilla JavaScript, you first need to download the SDK files. This can be done as follows:
mkdir -p scanbot-web-sdk/
cd scanbot-web-sdk
npm pack scanbot-web-sdk
tar -xf scanbot-web-sdk-*.tgz --strip-components=2 package/bundle
rm scanbot-web-sdk-*.tgz
The SDK files are now in the scanbot-web-sdk directory.
You can add the SDK to your HTML page by adding the following script tag:
<script src="scanbot-web-sdk/ScanbotSDK.min.js"></script>
Then you can initialize the SDK in the window.onload function:
window.onload = async () => {
scanbotSDK = await ScanbotSDK.initialize({ licenseKey: myLicenseKey, enginePath: "see below" });
}
Vue.js
In Vue.js, you have the option to create globally available objects via its provide-inject API.
First, you initialize the SDK in your main.ts file and provide it to the app:
const app = createApp(App)
const scanbotSDK = ScanbotSDK.initialize({ licenseKey: myLicenseKey, engine: enginePath });
app.provide("scanbotSDK", scanbotSDK);
And then you can have it available in any component via the inject function:
import { inject, onMounted } from "vue";
const scanbotSDK: Promise<ScanbotSDK> = inject("scanbotSDK")!;
And then initialize it after the component is mounted:
import { inject, onMounted } from "vue";
onMounted(async () => {
const SDK = await scanbotSDK;
});
Note that it's also possible to await the Scanbot SDK injection immediately, but deferring the SDK initialization to after the component has been mounted generally leads to a better user experience.
Also note that it's possible to immediately await the Scanbot SDK initialization in
main.ts. However, that will increase the initial load time.
Next.js
Next.js is React-based, therefore initialization is similar to React, in the lifecycle methods componentDidMount or useEffect of your page.tsx, but with the caveat that you do not want the server bundle to contain the SDK,
as that will drastically increase initial page load time. As such, we import the SDK dynamically inline:
override async componentDidMount(): Promise<void> {
const reference = (await import('scanbot-web-sdk')).default;
const sdk = await reference.initialize({ licenseKey: myLicenseKey, enginePath: "see below" });
}
useEffect(() => {
(async () => {
const reference = (await import('scanbot-web-sdk')).default;
const sdk = await reference.initialize({ licenseKey: myLicenseKey, enginePath: "see below" });
});
}, []);
Svelte
Svelte is similar to Next.js in terms of that it's a server-side rendering (SSR) framework and similar to Vue.js in terms of its onMount API.
As such, you want to make sure the SDK is not bundled into the SSR bundle, as that will drastically increase the initial page load time.
This is why we import the SDK dynamically inline. After that,
initialize the SDK as you normally would, in your main +page.svelte>:
import { onMount } from 'svelte';
onMount(async () => {
const reference = (await import('scanbot-web-sdk')).default;
const sdk = await reference.initialize({ myLicenseKey: '' });
});
Engine flavor & location
The SDK needs additional JS and WASM files during runtime. These files will be loaded by the browser when the SDK is initialized.
During initialization, you need to specify the enginePath to point the browser to a location where it can download these files.
Assuming the files are available in the /wasm/ folder, you can initialize the SDK as follows:
scanbotSDK = await ScanbotSDK.initialize({
// ...
enginePath: "/wasm/"
});
The SDK WASM files come in three different flavors: complete, barcode-scanner, and document-scanner.
Note that the WASM file sizes for the complete flavor are larger than for the other flavors.
You should only use the complete flavor if the features you need are not entirely covered by the barcode-scanner or document-scanner flavors.
The following table shows the features covered by each flavor:
| Feature | complete | barcode-scanner | document-scanner |
|---|---|---|---|
| Barcode Scanner | ✅ | ✅ | ❌ |
| Document Scanner | ✅ | ❌ | ✅ |
| Image filters | ✅ | ❌ | ✅ |
| Document Quality Analyzer | ✅ | ❌ | ✅ |
| PDF generation | ✅ | ❌ | ✅ |
| TIFF generation | ✅ | ❌ | ❌ |
| Text Pattern Scanner | ✅ | ❌ | ❌ |
| MRZ Scanner | ✅ | ❌ |