DEV Community

jasuke-dev
jasuke-dev

Posted on

Import Data From CSV with pdf file Using JSON and Base 64 in Laravel

Recently I have a problem that required importing data in CSV that in every row of the data related to one pdf file. Here examples of data in csv

Examples data in csv file
The pdf file has name of data in column accno

The solution that I found is to convert csv data to JSON file along with converting pdf file to base 64 (This is done in order to be able to save the pdf file into JSON). Here the code to converting csv file to JSON with Python.

import csv
import base64
import json

with open('data_collection.csv', 'r') as datainput:
    reader = csv.reader(datainput, delimiter=';')

    list = []
    row = next(reader)

    for row in reader:
        with open('{}.pdf'.format(row[0]), "rb") as pdf_file:
            base64_byte = base64.b64encode(pdf_file.read())
            base64_string = base64_byte.decode('utf-8')

            dict = {
                'inventory_code' : row[0],
                'title' : row[1],
                'year' : row[2],
                'isbn' : row[3],
                'base64file' : base64_string
            }
            list.append(dict)

    jsonString = json.dumps(list)
    jsonFile = open("data.json", "w")
    jsonFile.write(jsonString)
    jsonFile.close()
Enter fullscreen mode Exit fullscreen mode

So after that I have data.json that contain data from csv + base 64 from pdf file

In Laravel Project

blade file

Just simple input file that send POST request

....
    <form method="POST" action="/import" enctype="multipart/form-data">
        @csrf
        <input type='file' name="file"/>
        <button type="submit">Import Data</button>
    </form>              
....
Enter fullscreen mode Exit fullscreen mode

Web.php

Define Route to handle POST Request in web.php

Route::post('/import', [importController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

ImportController

Make import Controller to handle JSON file and storing the logic to save data in the model

<?php

namespace App\Http\Controllers;

use App\Models\Book;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class importController extends Controller
{
    public function index(){
        $JSONfile = file_get_contents(request()->file('file'));
        $JSON_decode = json_decode($JSONfile);

        foreach ($JSON_decode as $row) 
        {
            //decode bs64 string to bytes
            $b64_pdf = utf8_decode($row->base64file);

            //randomize name 
            $RandName = Str::random(10).'.'.'pdf';
            $pathName = $RandName;

            //bring back pdf file from base 64 and save it in public directory
            Storage::disk('public')->put($RandName, base64_decode($b64_pdf, true));

            //save data to table
            Book::create([
                'accno' => empty($row->accno) ? NULL : $row->accno, 
                'title'    => empty($row->title) ? NULL : $row->title, 
                'year'    => empty($row->year) ? NULL : $row->year, 
                'isbn'    => empty($row->isbn) ? NULL : $row->isbn, 
                'path'    => $pathName, 

            ]);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Now you can import your data along with pdf file simultaneously, if you have another solution please tell me in comment section thanks

Top comments (0)