All files / src/utils/parses pdf-parse-1.ts

6.25% Statements 3/48
0% Branches 0/12
0% Functions 0/3
4.35% Lines 2/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89  1x     1x                                                                                                                                                                        
import { AnyObject } from '@loopback/repository';
import { StringUtils } from '..';
import { AwsS3Service } from '@logchain/components/aws';
 
export const pdfParseById = async (payload: AnyObject, s3_service: AwsS3Service) => {
    try {
      const pdf = require('pdf-parse');
      const res = await s3_service.getBufferObjectFromS3(payload.s3_key);
 
      return pdf(res?.body).then(async (data: { text: string; }) => {
        const extractedText0 = StringUtils.extractTextsByLineBreak(data.text, 'TRANSPORT DOCUMENT NUMBER', 'PAGE');
        const document_number = extractedText0[1];
        const shipper = extractedText0[2];
        const consignee = StringUtils.extractTextsByLineBreak(data.text, 'CONSIGNEE:', 'ADDITIONAL HANDLING INFORMATION')[1];
        const extractedText1 = StringUtils.extractTextsByLineBreak(
          data.text,
          'V E S S E L   N o .   A N D   D A T E P O R T     O F     L O A D I N G',
          'MARKS & NOS.'
        );
        const vessel_no_and_date = extractedText1[2] + ' ' + extractedText1[3];
        let port_of_loading = '';
        let port_of_discharge = '';
        let destination = '';
        if (extractedText1.length >= 11) {
          port_of_loading = extractedText1[6].trim();
          port_of_discharge = extractedText1[8].trim() + ' ' + extractedText1[9].trim();
          destination = extractedText1[10].trim();
        } else {
          port_of_loading = extractedText1[4].trim();
          const commasSplit = extractedText1[6].split(',');
          const spaceSplit = commasSplit[1].trim().split(' ');
          port_of_discharge = commasSplit[0].trim() + ', ' + spaceSplit[0].trim();
          destination = commasSplit[2].trim() + ', ' + spaceSplit[1].trim();
        }
 
        // Container No, Seal number, Gross weight, Net weight
        const extractedText2 = StringUtils.extractTextsByLineBreak(
          data.text,
          'QUANTITY',
          'CONTAINER IDENTIFICATION No.'
        );
        // console.log(data.text.split('\n'));
        const containers = [];
        const seals = [];
        const grossWeights = [];
        for (let index = 0; index < extractedText2.length; index++) {
          const line = extractedText2[index];
          if (line.includes('Seals:')) {
            containers.push(extractedText2[index - 1]);
            if (extractedText1.length >= 11) {
              seals.push(line.replace('Seals:', '').trim());
            } else {
              const line2 = extractedText2[index+1];
              seals.push(line.replace('Seals:', '').trim() + ' ' + line2);
            }
          }
          if (line.includes('EMERGENCY RESPONSE NUMBER:')) {
            let grossWeightIndex = index + 2;
            // eslint-disable-next-line @typescript-eslint/no-unused-vars
            for (const _seal of seals) {
              grossWeights.push(extractedText2[grossWeightIndex]);
              grossWeightIndex += 3;
            }
          }
        }
 
        return {
          document_number,
          shipper,
          consignee,
          vessel_no_and_date,
          port_of_loading,
          port_of_discharge,
          destination,
          container_number: containers,
          seal_number: seals,
          gross_weight: grossWeights,
        };
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
      }).catch((error: any) => {
        console.error(error);
        return false;
      });
    } catch (e) {
      console.error(e);
      return false;
    }
  }