• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PDF.js viewer packaged as a Rails engine.

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.

More Repositories

1

emacs-eclim

This project brings some of the great eclipse features to emacs developers. It is based on the eclim project, which provides eclipse features for vim.
Emacs Lisp
587
star
2

sablon

Ruby Document Template Processor based on docx templates and Mail Merge fields.
Ruby
446
star
3

rvm.el

use rvm to manage ruby versions within emacs
Emacs Lisp
214
star
4

cabbage

get the maximum out of emacs
Emacs Lisp
156
star
5

rbenv.el

use rbenv to manage your Ruby versions within Emacs
Emacs Lisp
87
star
6

emacs-xcode

A set of tools to make OS-X and iPhone development with emacs pleasant. It simplifies writing objective-c applications and provides utilities to compile and lunch the project.
Emacs Lisp
39
star
7

corner_stones

capybara building blocks for acceptance tests.
Ruby
18
star
8

emacs-configs

moved to: https://github.com/senny/e-max
Emacs Lisp
16
star
9

emacs.d

Personal Emacs configuration
Emacs Lisp
15
star
10

theme-roller.el

a wrapper around color-theme to keep your themes in sync and up to date.
Emacs Lisp
14
star
11

dotfiles

My personal dotfiles (ruby, rails, zsh, git)
Emacs Lisp
10
star
12

dm-freebase-adapter

A DataMapper adapter to connect to Freebase. The goal of this project is to link any Freebase data into your existing database structure as needed.
Ruby
7
star
13

backyard

manage the models in your cucumbers with ease
Ruby
7
star
14

dm-isbndb-adapter

a DataMapper adapter for isbndb
Ruby
5
star
15

dumpster

Abstraction layer to export tabular data. Currently dumpster supports XLSX and CSV exports.
Ruby
4
star
16

qa-mode

Emacs Major mode to edit QA files for SuperMemo.
Emacs Lisp
4
star
17

git-showoff

Git Schulungsmaterial
Shell
3
star
18

asdf-postgres-app

asdf plugin to manage Postgres.app binaries
Shell
2
star
19

cucumber-i18n.el

translate i18n keys inside your cucumber features
Emacs Lisp
2
star
20

git-workshop-example

รœbungen zum Git-Workshop
JavaScript
2
star
21

cabbage-contrib

cabbage-contrib is a repository of common cabbage bundles, which are not shipped in the core cabbage distribution.
Emacs Lisp
1
star
22

ichnite

A structured logging library for Ruby
Ruby
1
star