PDF in iFrame getting downloaded instead of displaying [duplicate] - javascript

I want to open the pdf file in an iframe. I am using following code:
<a class="iframeLink" href="https://something.com/HTC_One_XL_User_Guide.pdf"> User guide </a>
It is opening fine in Firefox, but it is not opening in IE8.
Does anyone know how to make it work also for IE ?

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?

It also important to make sure that the web server sends the file with Content-Disposition = inline.
this might not be the case if you are reading the file yourself and send it's content to the browser:
in php it will look like this...
...headers...
header("Content-Disposition: inline; filename=doc.pdf");
...headers...
readfile('localfilepath.pdf')

The direct PDF didn't work on Mobile phones and it doesn't support responsive UI.
Here is the best solution.
https://stackoverflow.com/a/66548544/2078462

Do it like this: Remember to close iframe tag.
<iframe src="http://samplepdf.com/sample.pdf" width="800" height="600"></iframe>

Related

How to embed a PDF in an HTML page?

I need to embed a PDF file in an HTML page for the users to see it on every major device.
Most of the approaches work fine on desktop but they start to show problems on iPad devices. The PDFs are no longer scrollable if placed inside an iframe or embed tag.
I used the following techniques to overcome the problem:
1) Using pdf-image for node and converting the PDF to images and then sliding them in a div.
The problem in this approach is that the image quality gets degraded and is not suitable for viewing on Web.
2) Using PDF.js by Mozilla
It works fine on every device but it makes the page extremely slow and unresponsive on iPad
3) Using Google PDF viewer
<iframe src="https://docs.google.com/viewer?url=http://public-Url-of-pdf.pdf&embedded=true" frameborder="0" height="500px" width="100%"></iframe>
The problem with this approach is that I need to make my PDFs publicly available which I don't want to do for security reasons.
None of the above method is working for me. Is there any solution available to embed PDF in a page which works on iPad also.
One of my colleagues told me about using LibreOffice(open office) headless to embed PDFs in my page but I cannot find any documentation about it usage?
Can anyone please help? :(
Thanks in advance!
<embed src="http://example.com/the.pdf" width="500" height="375" />
Try above one for pure HTML. But another option is if you'd like to use with javascript, try Pdf.js by mozilla. https://github.com/mozilla/pdf.js
I think the simplest way to embed a PDF into a web page is to use the object tag:
<object data="assets/test.pdf" type="application/pdf" width="100%" height="800px">
<p>It appears you don't have a PDF plugin for this browser.
No biggie... you can <a href="assets/test.pdf">click here to
download the PDF file.</a></p>
</object>
What the code above will do is:
- If the user browsing your site has a PDF viewer plugin (which is included by default in some browsers) it will open the PDF in the browser:
- If the user does not have a PDF viewer plugin, they will be presented with a link to download the PDF and view it on their site.
I spent a lot of time with this issue and finally reached a solution for embeeding PDFs in a HTML files, also inspired by this post. You mentioned that "converting the PDF to images and then sliding them in a div" was not satisfactory due to quality problems. Here I experienced the same since the images were blurry.
However, I tried converting the images to SVG instead of PNG and the situation was a different one: The fonts were crystal-clear when embedding the image like below:
<object type="image/svg+xml" data="https://ik.imagekit.io/nudvztcu8my/pdf2svg_example_Ft2FQgqWaG.svg">
<!-- Your fall back here -->
<img src="https://ik.imagekit.io/nudvztcu8my/pdf2svg_example_Ft2FQgqWaG.svg" />
</object>
You can directly paste that snippet into a HTML file and you will see the result. For producing this example I used a ramdom PDF from ArXiv.org and converted it to SVG using an online converter.
There are also free command line tools like pdf2svg or commerical APIs like Aspose and probably it is worth examining which approach gives the best results.
You can easily build a slider which is loading the SVG images dynamically and it is even possible to scale them to different viewports due to the vector character of the SVG images. The approach so far worked for all PDFs I tried but probably it is recommendable to implement a fallback solution still using PNGs.

HTML5 show pdf embedded

In my application I show a pdf embedded in my browser page in this way:
<div>
<object ng-if="vm.ShowPanel" data="{{'data:application/pdf;base64,'+ vm.URL}}"
style="width:100%; height:99%; overflow-y:hidden" type="application/pdf"></object>
</div>
This approach works perfectly in Chrome but as known not in Internet Explorer.
Is there a workaround to perform that action in IE?
I was thinking about saving the file locally and then read it in my object tag.
Could it work?
thanks

How to hide toolbar coming with pdf file when trying to render by using iframe in firefox?

I want to remove toolbar coming with PDF while rendering through iframe. It is working fine in browsers such as Chrome, IE when I use #toolbar=0 with the URL (or file path). But it is not working in Firefox.
Can anyone suggest where am I doing wrong ?
This is my code.
<html>
<body>
<iframe src="Reports/reports.pdf#toolbar=0" width="100%;" height="80%">
</iframe>
</html>
I think it's also dependent on the application/plugin in which browser opens PDF, it works differently and it might ignore there directives (depending on browser, plugin, platform, PDF viewer).
The general recommendation here is to use these "directives" at the end of URL:
#toolbar=0&navpanes=0
You may also try recommendations from http://blogs.adobe.com/pdfdevjunkie/web_designers_guide :
Embedding using PDFObject
You could use basic HTML markup to embed PDF files in your page but
there is a more elegant solution out there. Take a look at PDFObject
by Philip Hutchison. PDFObject is a pretty easy scripting tool for
dynamically embedding PDF files into web pages. It uses JavaScript to
inject an element into the DOM tree of your HTML file. There’s
even a handy code generator to help you out with all of the extra
parameters you may need.
<script type="text/javascript" src="scripts/pdfobject.js"></script>
<div id="pdf1" style="width:500px; height:375px;"></div>
<script type='text/javascript'>
var myPDF = new PDFObject({
url: 'ConferenceGuide.pdf',
pdfOpenParams: {
view: 'Fit',
scrollbars: '0',
toolbar: '0',
statusbar: '0',
navpanes: '0' }
}).embed('pdf1');
</script>
The link to PDFObject is here: http://www.pdfobject.com/ (taken from their website)
SMALL UPDATE
Disclaimer: To avoid further speculations on the topic, you need to take into consideration that your clients/customers might have different plugins and PDF viewers installed on their machines, and different versioning of applications and plugins, so none of the above solutions will work in 100% of the cases. Adobe for example, has separate plugin for Firefox installed, and they had different versions and different behavior built in.
here's reference for the tricky case with FF and Reader X, as the good example of the above said.
iframe{
width: 50%;
height: 100vh;
}
In scss:
<div>
<iframe src="./schedule.pdf" frameborder="0" </iframe>
</div>
This worked fine for me in Firefox, no need for any plugins.
You Can Manage it by using css.... like
<div style="background-color:rgba(51, 189, 204, 0);width:120px;height:30px;z-index:2;top:1.9em;position:relative;margin-left:60%;"></div>
adjust position as per your requirement.

Opening a Base64 encoded PDF in native pdf viewer

I'm working on generating pdfs that contain the image from a html canvas element. I've managed to get an implementation of that working, but I'm having trouble opening the result in a useful way.
PDF.dataURI() returns a string that looks like data:application/pdf;base64,BASE64_ENCODED_PDF_HERE.
I'm currently using window.location = certificate.dataURI() to open up the pdf. I already tried window.open, but Safari wouldn't play nice with it.
RESULTS:
In Firefox, this saves a file that is a random name, followed by .pdf(1).part.
In Safari (desktop and mobile), it opens the PDF in the same tab, but doesn't bring up any pdf viewer interface.
In Chrome, it opens the PDF in the same tab and brings up the PDF interface.
Basically, my question is how to open a string of that format inside of the browser as a PDF, ideally in a new tab.
Any thoughts?
You can use iframes to view the pdf like below
<object data="data:application/pdf;base64, your_base64_data" type="application/pdf">
<iframe src="https://docs.google.com/viewer?&embedded=true"></iframe>
</object>
have you tried document.location.href ?
you can try to control UI interface for PDF viewer from appropriate options inside generated pdf but browser may ignore it, so as al

PDF calling twice in IE

I am facing an issue with PDF loading in my page.
I am using object tag to embed PDF,
<object data='addDocs.do?viewName=PDF&id="+$("#pdfId").val()+"&File=regDoc.pdf' type='application/pdf' width='95%' height='750'> </object>
This is getting called twice when using IE Browser.
I did some search on the same question and tried with response.setDateHeader("Expires", 30000);. No Luck.
I can't use IFRAME because I need to open the links inside the PDF in the same window rather than inside IFRAME.
When using EMBED tag to load PDF, If the size of the PDF is larger then browser is freezing.
So, please help me out on calling PDF only once in IE.
Thanks in advance.

Categories