Rails wicked_pdf, convert page as pdf - ruby

I have a normal Rails layout which is my normal Site.
I want a Button on this page to convert/transform this page into
a PDF file.
I want to do it with this gem:
https://github.com/mileszs/wicked_pdf
I added gem 'wicked_pdf' to my Gemfile, but now how can I convert the actual page?

Here's what I did to use wicked_pdf:
# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary' # if you need the binary too
Then run
$ bundle install
$ (bundle exec) rails g wicked_pdf # generates the initializer
Then you can configure global settings in config/initializers/wicked_pdf.rb as stated in the wicked readme on github.
Rails 4 already knows the pdf mime type, but in older versions you'll probably need to register pdf as a mime type:
# config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
In your controller
respond_to do |format|
format.html
format.pdf do
render pdf: "your-filename"
end
end
Then you put a view under app/views/<your-view>/<controller-action>.pdf.erb with the content you wan't.
To reference this by a link you can do something like:
= link_to "Get PDF", your_path(format: :pdf)
Does this solve your question?

Please follow the below link. Its a details tutorial, this will make your life easier:
http://dchua.com/2014/10/30/generate-pdfs-with-html-templates-in-rails/
Note
If you don't want to have your pdf views separately and want to use the same html view, then please use
template: invoices/show.html.erb
instead of
template: invoices/show.pdf.erb

Related

Controlling a Ruby script from the web

I have a simple script written in Ruby that can start and stop an executable. I would like to have two buttons on a simple web page, start and stop, and execute the code on the machine the page is hosted from. What would be the easiest/lightest way of doing it?
I would suggest using Sinatra for such a small tasks. Let's start with Gemfile, if you're using bundler:
source 'https://rubygems.org'
gem 'sinatra'
gem 'slim'
Then just run "bundler install" or just install these gems manually. Let's write our tiny web server, create myapp.rb with following content:
require 'sinatra'
get '/' do
slim :index
end
post '/start' do
puts "Starting..."
redirect '/'
end
post '/stop' do
puts "Stopping..."
redirect '/'
end
This basically creates three handlers for paths "/", "/start", "/stop". "/" renders slim html template (we will get there soon), "/start" and "/stop" for now just print a text into STDOUT and redirects you back to "/". Let's create a folder "views" and file "views/index.slim" in it:
doctype html
html
head
title Simple Ruby web interface
body
form method="post" action="/start"
input type="submit" value="Start"
form method="post" action="/stop"
input type="submit" value="Stop"
This will be rendered into html with two forms with buttons, which will post into /start and /stop on submit. You can run it with just
ruby myapp.rb
This will start a web server on port 4567. Just go to localhost:4567 and press the buttons. Now you can just add actions you need to perform instead of puts "Starting..." and puts "Stopping..." and customize this very basic look of your webapp.

Middleman localized template not loading only for 1 locale

Im using middleman to render index.html.erb (int), index.us.html.erb (us), index.br.html.erb (br) to load localized content.
It works fine with all locales except for BR.
I have created locales/br.yml
---
br:
hello: "Hello"
Also, my config.rb file has proper i18n activation.
# Country specific pages ('source/html')
activate :i18n,
:mount_at_root => :int,
:templates_dir => "html",
:langs => [:int, :in, :us, :br]
When I visit http://localhost:4567/br it load int template for br
Also, please note if Im using the build source, it works perfectly fine. But I in development environment while running server with bundle exec middleman BR doesn't load correct template.
Please let me know if you need any more code from my side.

Padrino Tutorial: Can't Modify Frozen String (Runtime Error)

I am following the Padrino tutorial from here:
https://www.padrinorb.com/guides/blog-tutorial
I am copy and pasting the commands but I quickly ran into an error I don't understand:
$ padrino g controller posts get:index get:show
create app/controllers/posts.rb
create app/views/posts
apply tests/shoulda
/Users/waprin/.rvm/gems/ruby-2.1.0/gems/padrino-gen-0.12.4/lib/padrino-gen/generators/controller.rb:66:in `prepend': can't modify frozen String (RuntimeError)
from /Users/waprin/.rvm/gems/ruby-2.1.0/gems/padrino-gen-0.12.4/lib/padrino-gen/generators/controller.rb:66:in `create_controller'
This might be a bit late, but in case anyone else runs across this error (and because I just worked through the same tutorial) I'll post anyway...
It looks like there's an issue when generating controllers if a test component is specified. In this case you're using shoulda, but the same happens when using rspec and maybe others. It's been reported as a bug: https://github.com/padrino/padrino-framework/issues/1850 and has been fixed, but isn't yet part of a stable release.
One option to fix this would be to change your Gemfile to work with the latest from their github repo. To do this delete your GemFile.lock file, and comment out the line under 'Padrino Stable Gem' in your GemFile:
gem 'padrino', '0.12.4'
then uncomment the line under 'Or Padrino Edge':
gem 'padrino', :github => 'padrino/padrino-framework'
then re-run bundle install.
Of course, you'll no longer be running the stable release, and that may come with other trade-offs.
As a side-note, I believe that the guide on that page is fairly out of date. I also needed to replace:
get :index do
#posts = Post.all(:order => 'created_at desc')
render 'posts/index'
end
with:
get :index, :provides => [:html, :rss, :atom] do
#posts = Post.order('created_at desc')
render 'posts/index'
end
in the Post controller as the active record interface has changed since the time that the guide was written.
I was able to sole this problem by simply going to padrino gem path.
For me it was:
/Users/ahmadhassan/.rvm/gems/ruby-2.2.0/gems/padrino-gen-0.12.4/lib/padrino-gen/generators
open controller.rb and change line number 61:
path = #controller
to
path = #controller.dup

