0

i would like to create a function which converts an image from a website to pdf and serves it as download by pressing a button. The image is created by a python cgi script depending on users input. The address of the image is a base64 encoded string which will change depending on users request. E.g:

<img src='data:image/png;base64,iVBORw0KGgoAAAAN....='/>

I have found this piece of code.

var imgData = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD...';
var doc = new jsPDF();

doc.addImage(imgData, 'JPEG', 15, 40, 180, 180);

In this example you have to know the string already. Is there a way to make java script read out the image address and save it in variable like above?

Thanks for your help.

Dukaaza
  • 47
  • 1
  • 8
  • You can use the [FileReader](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) object to read in the image as a [dataURL](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL) – James Newton May 28 '15 at 15:46
  • [This answer to a similar question](http://stackoverflow.com/a/5420409/1927589) might be what you are looking for. – James Newton May 28 '15 at 15:55

1 Answers1

1

You can get the src of the image this way:

<img id="new_img" src="data:image/png;base64,iVBORw0KGgoAAAAN....="/>

var imgData = document.getElementById("new_img").src;

Or you can use jQuery like this:

var imgData = $('#new_img').attr('src');