Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
How to convert local pdf to base64?

I have a local pdf file that I want to receive and convert to base64

import file from 'assets/PDFs/file.pdf';

const getBase64 = async (file) => new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
});

const fileURI = new File([file],  'file')

const base64Pdf = await getBase64(fileURI);

Finnaly, instead of a base64 file, I got a text file which contain a path to file assets/PDFs/file.pdf

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For node environment:

import fs from 'fs'

try {
     const data = fs.readFileSync('assets/PDFs/file.pdf', 'utf8')
     const buff = new Buffer.from(data)
     const base64pdf = 'data:application/pdf;base64,' + buff.toString('base64')
     console.log(base64pdf)
} catch (err) {
     console.error(err)
}

For browser environment:

const getBase64 = async(file) => new Promise((resolve, reject) => {
  const reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = () => resolve(reader.result);
  reader.onerror = error => reject(error);
});

const file = document.querySelector("input").onchange = async(e) => {
  let b64Data = await getBase64(e.target.files[0]);
  console.log(b64Data)
}
<input type="file" accept=".pdf" />

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!