How to use with Base64string PDF data - javascript

I have base64 string of the pdf file(Gmail PDF attachments), i am getting the base64 encoded from the server directly,now i need to convert this base64 to image to display the pdf in next tab of the browser.
I'm using the below code,
var pdfAsDataUri = "data:image/png;base64,"+file;
window.open(pdfAsDataUri);
* the new tab is open and is not displaying the pdf
Can you sugest me, how to convert pdf base64 encoded string to image[i'm getting base64 encoded string directly from server]
Note: Please find the below image, if i click on that green marked icon, the pdf should open in new tab.enter image description here

You can try making an Image object. Then in the src put the base64.
var myImage = new Image();
myImage.src="data:image/png;base64,"+file;

<embed src="data:application/pdf;base64,BASE_64_STRING" type="application/pdf" width="100%" height="600px" />

why you dont use iframe
<iframe id="framTest" height="100%" width="100%" />
$('#framTest').prop("src", your base64 string);

Related

Get Base64 image data from image loaded on page

Is it possible in Javascript/JQuery to get the base64 image data from an image on the page?
For example is there a function like...
<img id="foo" src="https://mywebsite.com/bar.png">
<script>
var base64Data = $('#foo').base64();
</script>

Cannot save data:image/jpeg;base64

I have this code that displays a color converted image for a customer on the web page, the customer can then click a download button to download the image,
It works fine on a PC but if i try to download the image on a android phone i get the error "Can only download http or https urls"
im guessing its because the saved image has a path like "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD//gA8Q1JFQVRPU"
how do i get this working? does it need to be converted or something?
here is my code:
imagejpeg($gd,NULL,100);
$outputBuffer = ob_get_clean();
$base64 = base64_encode($outputBuffer);
echo '<a id="downloadimage" style="text-decoration:none;" download>
<img id="image2" style="border:1px solid lightgrey;" width="300" height="400" src="data:image/jpeg;base64,'.$base64.'" />
<li style="padding-top:10px;textalign:center;display:block;border-radius:10px;background-color:royaleblue;height:30px;width:100px;background-color: lightgray;
padding-left: 30px;margin-left: 80px;">download</li></a>';
Well base64 encoded data represents the actual content of image in binary format and cannot be used with img tag. You need to either give the actual path where it is stored locally in the phone as in "../../file.jpg" or in the cloud as in "http://www.google.com/happy.png."

How To Generate Pdf Stream, Or Send Post Data To Pdf Url and generate it as an html5 object?

I'm trying to generate PDF in a div tag like this.
<div id="pdf">
<object width="400" height="500" type="application/pdf" data="pdftext.pdf" id="pdf_content"></object>
</div>
I am still unable to accomplish this due to my requirements.
request using GET values must not be used.
IFrame must not be used.
no physical file is generated. Only strings.
I tried rendering base64 pdf byte stream using HTML 5 object but still no luck.
<div id = 'pdfHere'>
<object src="data:application/pdf;base64,JVBERi0xLjQKJeLjz9MKNSAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDIzNzU+PnN0cmVhbQp4nL1cXXPauAJ951fo7XZnKGtJNrb3KUmzTT82bTZhOrNz9z4oWIBbY+faZjvkX/cfrCQISQAdUyNnyhCnsX2ODjo6+jL/752NenxIIm9IRknv91Hvzx4jH/T/UuKpf/o98hkZzXu/vqWEemQ06b36ZfRVn/t4ikfG86cXhYxElA/CcHUhI9TXF3rmz+W09+qvIk8W5O9XIzme5UVWTJfkUiSSvFvMRf73LxrAI9M9IP/9n/qZGJIAXr3ieANPyfA5eljPyNusKEpyJfKiFHNBzhZplqT5lHBf/fGmLqWsybgoc1mSP0QuyWmfnBV5OhHjtCAXWXErMvImrZckX2RZ/4HwMwZswIxyj8BXIhO5qPrkapZm6d1dmsuKUEq5gwJHzF5gzmhAmX4ZuubNAWTo2SE3OIeD2XHbXPOEa+CpjyImYRBu+PJtvueiluRCqs9bHSTkN1Ufl8TvE+bRoI1USpqAxT7Z/qmwlIvCwCNxQF7TgJSyN3lKlrMBj0jI4wHjD9pGz7n+Pr/LiqWU5FMxsHOj5Lv6y4f1ddcXD2TjQWDw1RlesD7OejfW81d01Ylzc8h9dYjON/fnweP9OT5fnfUEIHiC0FLdVZHi4V55KeMD3z9UXzGXPy8w9fyNxKr6HCSxTYIGDCPzGqNBZnXWBsMcHyfz+nY2mZm6O+dY5tMkKWVV/bzAzPMeBY7DAwS2F74BYyXwCqNJ4Dh8xNDHRwq8up1NYM6DpkosUtDu2ksx3GjLY36IttZyN2AYbdcYDdqqszYY5vg4bde3s2nrM29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pdf" width="100%" class="internal">
<param name="view" value="fitH" />
</object>
</div>
I have tried using plugins like pdf.js or pdfObject but this plugins won't allow me to pass POST values to the url (as far as I know).
PHP accomplished my task by doing this.
I use header('Content-type: application/pdf') and rendered string like this %PDF-1.4 %âãÏÓ 5 0 obj <>stream xœÅ\ÛrÛ¶}÷Wàí¤3 but they wanted it inside a div tag (not iframe).
Is there any way javascript can render byte stream or plugins which I can post values to URL and render the return and put it inside a div or generate an HTML 5 pdf object?
tried to pass the php file name that genrates the pdf like <object width="400" height="500" type="application/pdf" data="pdfgenrator.php" id="pdf_content"></object>
And to pass data to pdfgenrator.php you have to do it before the whole page load else it will not work

