Created
June 7, 2014 08:09
-
-
Save esedic/c255b1f656ac6694a5d1 to your computer and use it in GitHub Desktop.
Display PDF in-page without a javascript plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* When browser supports inline pdfs | |
* There is no need to append a large jquery plugin that displays them inline. | |
* | |
* You can actually use an iframe (and style it appropriately) | |
* or a link whenever inline PDF viewing is not supported | |
* | |
* This function is fairly simple and it's only for demo purposes | |
*/ | |
function appendPdf(id, url) { | |
var $el = $('#'+id); | |
// Check whether the browser supports displaying pdf files inline (ie. without downloading them) | |
if (navigator && navigator.mimeTypes && navigator.mimeTypes['application/pdf']) { | |
// You may add extra attributes (eg. to allow transparency) or style the iframe | |
$el.html('<iframe src="'+url+'"></iframe>'); | |
} else { | |
$el.html('<a href="'+url+'">Download file</a>'); | |
} | |
} | |
/** | |
* If you need a really lightweight solution and you don't mind | |
* if the users with browsers that don't support PDF viewing, | |
* don't view the document, here's a snippet you may use | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment