0

I am trying to store a base-64 encoded pdf file into firestore, but the base-64 text is too long to store to the database is there a way to compress the text before storing?

I tried the main program at first this is what I got:

Uncaught (in promise) FirebaseError: Document 'projects/jeel-al-ebdaa/databases/(default)/documents/Teachers/testteacher' cannot be written because its size (1,377,477 bytes) exceeds the maximum allowed size of 1,048,576 bytes.

I know this means the text is too long. I can not find a way to compress the text in pure vanilla js

1 Answers1

2

yes, it can be possible using compressing and decompressing data algorithms, So many algorithms for that but I recement use DEFLATE algorithm for compressing data. and in JavaScript zlib library is used for compressing data, it's based on DEFLATE algorithm you can install that library using npm or yarn.

npm install zlib

after that you create code that compresseszlib.deflate() your pdf size ;

here it's example:-

const zlib = require('zlib');

const buffer = Buffer.from(base64String, 'base64');

zlib.deflate(buffer, (err, compressedBuffer) => {
 if (err) {
   console. Error(err);
   return;
 }

   const compressedBase64String = compressedBuffer.toString('base64');

    // Store the compressed base-64 string in Firestore
    firestore.collection('myCollection').doc('myDoc').set({
    compressedPdf: compressedBase64String
    });
  });

and for decompress for use zlib.inflate() example:-

const compressedBuffer = Buffer.from(compressedBase64String, 'base64');

// Decompress the buffer using the zlib library
zlib.inflate(compressedBuffer, (err, decompressedBuffer) => {
   if (err) {
    console. Error(err);
    return;
     }
  const decompressedBase64String = decompressedBuffer.toString('base64');

    });
Parthis
  • 359
  • 1
  • 10