Found the solution here it is, i was sending byte array from spring controller which is in the form like %PDF-1 %����. So i send base64 encoded string from spring controller and send the base64 encoded string to browser and it works.
javascript code :
var arrrayBuffer = base64ToArrayBuffer(data); //data is the base64 encoded string
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
var blob = new Blob([arrrayBuffer], {type: "application/pdf"});
var link = window.URL.createObjectURL(blob);
window.open(link,'', 'height=650,width=840');
convert byte array to base64 encoded string in spring controller
String encodedString = Base64.getEncoder().encodeToString(bytearrayofpdf);
Answer from user on Stack OverflowFound the solution here it is, i was sending byte array from spring controller which is in the form like %PDF-1 %����. So i send base64 encoded string from spring controller and send the base64 encoded string to browser and it works.
javascript code :
var arrrayBuffer = base64ToArrayBuffer(data); //data is the base64 encoded string
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
var blob = new Blob([arrrayBuffer], {type: "application/pdf"});
var link = window.URL.createObjectURL(blob);
window.open(link,'', 'height=650,width=840');
convert byte array to base64 encoded string in spring controller
String encodedString = Base64.getEncoder().encodeToString(bytearrayofpdf);
You can use PDFObject JavaScript utility to view PDF files in your browser. Just create a div with some id and insert the byte array that you get into that div.
PDFObject.embed(<byte array>, "#pdfObjectViewer");
You need to download the PDFObject library and include it in your project from their site for this to work. Or you can use this CDN.
javascript - display byte array as pdf in browser i tried several options from stack overflow but it did not work for me - Stack Overflow
Pass byte[] array to javascript and convert the byte array to PDF in javascript.
Display pdf as byte-array
Render byte array as pdf using Javascript inside iframe Internet Explorer/Edge - Stack Overflow
This works for me on Chrome, but not on IE:
// base64DataFromWebAPI comes from a Web API, in the form of base 64 string
var pdfData = base64DataFromWebAPI;
var x = window.open();
var iframe = x.document.createElement('iframe')
iframe.width = '100%'
iframe.height = '100%'
iframe.frameBorder = 0
iframe.style = "border: 0"
iframe.src = "data:application/pdf;base64, " + pdfData
x.document.body.appendChild(iframe);
I got it from the following Stack Oveflow question: JsPDF - Not allowed to navigate top frame to data URL
Using Chrome*, you can also simply paste the following code in the address bar:
data:application/pdf;base64, YOUR_PDF_DATA_HERE
It will open it in the included PDF viewer of the browser.
*didn't tested other browsers
Nope there is no interface that would allow you to this sort of thing in the web-side security sandbox. The reason you aren't just downloading the PDF from the server is?...
As I said in my question we already generate the PDF data on the server.
We need to have the byte data available on the client side to be passed via a JS call to a Java applet. We do this currently via a hidden field. That's okay and it works just fine.
The problem is that the call to the applet only takes place if the okay button on an ajax modal dilaog is clicked ('Do you want to send this document to UglyProprietorySystem?').
As you all know you can't put anything on top of an adobe plug-in display. Obviously if the whole page is of type PDF then there's no place for any JS to show the dialog so that wouldn't work and if you use an IFrame you still can't put anything on top of what the plug-in displays (it's effectively another app's airspace and violating that is just plain rude!).
We worked around this by:
- Inititally showing a blurred image of a generic PDF doc and popping the modal dialog on top of that.
- When the user had responded to the dialog we called (or not) the java app with the pdf byte data from the hiden field
- We then posted back after clearing the hidden field (Woo! Posting back a form with PDF binary data is not appreciated by the server!)
- In the page_load of the code behind we then, on postback, wrote to the response stream with he PDF byte data, setting the contentype to be appplication/pdf and thus the pdf is displayed.
Lovely. It all works and gets us kind of what we'd like.
Only problem is we either need to:
- generate the PDF data twice (once on initital page_load for the clientside call to our applet and then again on postback to display).
or
- We store the PDF byte data in the session after the first response so as to be able to, after the postback, still have the pdf data to be able to display it.
Neither is great but we went with the latter but now we're thinking we'd like to avoid that if we can.
Thus the idea of generating the PDF, putting it into a hidden field, having a button on the page to save the PDF to our proprietary system via an applet call and also writing to the IFrame with that data already on the client in the hidden field thus displaying it.
Phew!
So that's why we wanted to on the client using JS, write the pdf binary data to the IFrame and have it's content type set to application/pdf so the browser would load up the adobe plug-in and display it.
CodeSAndbox Demo (open the result of the codesandbox in a new tab for it to work)
Hey Hiten,
Apparently in mozilla official documents for PDF.js, They say that no need to convert to Byte array (uint8Array), you can parse the base64 code into pdf directly....
Just render the iframe with data:application/pdf;base64 before the raw base64 data like this and it will appear :)
<iframe
title="frame"
width="300px"
height="700px"
src={`data:application/pdf;base64,${this.base64PDF()}`} />
A good reference in another Stackoverflow Thread discussing rendering a pdf file using a base64 instead of url
I hope I have helped you
My main aim here was to render the pdf from my local filesystem in the electron app that I am creating.
My earlier approach was to pass on the raw base64 data of the pdf in an iframe with src as web/viewer.html of the pdfjs but due to some limitation in electron, this wasn't working.
So, I figured out an easy solution that while starting electron I ran a node server on the directory from where I wanted to serve the pdf files and used the URL in the form http://localhost/{port_number}/{path_to_my_pdf_files} to access the pdf in pdfjs
<iframe
src={`/web/viewer.html?file=http://localhost:${process.env.REACT_APP_PDF_SERVER_PORT}/${path}`}
title="iframe"
width="100%"
height="700px"
/>
I have a slight problem in a Blazor Server application when displaying PDF documents. I have tried to find a C# based solution for this but cannot find one anywhere.
The background to this is I am working on an application that is used in part to store and display PDF documents. For various reasons (which I won't go into as I have tried to change this many times but the arguments to not do it are actually quite well presented) these are not stored in a document format but within the database so are handled in the Blazor application as byte[] variables.
So for display to the user when they select the document we convert the byte[] into base64 and embed the resultant code into the pages displayed.
In the C#
string DisplayPDF; DisplayPDF = "data:application/pdf;base64," + Convert.ToBase64String(nc.DocData);
and in the HTML
<embed src="@DisplayPDF" type="application/pdf" width=1000 height=800 />
And to some extent this works fine, until we get over approx 2Mb in size for the document at which point we get a blank screen. I believe this is to do with the URL size displayed as over 2Mb the src value gets too long and most browsers won't display it.
Has anyone had any experience with this issue and know of a solution? I have found one using Java but this doesn't work for us. How do we handle large PDF files as the vast majority of the files in use are over the size limit set by this issue?
You would need to pass the responseType in your service call
$http.post('/Service-URL', dataTO, {responseType: 'arraybuffer'});
then in the success of your data call this should open up pdf in a new window:-
getDocument()
.success(function(data) {
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
})
From this answer :- https://stackoverflow.com/a/21730535/3645957 by https://stackoverflow.com/users/2688545/michael
If anyone still looks for that, here is what I'm doing (and working) :
var pdfAsDataUri = "data:application/pdf;base64,"+byteArray;
window.open(pdfAsDataUri);
Where byteArray is the data you receive. It's maybe not a nice solution (byte array is visible in the URL), but it works...