Coder Social home page Coder Social logo

pdfjs_viewer-rails's Introduction

pdfjs_viewer-rails

Build Status

Installation

Add this line to your application's Gemfile:

gem 'pdfjs_viewer-rails'

Note: pdfjs_viewer-rails is still in early development. Please report if you encounter any issues along the way.

Viewer Styles

This gem ships with three viewer styles:

full

full style

reduced

reduced style

minimal

minimal style

Usage

Using the mountable Engine

The mountable engine makes it extremely simple to integrate the PDF.js viewer into your application:

config/routes.rb

mount PdfjsViewer::Rails::Engine => "/pdfjs", as: 'pdfjs'

Now you can use a link in your templates to open up the viewer:

<%= link_to "display using the full viewer", pdfjs.full_path(file: "/sample.pdf") %>
<%= link_to "display using the minimal viewer", pdfjs.minimal_path(file: "/sample.pdf") %>

Using the helper

If your integration scenario is more complex you may want to consider using the pdfjs_viewer helper. This allows you to embed the viewer into a container like an iframe.

<%= pdfjs_viewer pdf_url: "/sample.pdf", style: :full %>
<%= pdfjs_viewer pdf_url: "/sample.pdf", style: :minimal %>

NOTE: The helper will render a full HTML document and should not be used in a layout.

Verbosity of PDF.js

The verbosity of PDF.js can be set with:

$ export PDFJS_VIEWER_VERBOSITY=warnings

Verbosity levels:

  • errors (default)
  • warnings
  • infos

Customizing the viewer

If you're not happy with the 3 different styles with which pdfjs_viewer-rails is shipped, you can make your own adjustments by creating a file in app/views/pdfjs_viewer/viewer/_extra_head.html.erb. This file will be appended to the viewer's <head> tag.

So for example, if you'd like to hide the print icon:

<!-- app/views/pdfjs_viewer/viewer/_extra_head.html.erb -->

<style>
  #print { display: none; }
</style>

