How to scan a document and save it to Database in angular 7

3.9k Views Asked by At

I am using angular 7 and I want to scan a document and save it to database table.

I tried with ngx-document-scanner and Dynamic Web TWAIN from below links but it is not giving option to save it to database

https://www.npmjs.com/package/ngx-document-scanner https://blog.dynamsoft.com/imaging/using-dynamic-web-twain-angular-application/

Is there any other way to do this?

Thanks

1

There are 1 best solutions below

0
On

Dynamic Web TWAIN is capable of interacting with physical document scanners via protocols including TWAIN | ICA | SANE on Windows | macOS | Linux. Have you got the document scanning part working in an angular application?

Once pages are scanned, the images will be transferred to the Dynamic Web TWAIN library and stored in memory or cached on the local disk. Then you can call a few APIs to encode the data into 1 of 5 formats (bmp, jpg, png, tif, pdf) and send the encoded data (file) in an HTTP Post request. After that, the data leaves the application and is no longer relevant to Angular. The mentioned API specifies a URL as the receiver for that HTTP request. Once the data reaches that URL, it's extracted and can then be saved into a database. So the question would be: 1 what format do you want the scanned images to be saved into? 2 what database and server-side language do you use?

For a simple demonstration, I'm pasting below the code snippet for that URL written in PHP for mySQL

<?php   

    // Interacting with MySQL
    $servername = "127.0.0.1";
    $username = "root";
    $password = "root";
    $dbname = "dwtsample";
    $tablename = "uploadedimages";

    // Create connection
    $conn = new mysqli($servername, $username, $password);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else {
        // Check Database Existance
        $db_selected = mysqli_select_db($conn, $dbname);
        if(!$db_selected) {
            // Create database
            $sql_newDB = "CREATE DATABASE ".$dbname;
            if ($conn->query($sql_newDB) === TRUE) {
                // echo "Database created successfully";
            } else {
                die("Error creating database: " . $conn->error);
            }           
        }
        mysqli_select_db($conn, $dbname);

        // Check Table Existance
        $sql_showtable = "SHOW TABLES LIKE '".$tablename."'";
        $rowcount = mysqli_num_rows($conn->query($sql_showtable));
        if ($rowcount > 0) {
            // echo "the table exists";
        } else {
            // sql to create table
            $sql_newtable = "CREATE TABLE ".$tablename." (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
            document_name VARCHAR(30) NOT NULL,
            document_data longblob NOT NULL,
            reg_date TIMESTAMP
            )";
            if ($conn->query($sql_newtable) === TRUE) {
                // echo "Table ".$tablename." created successfully";
            } else {
                die("Error creating table: " . $conn->error);
            }
        }           

            $fileTempName = $_FILES['RemoteFile']['tmp_name'];  
            $fileSize = $_FILES['RemoteFile']['size'];
            $fileName = $_FILES['RemoteFile']['name'];
            $strFileSize = (string)intval($fileSize/1024)."KB";
            $fReadHandle = fopen($fileTempName, 'rb');
            $fileContent = fread($fReadHandle, $fileSize);
            fclose($fReadHandle);
            $imgIndex = 0;
            $sql_insertdata = "INSERT INTO ".$tablename." (document_name,document_data) VALUES ('".$fileName."','".addslashes($fileContent)."')";
            if ($conn->query($sql_insertdata) === TRUE) {
                // echo "File saved in db successfully.";
                $sql_getIndex = "SELECT id FROM ".$tablename;
                $IDs = $conn->query($sql_getIndex);
                if ($IDs->num_rows > 0) {
                    // output data of each row
                    while($row = $IDs->fetch_assoc()) {
                        $_temp = intval($row["id"]);
                        if($_temp > $imgIndex)
                            $imgIndex = $_temp;
                    }
                }
            } else {
                die("Error saving file: " . $conn->error);
            }
            $conn->close();
            echo "PHP:"."DWTUploadFileIndex:".strval($imgIndex)."DWTUploadFileName:".$fileName."UploadedFileSize:".$strFileSize;

    }
?>