You are here:

Javascript/base64 encoded string from pdf

Advertisement


Question
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:

Hi Onur YILMAZ,

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:

http://code.google.com/p/jspdf/

http://jspdf.com/

http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.p

http://bytescout.com/buy/pdfgeneratorsdkjs.html

http://bytescout.com/products/developer/pdfgeneratorsdkjs/create_pdf_invoice_jav

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);
        callback(canvas.toDataURL());
        }
        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.

Answer
Hi Onur YILMAZ,

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

data:application/pdf;base64,JVBERi0xLjMKMyAwIG9iago8PC9UeX.....(etc...)

JS Fiddle example: http://jsfiddle.net/NTjEw/

(works in Chrome)

Best Wishes,

Kevin Chisholm

http://kevinchisholm.com/

http://blog.kevinchisholm.com/

Javascript

All Answers


Ask Experts

Volunteer


Kevin Chisholm

Expertise

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

Experience

I am a Front-End Web Developer living in New York City. I have provided services to titles such as Time.com, TimeForKids.com, SportIllustrated.com, coastalliving.com, People.com, TDAmeritrade.com, and Instyle.com. 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: http://blog.kevinchisholm.com/ JavaScript-specific blog posts can be found here: http://blog.kevinchisholm.com/category/javascript/

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

Education/Credentials
Syracuse University - VPA - 1983 - 1987

Past/Present Clients
cookingchanneltv.com foodnetwork.com food.com hgtv.com diynetwork.com gactv.com frontdoor.com hgtvremodels.com travelchannel.com Time.com TimeForKids.com SportIllustrated.com coastalliving.com People.com TDAmeritrade.com ramirezco.com njsga.org soccercoliseum.com superdomesports.com afeenj.org

©2016 About.com. All rights reserved.