This do the trick for me.
$.ajax(
{
url: "myServlet",
...
complete: function(data) {
$('#myframe').attr('src','data:application/pdf;base64,'+data);
}
}
);
Also, you should encode the ajax ans this way base64_encode($data).
I hope you find this helpful.
Answer from Daniel Perez Malagon on Stack OverflowHow to display a PDF in iframe with ajax after calling a servlet
php - Jquery binary response to display pdf in iframe - Stack Overflow
apex - How to display downloaded PDF blob in an iframe? - Salesforce Stack Exchange
display pdf from binary data | OutSystems
This do the trick for me.
$.ajax(
{
url: "myServlet",
...
complete: function(data) {
$('#myframe').attr('src','data:application/pdf;base64,'+data);
}
}
);
Also, you should encode the ajax ans this way base64_encode($data).
I hope you find this helpful.
In Javascript, to encode the ajax answer you should do it this way:
var enc = btoa(data)
If you have problems, you should try this:
var enc = btoa(unescape(encodeURIComponent(data)))
Then
$('#myframe').attr('src','data:application/pdf;base64,'+ enc);
I've been stuck on this for awhile. Long story short, I'm trying to have my web application preview a pdf within the web browser.
My front-end uses a script to send a GET request to retrieve a pdf stored on my backend. My backend successfully sends the pdf data via Expressjs. The front-end receives the pdf binary then stores it within a variable.
From here I have no idea how to render the binary data into a pdf that can be displayed within an html canvas. I was hoping someone might be able to give me a few ideas.
// FILE: app.js (back-end)
const express = require('express');
const app = express();
var fs = require('fs');
var path = require('path');
app.use(express.static('./methods-public'));
app.get('/api/pdf', (req, res) => {
absolutePath = path.resolve(__dirname, 'dog.pdf');
res.contentType('application/pdf');
res.sendFile(absolutePath);
});
app.listen(5000, () => {
console.log('Server is listening on port 5000....');
});-
<!-- FILE: index.html (front-end) -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<div class="result"></div>
<!-- Axios script -->
<script
src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"
integrity="sha512-bZS47S7sPOxkjU/4Bt0zrhEtWx0y0CRkhEp8IckzK+ltifIIE9EMIMTuT/mEzoIMewUINruDBIR/jJnbguonqQ=="
crossorigin="anonymous"
></script>
<!-- PDF logic -->
<script>
const result = document.querySelector('.result');
const fetchPDF = async () => {
try {
const { data } = await axios.get('/api/pdf');
result.innerHTML = '<h3>' + data + '</h3>';
} catch (error) {
result.innerHTML = `<div class="alert alert-danger">Can't Fetch PDF</div>`;
}
};
fetchPDF();
</script>
</body>
</html>You can just return it using File. Something like this:
public ActionResult ShowPDF()
{
byte[] pdf = myService.GetPDF();
return File(pdf, "application/pdf");
}
UPDATE
Create a page containing a iframe element and set the src attribute to point to your view that renders the PDF file. Here is an example taken from here
<iframe src="ShowPDF" width="100%" style="height:20em">
[Your browser does <em>not</em> support <code>iframe</code>,
or has been configured not to display inline frames.
You can access <a href="ShowPDF">the document</a>
via a link though.]
</iframe>
HTML5 has added the embed tag which allows a variety of rich content to be embedded into the page as follows:<embed src="your file path here" type="application/pdf" />
Using an iframe to "render" a PDF will not work on all browsers; it depends on how the browser handles PDF files. Some browsers (such as Firefox and Chrome) have a built-in PDF rendered which allows them to display the PDF inline where as some older browsers (perhaps older versions of IE attempt to download the file instead).
Instead, I recommend checking out PDFObject which is a Javascript library to embed PDFs in HTML files. It handles browser compatibility pretty well and will most likely work on IE8.
In your HTML, you could set up a div to display the PDFs:
<div id="pdfRenderer"></div>
Then, you can have Javascript code to embed a PDF in that div:
var pdf = new PDFObject({
url: "https://something.com/HTC_One_XL_User_Guide.pdf",
id: "pdfRendered",
pdfOpenParams: {
view: "FitH"
}
}).embed("pdfRenderer");
This is the code to link an HTTP(S) accessible PDF from an <iframe>:
<iframe src="https://research.google.com/pubs/archive/44678.pdf"
width="800" height="600">
Fiddle: http://jsfiddle.net/cEuZ3/1545/
EDIT: and you can use Javascript, from the <a> tag (onclick event) to set iFrame' SRC attribute at run-time...
EDIT 2: Apparently, it is a bug (but there are workarounds):
PDF files do not open in Internet Explorer with Adobe Reader 10.0 - users get an empty gray screen. How can I fix this for my users?
If someone is facing the issue, here is how I have resolved it.
On the above code, I was creating a Blob of pdf binary manually, I am not sure what was causing the issue that for one pdf it was working and for another, it was not working but now I changed my code and instead of creating blob manually I am using $http (I am using AngularJS) to get blob by using responseType: "blob".
Code -
$http({
url: settings.url,
method: "GET",
responseType: "blob",
headers:{
"Authorization": "Bearer <?php echo $jwt; ?>"
}
}).then(function(response) {
var fileURL = URL.createObjectURL(response.data);
$('#iframePdfURL').attr('src',fileURL);
});
As I am using AngularJS I used $http but I found we can also do the same and request blob as responseType with fetch API.
I hope this will help.
DataURL has file size (length) limitation Chrome - 2mb
You can choose to render it directly through JS code, after you download the file with HttpRequest... People are making JS PDF readers for HTML5 - See here http://andreasgal.com/2011/06/15/pdf-js/
The other option is to embed it with an Iframe option
are you trying to show a pdf file in browser, use iframe
<iframe id="abcdef" src="stargeturl" style="width: 100%; height: 800px;">
Your browser does <em>not</em> support <code>iframe</code>
</iframe>