How to install 'Ckeditor' for 'Active Admin' with 'Rails 3.1'

I looking for how to install Ckeditor for Active_Admin
I already installed Ckeditor and I have put this line in my 'initializers/active_admin.rb' file.
# To load a javascript file:
config.register_javascript
'ckeditor/config.js'
And the line
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
in my 'config/application.rb'
In the begining the 'config.js' file wasn't loaded because when I install Ckeditor gem it creates a folder named Ckeditor in public/javascripts but with rails 3.1, I had to move this folder into assets/javascripts...
That's what I've done.
Now the file "config.js' is loaded.
But now I don't know what I have to do next
Rails 3.2
activeadmin-0.4.3
in 'initializers/active_admin.rb' file.
config.register_javascript 'ckeditor/ckeditor.js'
config.register_javascript 'ckeditor/init.js'
in form
= f.cktext_area :text
If your're using the assets pipeline with external host server you have to hack a little bit.
Your assets/javascripts/active_admin.js should look like this:
//= require active_admin/base
//= require_self
//= require ckeditor/ckeditor
//= require ckeditor/init
var CKEDITOR_BASEPATH = 'http://assets.yourdomain.com/assets/ckeditor/';
Then you're good to go.
Add
var CKEDITOR_BASEPATH = '/assets/ckeditor/';
to the start of your ckeditor.js file.
I believe you are having an issue with the new location of the files in rails 3.1. Make sure all your ckeditor files are located in the javascript/ckeditor folder (or modify the link accordingly).
See http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Specifying_the_Editor_Path
You can also use in your forms
f.input :name, :as => :ckeditor
There is a page in the oficial wiki for this task.

How do I download a picture using Ruby?

I want to download this picture using Ruby. How do I do that?
http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg
I am using Mac OS.
require "open-uri"
open("your-url") {|f|
File.open("whatever_file.jpg","wb") do |file|
file.puts f.read
end
}
Try using the Mechanize gem:
Start with: gem install mechanize to install Mechanize.
Then:
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
link = 'http://www.asite.com/pic.jpg'
agent.get(link).save "images/pic.jpg"
EDIT:
To make things cleaner I would suggest you use the File's name itself when you save the image. I had an issue saving bulk images because they were not formated correctly. The images were saved like this:
#pic.jpg(1)
#pic.jpg(2)
#etc.
Thats why you should probably use the image name as the file name like this:
agent.get(src).save "images/#{File.basename(url)}"
File.basename takes the image url and just returns the the actual image name:
File.basename("http://www.asite.com/pic.jpg")
# returns the image name
pic.jpg
If you want to download all the images on one page, you can use the image_downloader gem:
require 'rubygems'
require 'image_downloader'
downloader = ImageDownloader::Process.new('www.test.com','img_dir/')
downloader.parse(:any_looks_like_image => true)
downloader.download()
There is a gem called down which is simple to use.
Firstly, install this gem by gem install down.
Then:
require "down"
file = Down.download("http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg")
You can read more on this gem in its github repo https://github.com/janko-m/down.
The easiest way would be to require open-uri and use that with the previous answer or use the also supplied Net::HTTP module with its get method.
To make things cleaner I would suggest you use the File's name itself when you save the image. I had an issue saving bulk images because they were not formated correctly. The images were saved like this:
#pic.jpg(1)
#pic.jpg(2)
#etc.
Thats why you should probably use the image name as the file name like this:
src = 'http://www.asite.com/pic.jpg'
agent.get(src).save "#{folder}/#{File.basename(src)}"
File.basename takes the image url and just returns the the actual image name:
File.basename("http://www.asite.com/pic.jpg")
# returns the image name
pic.jpg
%x(wget http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg)
or
`wget http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg`
The same way you download anything else. Net::HTTP

Resources