[转]PDF预览插件PDFObject.js
本文转自:http://pdfobject.com/index.php
When possible, use standardized HTML markup and avoid JavaScript
In an ideal world, you would always embed your PDF files using an <object> element hard-coded in your HTML markup. Why? For starters, avoiding JavaScript usually means greater accessibility, greater searchability (important for those concerned with search engine optimization), and fewer browser compatibility issues. Since no JavaScript is being used, standardized markup is also quicker to load and less of a drain on computer and network resources.
Click here to learn more about embedding PDFs using standardized HTML markup.
Sometimes you need a little JavaScript. When you do, try PDFObject.
Some projects will require dynamically embedded PDFs. PDFObject was designed for this purpose, and makes embedding PDFs quick and easy while maintaining a healthy respect for standards.
PDFObject uses JavaScript to generate the same standards-compliant <object> markup you would normally write yourself, then inserts the <object> into your HTML element of choice. You can fill the entire browser window, or insert the PDF into a <div> or other block-level element.
Here's a very simple no-frills example of PDFObject; this example will make the PDF fill the entire browser window:
<html>
<head>
<title>PDFObject example</title>
<script type="text/javascript" src="pdfobject.js"></script><script type="text/javascript">
window.onload = function (){
var success = new PDFObject({ url: "sample.pdf" }).embed();
};
</script> </head>
<body>
<p>It appears you don't have Adobe Reader or PDF support in this web
browser. <a href="sample.pdf">Click here to download the PDF</a></p>
</body>
</html>
Embedding a PDF can't get much easier! There are also many other embed options available; visit the code generator to see what's possible.
- Click here to read detailed instructions for using PDFObject.
- Click here to see examples of PDFObject in action.
Features
- Detection of PDF plugin or PDF file-handling support.
- Checks for Adobe Reader.
- Checks for generic support via the application/pdf mime type.
- Only attempts to embed PDF if support is detected.
- if PDF support is detected, the <object> is embedded with all specified parameters;
- if PDF support is not found, the <object> will not be embedded, and the fallback content is left as-is.
- Returns reference to the HTML <object> (similar to getElementById) or null.
- If no element ID is passed into the embed() call, PDFObject will default to document.body, filling the entire browser window.
- Accepts optional embed parameters, including size, id, and most PDF open parameters.
- Automatically appends extra CSS needed for full-window PDF embeds
- Removes padding and margins
- Fixes 100% height issue in some browsers
Compatibility
Please note this compatibility chart has not been updated to reflect PDFObject 1.2.
PDFObject is compatible with every major browser capable of displaying PDFs. Please note these results are from internal testing of PDFObject using Adobe Reader; no open-source PDF browser plugins were used during testing.
| Windows XP (Professional) | Windows Vista (Business) | Mac OS X 10.5 (Leopard) | Ubuntu 8.04 (Hardy Heron) | |
|---|---|---|---|---|
| Internet Explorer 6 | X | not applicable | not applicable | not applicable |
| Internet Explorer 7 | X | X | not applicable | not applicable |
| Mozilla Firefox 3 | X | X | no Adobe Reader plugin | X |
| Apple Safari 3.1 | X | X | X | not applicable |
| Opera 9.5 | X | X | no Adobe Reader plugin | X |
not applicable: Specified browser is not produced for this operating system. no Adobe Reader plugin: Adobe Reader unavailable for this browser/OS combination.
Syntax
PDFObject's syntax will feel very familiar to users of JavaScript frameworks such as jQuery and MooTools. Here's a typical example:
var myPDF = new PDFObject({
url: "sample.pdf",
id: "myPDF",
width: "500px",
height: "300px",
pdfOpenParams: {
navpanes: 1,
statusbar: 0,
view: "FitH",
pagemode: "thumbs"
}
}).embed("mydiv");
Here's another example:
var myParams = {
url: "sample.pdf",
pdfOpenParams: { view: "FitV" }
};
var myPDF = new PDFObject(myParams).embed("mydiv"); //returns reference to new HTML <object>
Note that embed is chained on to the new PDFObject statement. You can also use them separately:
var myParams = {
url: "sample.pdf",
pdfOpenParams: { view: "FitV" }
};
var myPDF = new PDFObject(myParams); //returns reference to JavaScript object
myPDF.embed("mydiv"); //returns reference to new HTML <object>
Properties and methods
PDFObject contains the following properties and methods.
| Name | Type | Description |
|---|---|---|
| PDFObject(obj) | constructor |
Use an object for passing arguments. obj can contain the following arguments:
Returns reference to self (this). |
| PDFObject.embed(targetID) | method | Embeds PDF. Returns HTML <object> element or false if embed not successful. |
| PDFObject.get(prop) | method |
Returns value of property. If property is not found or has no value, returns null. Available properties:
|
| pipwerks.pdfUTILS.detect.hasReader() | support function | Returns boolean indicating whether navigator.plugins version of Adobe Reader has been found. |
| pipwerks.pdfUTILS.detect.hasReaderActiveX() | support function | Returns boolean indicating whether ActiveX version of Adobe Reader has been found. |
| pipwerks.pdfUTILS.detect.hasGeneric() | support function | Returns boolean indicating whether application/pdf mime type is supported via navigator.mimeTypes |
| pipwerks.pdfUTILS.detect.pluginFound() | support function | Returns string "Adobe" or "generic". Returns null if none found. |
| pipwerks.pdfUTILS.setCssForFullWindowPdf() | support function | sets the following style properties: html.style.height = "100%"; html.style.overflow = "hidden"; body.style.margin = 0; body.style.padding = 0; body.style.height = "100%"; body.style.overflow = "hidden"; These properties need to be set in order to remove margins/padding in the document, and to enable the object to stretch to 100% vertically. |
| pipwerks.pdfUTILS.buildQueryString(pdfOpenParams) | support function | Takes properties of pdfOpenParams object and 'stringifies' them. |
| pipwerks.pdfUTILS.termFound(strToSearch, term) | support function | Returns boolean shortcut for strToSearch.indexOf("term") !== -1 |
Known issues
PDFObject has no known issues at this time. The biggest issue you may face is browser support for Adobe Reader and/or funky behavior in Reader. This is beyond the scope of this simple embed script. Shortcomings encountered with Reader during testing included:
- Crashes/hanging when trying to replace an embedded PDF with a second PDF (by replacing the first <object> element with a new <object>).
- Crashes/hanging when attempting to embed more than one PDF on a single HTML page (FYI this behavior can be avoided by using iframes).
Important note: PDFObject does NOT include version detection
PDFObject does not allow you to target a specific version of Adobe Reader, such as version 7 and higher. While version detection is possible, it is a very cumbersome task and nearly impossible to maintain or make future-proof, largely due to Adobe frequently changing the plugin's name and description. For instance, in Safari, Adobe Reader 8.1's name is reported as "Adobe Acrobat and Reader Plug-in," while the name in the Windows version of Firefox is "Adobe PDF Plug-In For Firefox and Netscape." Until version reporting is consistent in Adobe Reader (across browsers and operating systems), PDFObject will not include version detection.
Tip of the cap
PDFObject was built using information, code, tools, or inspiration provided by others, including (in no particular order):
- SWFObject (Geoff Stearns and Bobby van der Sluis)
- Douglas Crockford's JSLint
- The JavaScript Compressor
- Documentation provided by Adobe at adobe.com
- Detecting plugins in Internet Explorer (and a few hints for all the others)
- http://www.howtocreate.co.uk/wrongWithIE/?chapter=navigator.plugins (Mark Wilton-Jones)
- Determining Adobe Acrobat PDF Reader Plugin Support Using JavaScript
Thanks, guys!
[转]PDF预览插件PDFObject.js的更多相关文章
- PDF预览之PDFObject.js总结
get from:PDF预览之PDFObject.js总结 PDFObject.js - 将PDF嵌入到一个div内,而不是占据整个页面(要求浏览器支持显示PDF,不支持,可配置PDF.js来实现 ...
- 最好用的js前端框架、组件、文档在线预览插件
这里收集的都是个人认为比较好的js框架.组件 js前端ui框架 此处列举出个人认为最好的几个框架(排序即排名),现在好点的框架商用都需要付费,以下几个也不例外,但是由于组件丰富,都可以作为企业应用的完 ...
- 基于Three.js的360X180度全景图预览插件
基于Three.js的360X180度全景图预览插件 时间 2015-08-12 10:01:10 HTML5中国 原文 http://www.html5cn.org/article-8621-1 ...
- js图片预览插件,不涉及上传
小小的几十行代码,很牛逼,很实用. 支持多个图片的预览,只要new多个对象就行了. html如下 <!-- zhouxiang www.zhou-xiang.com --> <!DO ...
- 浏览器实现PDF预览
1.使用jquery.media.js预览PDF <!DOCTYPE html> <html> <head> <meta charset="utf- ...
- Pdf预览功能实现(asp.net)
asp.net中使用 1.pdf预览功能实现的插件是pdfjs-1.5.188-dist //引入插件中相关的文件以及jquery文件 @section css{ <link rel=" ...
- uploadPreview 兼容多浏览器图片上传及预览插件使用
uploadPreview兼容多浏览器图片上传及预览插件 http://www.jq22.com/jquery-info2757 Html 代码 <div class="form-gr ...
- 图像本地预览插件(基于JQUERY、HTML5)
最近是被这项目搞疯了.害我天天写插件,上周才写,现在就继续吧..... 说说这个吧.主要是用于本地图像预览的.我们知道在以前,图像预览一般都很麻烦,一般都是异步上传然后返回路径,动态设置路径,但是这样 ...
- Android原生PDF功能实现:PDF阅读、PDF页面跳转、PDF手势伸缩、PDF目录树、PDF预览缩略图
1.背景 近期,公司希望实现安卓原生端的PDF功能,要求:高效.实用. 经过两天的调研.编码,实现了一个简单Demo,如上图所示. 关于安卓原生端的PDF功能实现,技术点还是很多的,为了咱们安卓开发的 ...
随机推荐
- EF工作中踩过的坑.
1.EF同一个linq里边不支持两个或两个以上不同dbcontext的使用,必须拆解开才能使用; ef也不支持自定义集合和dbcontext属性的混合使用. 2.如果要用用统一域账号连接databas ...
- C#中 导入和导出Excel的方法
using System.Data; using System.Data.OleDb; /// <summary> /// Excel转为DataTable /// </summar ...
- 回文串---吉哥系列故事——完美队形II
HDU 4513 Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出 ...
- mybatis mapper association collection
1.Question Description: sometimes, POJO bean contains another bean or collection as property, it's s ...
- java中的代码块执行顺序
/* 代码块:在Java中,使用{}括起来的代码被称为代码块. 根据其位置和声明的不同,可以分为 局部代码块:局部位置,用于限定变量的生命周期. 构造代码块:在类中的成员位置,用{}括起来的代码.每次 ...
- Verilog学习笔记设计和验证篇(一)...............总线和流水线
总线 总线是运算部件之间数据流通的公共通道.在硬线逻辑构成的运算电路中只要电路的规模允许可以比较自由的确定总线的位宽,从而大大的提高数据流通的速度.各个运算部件和数据寄存器组可以通过带有控制端的三态门 ...
- 数据库==>>数据查询基础
数据查询基础 还好吗?几天不见,甚是思念呀!笑对人生,好好生活,快快乐乐的迎接我们的美好未来吧! 好吧!抒情结束,我们一起来学习一下我们今天的主题:数据查询基础,很有意思哟.让我们来感受它的魅力吧! ...
- svn的管理与维护要点—纯手工编写
由于在公司要维护阿里云的linux服务器,我们的svn服务器就安在阿里云上面.所以经常会涉及到svn的维护操作.离职的时候编写交接文档,刚好有充足的时间写一篇说明介绍,此说明纯原创,不是从网上复制,手 ...
- log4j.xml 配置参数属性level使用心得
jdbc.sqlonly 只显示执行的sql语句.info级才可以显示,debug增加显示java源代码位置. jdbc.sqltiming 显示执行的sql语句以及语句执行时间, ...
- 关于一个js连续赋值问题之我见(词略穷,见谅)
前几天在搜索面试题时发现了这么一段代码,执行完后感觉完全不与所想的一样 var a = { n : 1 }; var b = a; a.x = a = {n : 2}; console.log(a.x ...