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
Related
i know how to render it in webapp but i'm supposed to generate pdf through an api call , so i need to send url of the pdf generated instead of the pdf itself ( thats what the mobile developer is asking for ) , is there any way to do it ?
like:
respond_to do |format|
format.pdf do
pdf = CustomCertificatePdf.new(current_user, tutorials)
url = pdf.link
# send_data pdf.render, filename: "custom_certificate_#{current_user.first_name.downcase}_#{current_user.last_name.downcase}.pdf",
# type: "application/pdf",
# disposition: "inline"
render :json => {url: url}
end
end
even your slight hint will be appreciated , thanks :)
save pdf to public folder or upload to another server( S3, google drive, dropbox, ...)
then use that link in api controller
if you save pdf file in public folder, the link should be your host name + relative path with public folder
ex:
file_path = "/webapp/public/pdf/custom_certificate.pdf"
the link should be
http://yourhostname.com/pdf/custom_certificate.pdf
If you need protect your file, you should write another controller to serve it with authentication
This is what i ended up doing
pdf = CustomCertificatePdf.new(current_user, tutorials)
#filename = File.join("custom_certificate_#{current_user.first_name.downcase}_#{current_user.last_name.downcase}.pdf")
pdf.render_file #filename
current_user.cust_cert = File.open("custom_certificate_#{current_user.first_name.downcase}_#{current_user.last_name.downcase}.pdf")
current_user.save!
render :json => {url: "#{current_user.cust_cert}"}
in user model
has_mongoid_attached_file :cust_cert,
:default_url => "",
:path => ':attachment/:id/:cust_cert',
:storage => :s3,
:url => ':s3_domain_url',
:s3_credentials => File.join(Rails.root, 'config', 's3.yml')
validates_attachment_content_type :cust_cert, :content_type => [ 'application/pdf' ], :if => :cust_cert_attached?
def cust_cert_attached?
self.cust_cert.file?
end
I need a quick tip on something which seems really simple. I have some pictures inside private folder and would like to display them inside my View.
The only solution I found was this:
def show
send_file 'some/image/url', :disposition => 'inline', :type => 'image/jpg', :x_sendfile => true
end
I've read that :disposition => 'inline' should not trigger image download and allow me to display it inside my View. The problem is that each time I trigger show action, image download is automatically activated and it is automatically downloaded. View for show action is not displayed.
How can I display that image inside my View? Thank you.
The way I do it, and I'm not saying it's perfectly by the book, is I make a root for images and an action in the controller to render it.
So, for instance, in routes.rb
match '/images/:image', to: "your_controller#showpic", via: "get", as: :renderpic
In your controller:
def showpic
send_file "some/path/#{params[:image]}.jpg", :disposition => 'inline',
:type => 'image/jpg', :x_sendfile => true # .jpg will pass as format
end
def show
end
And in your view
<img src="<%= renderpic_path(your image) %>">
Here is a working example, with fewer parameters on "send_file"
def showpic
photopath = "images/users/#{params[:image]}.jpg"
send_file "#{photopath}", :disposition => 'inline'
end
I think the problem is type. From documentation:
:type - specifies an HTTP content type
So the proper HTTP content type should be image/jpeg instead of image/jpg, as you can see here. Try with:
:type => 'image/jpeg'
You also can list all available types coding Mime::EXTENSION_LOOKUP into a rails console.
Example:
Controller
class ImagesController < ApplicationController
def show_image
image_path = File.join(Rails.root, params[:path]) # or similar
send_file image_path, disposition: 'inline', type: 'image/jpeg', x_sendfile: true
end
end
Routes
get '/image/:path', to: 'images#show_image', as: :image
Views
image_tag image_path('path_to_image')
You would need to have the view use an image_tag to display on the view.
Similar question was raised here: Showing images with carrierwave in rails 3.1 in a private store folder
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
I am using Rails 2.3.11. I created a UserMailer with following methods:
def rsvp_created(user, rsvp, pdf_file)
setup_email(user)
content_type "multipart/mixed"
#subject << "Your RSVP for #{rsvp.ticket.holiday.title}"
#body[:rsvp] = rsvp
attachment :content_type => 'application/pdf',
:body => File.read(pdf_file),
:filename => "#{rsvp.confirmation_number}.pdf"
end
def rsvp_cancelled(user, rsvp)
setup_email(user)
content_type "text/html"
#subject << "Cancelled RSVP for #{rsvp.ticket.holiday.title}"
#body[:rsvp] = rsvp
#body[:holiday_url] = APP_CONFIG['site_url'] + holiday_path(rsvp.ticket.holiday)
end
protected
def setup_email(user)
#recipients = "#{user.email}"
#from = APP_CONFIG['admin_email']
#subject = "[#{APP_CONFIG['site_name']}] "
#sent_on = Time.now
#body[:user] = user
end
The rsvp_cancelled works fine and sends email properly. But the rsvp_created email, which has an attachment, doesn't work properly. It sends the email with the attached file but doesn't render any text. Any one faced this issue before or know how I can resolve it?
Thanks
With Rails 2.x, for some reason or the other you need to define all the parts for the HTML to appear.
def rsvp_created(user, rsvp, pdf_file)
setup_email(user)
content_type "multipart/mixed"
#subject << "Your RSVP for #{rsvp.ticket.holiday.title}"
part :content_type => 'multipart/alternative' do |copy|
copy.part :content_type => 'text/html' do |html|
html.body = render( :file => "rsvp_created.text.html.erb",
:body => { :rsvp => rsvp } )
end
end
attachment :content_type => 'application/pdf',
:body => File.read(pdf_file),
:filename => "#{rsvp.confirmation_number}.pdf"
end
Thankfully it appears this is not the case in Rails 3.x.
I was trying to do the same sort of thing, but following Douglas' answer above, kept getting corrupted pdf attachments. I was finally able to resolve the issue by reading the pdf file in binary mode:
attachment :content_type => 'application/pdf',
:body => File.open(pdf_file, 'rb') {|f| f.read}
:filename => "#{rsvp.confirmation_number}.pdf"
I was able to get mine working with Douglas' answer but was also able to get it working in one line. I was using a template but this also works by substituting "rsvp" for the render_message method.
part :content_type => "text/html",
:body => render_message("template_name", { :symbol => value } )
I'm trying to create a view with a download link to download the html source?
#Peter 's solution worked for me. Here is a code sample:
View:
<%= link_to 'download this page', object_path(#object, :download => true) %>
Controller:
def show
# ...
if params[:download]
send_data(render_to_string, :filename => "object.html", :type => "text/html")
else
# render normally
end
end
You can use render_to_string instead of render, which will give you the page, then to download it use send_data.
More on render to string here, and more on send_data here.