How to open pdf file in wicked_pdf? - ruby-on-rails

Right now im using rail 3.0.0 version.now im generate the pdf file and save that file in public folder using wicked_pdf.now i want open that pdf file using controller action.Im using this code in controller.but it is not working.please help me how to do.
def download_prescription_pdf
pdf_pres = UploadedDocument.find(params[:pdf])
send_file "#{RAILS_ROOT}/public/prescription/#{pdf_pres.file_path}", :type => "application/pdf"
end

This works for me:
render :template => 'admin/idreport',
:formats => [:pdf],
:handlers => [:erb],
:pdf => "ID List",
:save_to_file => Rails.root.join('../Documents', "ID List")
Good Luck
Bob

Related

Download Blob file in Rails App is shown inline and not attached

I get a blob from a function in my oracle Datenbank. Users of my Rails App should be able to download these BLOB. My Problem is, that after clicking on download, the BLOB file is shown inline in the browser, but it should be download as a file.
link:
= link_to export_pldw_plausibility_path, :class => "btn btn-primary
action-button" do
%span.glyphicon.glyphicon-pencil
%span.hidden-xs
= "Export"
route:
resources :pldw_plausibilities do
get 'export', on: :member
end
controller:
def export
send_data PldwPlausibility.export(params[:id]), :filename => "test.BLOB",
:disposition => 'attachment'
end
Model:
def self.export(id)
return plsql.pld.pld_mdc.exportMDOC('PLD_PLAUSIBILITY', id.to_i)
end
Screenshot
Sorry!
Adding :target => "_blank" to the link solves this Problem..

Issues creating PDF using wicked and Rails API

I'm trying to create a downloadable PDF using wicked and Rails API. At the moment I can only get a PDF to download but the contents are empty and the file name is response.pdf.pdf.
This is the method I'm using to generate the PDF when a GET request is made to a specific score.
def download_pdf(score)
html = render_to_string(:action => :show, :layout => "pdf.html.erb", :template => "scores/show.pdf.erb", locals:{:score => score})
pdf = WickedPdf.new.pdf_from_string(html)
send_data(pdf,
:filename => 'test.pdf',
:disposition => 'attachment')
end
I came across this issue today. Quite late but will be useful for people looking for a solution to this.
You will have to inherit from ActionController::Base and not ActionController::API. Or you can override render_to_string with the following code.
def render_to_string(*args)
controller = ActionController::Base.new
controller.locale = I18n.locale
controller.render_to_string(*args)
end
reference: https://github.com/mileszs/wicked_pdf/issues/652

Getting ActionController::MissingFile when trying to force a download (vice open) with send_file

I am using rails 4.x with paperclip. I want to ensure that when the user clicks on a link to download a paperclip attachment, the file is downloaded instead of opening up.
The link I was using that would open or save depending on the browser configuration was
<%= link_to image_tag("save.gif", document.doc_file.url, :target => "_blank" %>
That link will sometimes open the file instead of downloading.
I set up a method of
helper_method :download
def download
#document= Document.find(39)
send_file ("http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf?1458068410"),
:filename => #document.doc_file_file_name,
:type => #document.doc_file_content_type,
:disposition => 'attachment'
end
I hardwired the url for testing. I had also tried with send_file ("http://localhost:3000#{#document.doc_file.url}") and send_file (#document.doc_file.url). NEither worked.
My link to that is
<%= link_to image_tag("save.gif"), download_path(document.id) %>
routes.rb has
match 'documents/download/:id' => 'documents#download',via: [:get, :post], :as => 'download'
When I click on the download link, I get an error of
ActionController::MissingFile in DocumentsController#download
Cannot read file http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf
Rails.root: C:/Users/cmendla/RubymineProjects/technical_library
Application Trace | Framework Trace | Full Trace
app/controllers/documents_controller.rb:17:in `download'
If I put the URL into an address bar on a browser, it works. i.e. `http://localhost:3000/images/39/Medical_Opportunity_EvaluationForm.pdf'
The send_file method accepts the physical location of the file on your server, not public URL. So something like the following should work, depending on where the PDF file actually resides:
send_file "#{Rails.root}/public/images/39/Medical_Opportunity_EvaluationForm.pdf",
:filename => #document.doc_file_file_name,
:type => #document.doc_file_content_type,
:disposition => 'attachment'
If this is a paperclip document, the path method should work:
send_file #document.doc_file.path,
:filename => #document.doc_file_file_name,
:type => #document.doc_file_content_type,
:disposition => 'attachment'
See the docs for more info.

Rails - pdfkit generate an image as header

I want to generate pdf file with an image as header in each page
def download_contract
#campaign = Campaign.find_by_id(params['campaign_id'])
html = render_to_string partial: 'campaign_shoppers/contract', locals: {contract: #campaign.contract}
kit = PDFKit.new(html, :header_right => **Here i want to display an image**)
send_data(kit.to_pdf, :filename => "contract_campaign_#{#campaign.title}.pdf", :type => 'application/pdf', :disposition => 'attachment')
end
But I can't generate pdf file with option 'header_right' is image( text is fine).
Any suggestion? Thanks a lot

How to display images in Rails without plugins like Paper Clip?

I'm trying to display images using my web application written in Rails. I've come across solutions like PaperClip, Attachment Fu etc; but they modify my data model and require to save the image through UI. The problem is that, the images content is not stored using Rails, but a Java Servlet Application. Is there a way to just display the blob content of image to my View.
-Snehal
class ImagesController < ApplicationController
caches_page :show
def show
if #image = Image.find_by_file_name(params[:file_name])
send_data(
#image.file_data,
:type => #image.content_type,
:filename => #image.file_name,
:disposition => 'inline'
)
else
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
end
end
end
map.connect "/images/*file_name", :controller => "images", :action => "show"
Or something like that.

Resources