[转]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.

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:

  • url (string, required)
  • id (string)
  • width (string, with unit of measurement such as "100%" or "500px")
  • height (string, with unit of measurement such as "100%" or "500px")
  • pdfOpenParams (object, may contain any of the PDF Open properties specified by Adobe)

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:

  • url  (string)
  • id (string)
  • width (string)
  • height (string)
  • pdfOpenParams (object)
  • pluginTypeFound (string)
  • pdfobjectversion (string)
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):

Thanks, guys!

时间: 2024-12-07 00:15:21

[转]PDF预览插件PDFObject.js的相关文章

PDF预览之PDFObject.js总结

get from:PDF预览之PDFObject.js总结 PDFObject.js - 将PDF嵌入到一个div内,而不是占据整个页面(要求浏览器支持显示PDF,不支持,可配置PDF.js来实现) 官网: https://pdfobject.com/ github地址: https://github.com/pipwerks/PDFObject 支持: PDFObject 2.0不向后兼容1.0版本,针对现代浏览器设计,支持Chrome, Firefox, Safari (OS X and i

基于Three.js的360X180度全景图预览插件

基于Three.js的360X180度全景图预览插件 时间 2015-08-12 10:01:10  HTML5中国 原文  http://www.html5cn.org/article-8621-1.html 主题 Three.js 简要教程 Photo Sphere Viewer是一款基于Three.js的360X180度全景图预览js插件.该js插件可以360度旋转查看全景图,也可以上下180度查看图片.使用该插件的唯一要求是浏览器支持canvas或WebGL. 查看演示      下载插

Pdf预览功能实现(asp.net)

asp.net中使用 1.pdf预览功能实现的插件是pdfjs-1.5.188-dist //引入插件中相关的文件以及jquery文件 @section css{ <link rel="stylesheet" href="~/pdfjs-1.5.188-dist/web/viewer.css"> <script src="~/pdfjs-1.5.188-dist/web/compatibility.js"></scr

图片上传时预览插件

html代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script src="uploadView.js" type="text/javascript"></script> <script> window.

Eclipse安装propertie预览插件

Eclipse安装properties预览插件 在公司做项目都是用的myeclipse,myeclipse都是直接集成了插件不过占用内存非常巨大.Eclipse能比myeclipse省接近一半的内存,好久就想研究eclipse,这两天用了下eclipse,发现很不适应,好多东西得自己下载插件,尤其是页面和配置文件的预览功能,在此就以properties预览来举例,一共两种方法,第一种是自己装Properties Edito成功的方法,第二种是别人的方法,楼主表示自己笨装插件时第二种报错,始终没找

上传图片预览插件(转)

/* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *插件网站:http://keleyi.com/keleyi/phtml/image/16.htm *参数说明: Img:图片ID;Width:预览宽度;Height:预览高度;ImgType:支持文件类型;Callback:选择文件显示图片后回调方法; *使用方法: <div> <img id

JQuery插件:图片上传本地预览插件,改进案例一则。

/* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *插件网站:http://keleyi.com/keleyi/phtml/image/16.htm *参数说明: Img:图片ID;Width:预览宽度;Height:预览高度;ImgType:支持文件类型;Callback:选择文件显示图片后回调方法; *使用方法: <div> <img id

pdf预览-js版本

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>js插件让你的网站支持在线预览pdf功能</title>    <script src="http://www.lanrenzhijia.com/ajaxjs/pdfobject.js"></script>

JS图片上传预览插件制作(兼容到IE6)

其实,图片预览功能非常地常见.很意外,之前遇到上传图片的时候都不需要预览,也一直没有去实现过.现在手上的项目又需要有图片预览功能,所以就动手做了一个小插件.在此分享一下思路. 一.实现图片预览的一些方法. 了解了一下,其实方法都是大同小异的.大概有以下几种方式: ①订阅input[type=file]元素的onchange事件. 一旦选择的路径被改变就把图片上传至服务器,然后就返回图片在服务器端的地址,并且赋值到img元素上. 缺点:工作量大,有些上传并不是用户最终需要上传的图片,但是这种方式会