NOTE: You can use the parameters you passed into pdfjs_viewer (if you're using the helper):

<!-- Somewhere in a view in your project -->
<%= pdfjs_viewer style: "reduced", something: "sick!" %>

and then access them:

<!-- app/views/pdfjs_viewer/viewer/_extra_head.html.erb -->

<%= tag.meta name: "something", content: something %>

Setting up CORS

If you plan to load PDFs from that are hosted on another domain from the PDF.js viewer, you may need to set up a Cross-Origin Resource Sharing (CORS) Policy to allow PDF.js to read PDFs from your domain. If you're serving PDFs straight from Amazon S3 (e.g. bucket.s3-us-west-1.amazonaws.com), you will need to add a CORS policy to the S3 bucket. This CORS configuration has been tested on S3:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Range</AllowedHeader>
        <AllowedHeader>Authorization</AllowedHeader>
        <ExposeHeader>Accept-Ranges</ExposeHeader>
        <ExposeHeader>Content-Encoding</ExposeHeader>
        <ExposeHeader>Content-Length</ExposeHeader>
        <ExposeHeader>Content-Range</ExposeHeader>
    </CORSRule>
</CORSConfiguration>

Development

Tests can be executed with:

$ bin/rake

This will render the sample.pdf using headless chrome and save screenshots into test/sandbox.

License

pdfjs_viewer-rails is released under the MIT License.

pdfjs_viewer-rails's People

Contributors

bierik avatar gingermusketeer avatar jone avatar neodelf avatar nicwillemse avatar p8 avatar senny avatar siegy22 avatar yayajacky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pdfjs_viewer-rails's Issues

event for finishing scrolling

There should be an event from which we can calculate how much scrolling done while scrolling pdf.
Right now there is no such event. Can you please add an event?

When using relative root, pdfjs javascript assets do not load correctly

I'm sure there's something I'm overlooking here, but I haven't found a solution yet so I'll stick this here.

I have an application which uses a relative url for the production environment:
Rails.application.config.action_controller.relative_url_root = "/myrelativeroot"

When loading a pdf in development, everything works as intended. However, when I try to do the same in production, I get the following error from the js console:

Loading Worker from “https://mywebsite/pdfjs/web/pdf.worker.js?version=1.10.100” was blocked because of a disallowed MIME type (“text/html”)

It turns out, this file doesn't exists. However, when I go to https://mywebsite/myrelativeroot/pdfjs/web/pdf.worker.js?version=1.10.100, the file can be found just fine. Somehow, I need to tell pdfjs to append the relative url in the right spot to find pdf.worker.js.

# routes.rb
mount PdfjsViewer::Rails::Engine => "pdfjs", as: 'pdfjs'

display pdf in stored file returns error

want to use pdfs viewer to ensure cross browser compatibility and to view file (pdf) attached to annotation, I use:
<iframe src="<%= pdfjs_viewer pdf_url: "@annotation.file", style: :minimal %>" width=450px% height=450px class="img-rounded"></iframe>

It returns the error:

Couldn't find Annotation with 'id'=<!--Copyright 2012 Mozilla FoundationLicensed under the Apache License, Version 2

Note: this works totally fine <iframe src="<%= @annotation.file %>" width=100% height=450px class="img-rounded"></iframe>

iframe example?

Hello,

I'm trying to use the viewer inside an iframe and I'm just getting a white box. Could you provide a working example of an iframe using the helper method provided?

My iframe:
<iframe srcdoc="<%= pdfjs_viewer(pdf_url: pdf, style: :minimal) %>"></iframe>

I've also tried escaping the quotes, which loads the html, but the javascript and css don't load:
<iframe srcdoc="<%= pdfjs_viewer(pdf_url: pdf, style: :minimal).gsub('"', '&quot;') %>"></iframe>

Thanks for any help!

'Go back one page' doesn't work.

The previewer works great, except that when I click the back button to get out of the viewer, the routes change (I can see it change in the URL bar), but the previewer still stays open and the page doesn't actually change. I have to manually refresh the page which doesn't seem too bad, but as far as UX goes, it isn't really a good experience.

Integrate with iframe

Can you please give a full example of the syntax to integrate with irame?
<%=pdfjs_viewer pdf_url: "/sample.pdf", style: :full %> is too simple for me.

couldn't find file 'pdfjs_viewer/pdfjs/compatibility'

Hi, I've tried to remove the pdfjs_viewer-rails gem from my project but i'm facing the follow error

couldn't find file 'pdfjs_viewer/pdfjs/compatibility' with type 'application/javascript'

I already removed the gem from gem file, run the bundlecommand and remove the mounter from routes.rb but nothing changed

How to update underlying pdfjs?

As of 2020-5-13, stable Mozilla pdf.js version is 2.3.200.

When I look around in the source code, the version is 1.10.100. Correct me if I am wrong.

I want to confirm Mozilla pdf.js content (web and build) is distributed across public folder and app/assets folders.

pdf_url error (probably just me)

Apologies if this is the wrong place for this - But I'm struggling with basic implementation.

In the code below, where is the pdf_url: option looking? Is there a best place in the application to store your pdf files?

<%= pdfjs_viewer pdf_url: "/sample.pdf", style: :minimal %>

I've been trying to resolve the following error for quite awhile without success:

PDF.js v1.7.225 (build: 17d135f) Message: Missing PDF "http://localhost:3000/sample.pdf".

I have a sample.pdf file in the root of my application.

Getting Error file origin does not match viewer's

I am getting error file origin does not match viewer's. so please help me on this. we are using s3 for storage and i have configured CORS with cross origin. But i am getting above error. So please can anbody help me on this.
pdf-error

Use externally hosted PDFs (Amazon S3, etc)

I recently ran into a version of this error (mozilla/pdf.js#7153) when trying to use this gem to display PDFs hosted in an Amazon S3 bucket. Originally thought I had a CORS issue but was able to solve it by overwriting /pdfjs_viewer/viewer.js and inserting my development and production urls into the HOSTED_VIEWER_ORIGINS around line 7100.

Would you be open to a pull request, either to add some documentation to the README, or to make HOSTED_VIEWER_ORIGINS dependent on an ENV variable?

Prvate PDF views

Hi guys! How can i protect my pdf-files with authorization? And if not authorized user go to link_with_PDF he redirected to login page. Thanks!

Add way to configure verbosity

We're running some automated tests via PhantomJS using the PDF viewer, and it'd be nice to be able to turn off the "Warning: Setting up fake worker." output on every test run.

PDFJS allows you to configure it's verbosity, but it's difficult to configure it via the .pdfjs_viewer helper method. I'm thinking the best way to do that might be to add a verbosity: option.

Thoughts?

Syntax Error in Helper

When deploying to Heroku, this error comes up.

Important part:

def pdfjs_viewer(style:, pdf_url: nil, title: nil)

Full error:

/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require': /app/vendor/bundle/ruby/2.0.0/gems/pdfjs_viewer-rails-0.0.6/lib/pdfjs_viewer-rails/helpers.rb:4: syntax error, unexpected ',' (SyntaxError)
2015-11-17T19:29:45.025205+00:00 app[web.1]:       def pdfjs_viewer(style:, pdf_url: nil, title: nil)

uninitialized constant PdfjsViewer (NameError)

In production mode I got this error
config/routes.rb:17:in block in <top (required)> : uninitialized constant PdfjsViewer (NameError)
My routes.rb
mount PdfjsViewer::Rails::Engine => "/pdfjs", as: 'pdfjs'
In development mode everything is ok

Change directory path

<iframe src="<%= pdfjs.reduced_path(file: "/name.pdf") %>"> working perfect for Rails.root.to_s + "/public/" directory. But what i can do for get documents from Rails.root.to_s + "/upload/.../" folder

Zoom

Is there a way to set the default zoom level of the pdf when loaded? Thanks

Unexpected server response - Amazon

Heya

Getting this error when trying to retrieve from Amazon:

PDF.js v1.0.907 (build: e9072ac)
Message: Unexpected server response (0) while retrieving PDF "https://xxxxx.s3.amazonaws.com/uploads/document/document/3/xxxxx.pdf".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.