Skip to content

Instantly share code, notes, and snippets.

@joemasilotti
Last active October 25, 2022 08:40
Show Gist options
  • Select an option

  • Save joemasilotti/6045144 to your computer and use it in GitHub Desktop.

Select an option

Save joemasilotti/6045144 to your computer and use it in GitHub Desktop.

Revisions

  1. joemasilotti revised this gist Jul 20, 2013. 2 changed files with 12 additions and 1 deletion.
    1 change: 1 addition & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    group :test do
    gem 'rspec-rails'
    gem 'capybara'
    gem 'pdf-reader'
    end
    12 changes: 11 additions & 1 deletion pdf_viewing_spec.rb
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,16 @@
    feature 'Viewing a PDF' do
    scenario 'User requests the PDF page' do
    visit "/pdfs.pdf"
    page.should have_content("Hi, PDF!") # Fails: invalid byte sequence in UTF-8
    convert_pdf_to_page
    page.should have_content("Hi, PDF!") # Passes
    end
    end

    def convert_pdf_to_page
    temp_pdf = Tempfile.new('pdf')
    temp_pdf << page.source.force_encoding('UTF-8')
    reader = PDF::Reader.new(temp_pdf)
    pdf_text = reader.pages.map(&:text)
    temp_pdf.close
    page.driver.response.instance_variable_set('@body', pdf_text)
    end
  2. joemasilotti created this gist Jul 20, 2013.
    4 changes: 4 additions & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    group :test do
    gem 'rspec-rails'
    gem 'capybara'
    end
    8 changes: 8 additions & 0 deletions pdf_viewing_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    require 'spec_helper'

    feature 'Viewing a PDF' do
    scenario 'User requests the PDF page' do
    visit "/pdfs.pdf"
    page.should have_content("Hi, PDF!") # Fails: invalid byte sequence in UTF-8
    end
    end
    10 changes: 10 additions & 0 deletions pdfs_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    class PdfsController < ApplicationController
    def index
    respond_to do |format|
    format.pdf do
    render :pdf => 'filename.pdf',
    :show_as_html => params[:debug]
    end
    end
    end
    end
    4 changes: 4 additions & 0 deletions routes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    PdfValidation::Application.routes.draw do
    resources :pdfs, only: [:index]
    root to: "pdfs#index"
    end
    1 change: 1 addition & 0 deletions show.pdf.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    %h1 Hi, PDF!