Angularjs download image and display

I'm trying to download an image with Angularjs and then save it to mongodb and display it in the future.
First I just try to download image binary and display it and it doesn't work.
$scope.downloadProfileImage = function()
{
//Getting the user that logged in through facebook
$authentication.getUserInfoWithoutLocation(function(respone)
{
//getting the url for the profile picture from facebook
FB.api('/'+respone.id+'/picture',function(respone)
{
//the response hold the url of the profile picutre..trying to download it
$http.get(respone.data.url).success(function(success)
{
//success is the image binary,encoding it to base64 and bound to it
$scope.img = Base64.encode(success)
})
})
})
}
in the html I just have <img ng-src="{{img}}"/>.
this doesn't work.. why?
even if I remove the base64 encoding and bind $scope.img to success it still does now show the image..
please help me.
Thanks!.
In order to display an image encoded in base64 it has to be in following form:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
So you probably have to add data:image/png;base64, in front of your $scope.img. Of course it depends what your image format is (png, jpeg, gif).
Can you just use the URL in ngSrc?
FB.api('/'+respone.id+'/picture',function(respone)
{
$scope.imgSrc = respone.data.url;
});
View
<img ng-src="{{ imgSrc }}" />
First of all check whether the image is successfully encoded into base64 or not. I think so. Now before displaying it just add this "data:image/png;base64," to the base64 string.
Here is the sample
In angularjs file
$scope.picValue="data:image/png;base64,"+base64String;
In html page
<img ng-src="{{picValue}}" />

Get a local file from plugin to javascript

I have developped a brower plugin that Acquire a picture from a Scanner or a Camera and save the picture in the file system of the user.
The output is the filepath to the picture.
I want to preview the picture in the Broswer, using javascript...
How can I get the picture without user interaction ?
( part of a Web App only compatible with Google Chrome)
If you have the filepath returned by your browser plugin and you have identified the event when you have to display the image then you can call ShowImage(filepath) function on that event.
<script type="text/javascript">
function ShowImage(filePath) {
$("#preview").append("<img alt='img' src='" + filePath + "'");
}
</script>
Your HTML should contain the div:
<div id="preview"></div>
If you have the contents of the image already you can load them in directly, by base64 encoding it and providing an URL as follows:
<img src="data:image/png;base64,iVBORw0KGgoAAAANS..." />

Categories