开发者

Inline PDF viewer

Is there is PDF viewer which I can embed into my HTML and which I can style as I want.

Actually I need to show some page of PDF file and customize my own UI (few buttons to turn over the pages and commenting).

And if there is any Ruby solution (It's not actually about programming, as I understand) it will be great.

I want to show pdf because:

  • User can copy text
  • Text is in original layout

So I don't want to show it as images or as a converted text. But I want to show page in simple design. Without Fl开发者_开发问答ex or whatever.


Yes, it is possible, with HTML5!

https://github.com/mozilla/pdf.js/

This is a pure-javascript library that uses HTML5 functions to open a PDF file and render it in the page. I successfully test the library in following browsers:

  • IE9 and current version of Google Chrome, Firefox, Safari
  • IE8 and lower do not support pdf.js (lacking HTML5 functions)

Usage

Include the javascript libraries in the HTML file and add an -element ( is HTML5 element, so it will work only in HTML5 browsers):

<body>
  <canvas id="the-canvas" style="border:1px solid black;"/>
</body>

Use a javascript call similar to this to render the PDF inside this canvas:

PDFJS.getDocument('your-file-goes-here.pdf').then(function(pdf) {
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport(scale);

    var canvas = document.getElementById('the-canvas');
    var context = canvas.getContext('2d');
    canvas.height = viewport.height;
    canvas.width = viewport.width;

    var renderContext = {
      canvasContext: context,
      viewport: viewport
    };
    page.render(renderContext);
  });
});


Other than a few browsers that have PDF viewers built in (Chrome comes to mind), PDF display is handled by external plugins, over which you have no control. You can't embed a PDF into a page, other than via iframes, and even then it's a completely external app from the browser, and not subject to css styling rules.

I don't see what Ruby has to do with this, as that'd be a server-side operation, and you're talking about client-side preparing. You could use Ruby (or most any other language) to extract the PDF's text, but you explicitly say you don't want to do that.


The closest thing to what you're asking would be Google Doc Viewer. It at least doesn't require anything more than JavaScript.


You could alternatively convert the PDF into HTML5 and display those pages using the PDF2HTML5 product by IDRSolutions. The output can be placed inside your own CMS and you can add your own GUI or you could use the existing options. You can have a look at potential output on the free converter and trial it if you like, it's a 100% Java library though which may or may not be a problem.

Note: For the sake of transparency it's worth mentioning I am currently a developer on said product so am very familiar with it's capabilities.

It's also worth noting pdf.js doesn't always perfectly match the actual pdf but it is very difficult to get an exact match and still preserve some of the key features you want. However it does do quite a good job most of the time.


The only thing that I know of that would help you do that is Adobe Flashpaper (there could be open source alternatives? Not sure).

  • http://www.adobe.com/products/flashpaper/

Examples here:

  • http://www.adobe.com/products/flashpaper/examples/


  • Considering compatibility, the best is to use a genuine adobe software for rendering pdf. Third party renderers work most of the times, but occasionally have trouble with some aspect of formatting. You can just download pdf reader from the adobe website, do a simple setting on the web browser, and let the browser open a pdf file using the genuine pdf renderer. At least on Firefox, you can do that.

  • If you want to progmatically display a pdf file without using a web browser, that means you need a GUI toolkit that works with a pdf renderer. All I know is poppler on ruby/gnome2. I once was able to use it with ruby 1.8, but since I have moved to ruby 1.9, I have not tried it. Other standard GUI toolkits for ruby are wxruby, fxruby, ruby/qt, shoes etc., but I am not sure which of them has a pdf renderer.

Sorry if not helpful.


No need for external libraries. Much browsers support already multiple ways to vies the pdfs. Like object, iframe, embed. You can use an object, with an iframe fallback, which in return fallbacks on a download:

<object data="/pdf/sample-3pp.pdf#page=2" type="application/pdf" height="100%" width="100%">
<iframe src="/pdf/sample-3pp.pdf#page=2" style="border: none;" height="100%" width="100%">
This browser does not support PDFs. Please download the PDF to view it: <a href="/pdf/sample-3pp.pdf">Download PDF</a>
</iframe>
</object>

Source

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