title: MySQL BLOB to PDF in nodejs
body: <p>I have few records with BLOBs im posting one record's screenshot below:
  <a href="https://i.stack.imgur.com/J9E3k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J9E3k.png" alt="enter image description here"></a></p>
  
  <p>i need to convert it back to pdf and save in a folder.
  here is what i have tried so far but nothing worked:</p>
  
  <p>simply saving blob to file(failed)</p>
  
  <pre><code>const mysql = require('mysql');
  const fs = require('fs');
  
  const con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "SQL1234",
    database: "mydb"
  });
  
  con.connect(function(err) {
    if (err) throw err;
  
    const sql = 'SELECT * FROM documents WHERE idnum = 29347';
  
    con.query(sql, function (err, result, fields) {
      if (err) throw err;
      const row = result[0];
      const pdf = row.pdf;
      fs.writeFileSync('some.pdf', pdf);
  
    });
  
  });
  </code></pre>
  
  <p>then converting it to string base64 and using package <a href="https://www.npmjs.com/package/base64topdf" rel="nofollow noreferrer">base64topdf</a></p>
  
  <pre><code>const base64 = require('base64topdf');
  var bufferBase64 = pdf.toString('base64');
  let decodedBase64 = base64.base64Decode(bufferBase64, 'finalpdf.pdf');
  </code></pre>
  
  <p>another failed attempt:</p>
  
  <pre><code>    let buffer = Buffer.from(row.pdf);
      fs.writeFile('file.pdf', buffer, 'binary', function(err) {
          if(err) {
              console.log(err);
          }else{
              console.log('file saved');
              fs.readFile('test.pdf', (err, data) =&gt; {
                  res.contentType("application/pdf");
                  res.send(data);//
              });
          }
      });
  </code></pre>
  
  <p>using <a href="https://www.npmjs.com/package/blob-to-buffer" rel="nofollow noreferrer">blob-to-buffer</a>:</p>
  
  <pre><code>const toBuffer = require('blob-to-buffer');
   var blob = b64toBlob(row.pdf, 'application/pdf');
  
   toBuffer(blob, function (err, buffer) {
     if (err) throw err
  
     buffer[0] // =&gt; 1
     buffer.readUInt8(1) // =&gt; 2
     let decodedBase64 = base64.base64Decode(buffer, 'myfile2.pdf');
   });
  </code></pre>
  
  <p>i know im doing something stupid thats y i have posted a screenshot of the blob so you know what kind of data i have.
  any help would be appreciated.</p>