You are here:

Javascript/base64 encoded string from pdf


Hi Dear Kevin,

previously i asked this question and got this answer,

"Hello! The expert can't answer your question.

Your Question was:

I need to encode a PDF file to Base64 with Javascript. I can create Base64-encoded jpeg or png images in Javascript, but I could not find any way or sample code to create a Base64-encoded string from a PDF file.

Is it there any solution using a HTML5 canvas or javascript function?

Following is the reason:


Thanks for your question. JavaScript is a client-side language and does not have access to the file system, other than cookies and to a certain extent, some of the new data APIs.

That said, there are some projects out there that address this topic that you can look into. Here are a few links:

I hope this was helpful. Please feel free to follow-up if there are any other questions you may  have.

I think you misunderstand me,
In my case i want to base64 encode a file which is also located in client side in the same root with the javascript code.

i make base64 encoding of jpeg and png images located in the client side with this code;
  function encodeImage(src, callback) {
      var canvas = document.createElement('canvas'),
         ctx = canvas.getContext('2d'),
       img = new Image();
        img.onload = function(){
        canvas.width  = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0, img.width, img.height);
        img.src = src;;
  function() {
     encodeImage('1.png', function(encodedImage) {
         document.getElementById('data').value = encodedImage;

Both the image and my code is in the client side. And i can make the base64 encoding in this case works like a charm.

What i want to know is to be more clear is to convert a pdf file to a string. Then i can base64 encode that string also.

The point that i couldnt solve is:
The code to get the string (which starts as JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvY..............) is what i need.


Thanks for your question. I'm still not 100% what you are trying to do. Hopefully, this is what you are looking for:


JS Fiddle example:

(works in Chrome)

Best Wishes,

Kevin Chisholm


All Answers

Ask Experts


Kevin Chisholm


I can answer questions about: JavaScript, Object-Oriented JavaScipt, JSON, AJAX, jQuery, HTML, CSS


I am a Front-End Web Developer living in New York City. I have provided services to titles such as,,,,,, and Being particularly passionate about Object-Oriented JavaScript as well as Web Standards, I spend a fair amount of time writing about / teaching Front-End Web Development techniques. In my free time I enjoy reading about advanced JavaScript, as well as Web Development trends and best practices. My Blog is: JavaScript-specific blog posts can be found here:

NYC Front-end Developers CSS3 / HTML5 The future of front end Front-End Engineers Javascript Syracuse University Alumni Network

Syracuse University - VPA - 1983 - 1987

Past/Present Clients

©2017 All rights reserved.