senny / pdfjs_viewer-rails

PDF.js viewer packaged as a Rails engine.
MIT License
114 stars 174 forks source link

display pdf in stored file returns error #22

Closed dimitrideruiter closed 8 years ago

dimitrideruiter commented 8 years ago

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'=<!DOCTYPE html><!--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>

siegy22 commented 8 years ago

You can see in the README (https://github.com/senny/pdfjs_viewer-rails#using-the-helper) that the helper will render a whole HTML document.

Iframe src specifies the source of the iframe which should get loaded. So an URL like http://example.com/annotations/1/pdf.

I think you might make a new view for the pdf view of an annotation. Something like:

# config/routes.rb
Rails.application.routes.draw do
  resources :annotations do 
    get "pdf", on: :member
  end
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

controller:

# app/controllers/annotations_controller.rb

  def pdf
    @annotation = Annotation.find(params[:id])
  end

and then create a view which renders the pdfjsviewer:

# app/views/annotations/pdf.html.erb

<%= pdfjs_viewer pdf_url: @annotation.file, style: :minimal %>

So now you can embed the pdf preview inside an iframe using:

<%= content_tag :iframe, nil, src: annotation_path(@annotation), width: "100%", height: "450px", class: "img-rounded" %>

Let me know if that was what you wanted. Hope this helps.

🍺 cheers

dimitrideruiter commented 8 years ago

thanks for the approach but nope doesn't function; and got to say, kind of too much - not so DRY. would need to build the same when displaying PDF on other object.

siegy22 commented 8 years ago

nope doesn't function. What does that mean? any errors? stacktrace? whatever?

dimitrideruiter commented 8 years ago

doesn't function means that in the iframe it shows the annotation page it self; duplicate in stead of the PDF. And when changing the path to pdf_path(@annotation) it errors out. Not correct?

DRY problem - OK. I am new to RoR so happy to learn. wanted to create a partial called _pdfdisplay.html.erb

siegy22 commented 8 years ago

My bad.. I wrote it wrong (last code block should be):

<%= content_tag :iframe, nil, src: pdf_annotation_path(@annotation), width: "100%", height: "450px", class: "img-rounded" %>
dimitrideruiter commented 8 years ago

No sweat Yves; it renders the PDF now - yet like you wrote as a whole html document - including the shared menu and all within the iframe. This is not what is needed. Looking to get purely the pdf in the iframe (which sits in a a 2 column page (div being bootstrap column)).

siegy22 commented 8 years ago

I think you might want to add layout: false at the end of the pdf action:

# app/controllers/annotations_controller.rb

  def pdf
    @annotation = Annotation.find(params[:id])
    render layout: false
  end

This will only use the pdf view as the whole view, without your layout which is wrapped around.

dimitrideruiter commented 8 years ago

that works (learned to switch of the layout ;-) )

how would I make it a partial now? To reuse when displaying a pdf on other objects?

siegy22 commented 8 years ago

That's on you, it's a good exercise to learn how to implement ways without repeating yourself. It doesn't need to be a partial, that's all I say, it's just one way to simplify the view step, but there are some steps before, so I think you should investigate in the routes.

GitHub issues are here to report bugs or strange behaviour and not as a tutorial how to use this gem.

cheers :beer: