Best pdf library for rails - ruby-on-rails

I have a requirement to create pdfs with tables. I started it by using prawn. But it was too slow and kept utilizing 100% CPU. Now I moved to wicked_pdf.
This is much faster than prawn but still could be faster. One of my friend recommended TCPDF.
I found rfpdf gem which is TCPDF plugin for rails. Have anyone here used it before? How fast is it?
I also found fpdf. Are they better than wicked_pdf?

You can use 'wicked_pdf' gem. It provides very good support to generate pdf using html code.
https://github.com/mileszs/wicked_pdf
In controller:
def show
#report = Report.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.pdf do
render :pdf => "report",:template => "reports/show"
end
end
end
Create a show.pdf.erb and you can write simple html.
<%= wicked_pdf_stylesheet_link_tag "application" %>
<h1> Report </h1>
<p>
<%= #report.details %>
.................................
</p>

Try PdfKIT, it's awesome and very simple to use. Here is a screencast for it.

You don't state the format of your source material. If you are starting from simple text documents, HTML, or other simple markup formats, then you may wish to have a look at Pandoc. This tool will let you convert simple document types into a number of publication-type formats, including PDF.
Since you have a requirement to produce tables in your PDF files, one easy option would be to create your documents in Markdown format, which includes simple rules for creating tables. See this cheat sheet for an example.
Finally, here is a blog post I read recently that discusses converting Markdown documents to PDF files.
Since you are working with Rails there also exists a lightweight wrapper for Pandoc that might prove useful.

Have a look also on another gem: https://github.com/igorkasyanchuk/rails_pdf
It's using chrome headless to create PDF.

Related

How do I render an SVG inline in Rails?

I have an SVG file that's part of a template located at:
vendor/theme/assets/icons/icon-1.svg
How do I render that inline in my view? render partial: path fails and says it can't find a partial.
In your view insert the following:
<%= render inline: Rails.root.join('vendor/theme/assets/icons/icon-1.svg').read %>
If you're going to be doing this multiple times, you may want to refactor this into a helper.
Also, think about the following:
Are you okay with dumping third party code directly into your view?
Is the vendor SVG file updated automatically without review?
Are you sure the vendor SVG file will never contain malicious code?

Slim templates, format.js template rendering in Rails 4

I've always worked in ERB and made heavy use of .js.erb files to render JS responses. I've recently fallen for Slim templates but I'm having a difficult time finding an equivalent to this that keeps files in the Slim format.
Assuming I'm executing the update action within my controller and the end looks like this:
respond_to do |format|
format.js {}
end
And let's pretend that I'm trying to just send alert('hello') as a response.
update.js works correctly and so does update.js.erb, naturally. If update.js.slim looks like this:
javascript:
alert('hello')
...it is sent as a response that looks like this:
<script type="text/javascript">(function() {
alert('hello');
}).call(this);
</script>
That won't work because the browser expects Javascript and that is an HTML file with script tags. Thanks, Slim. It DOES work if I use | to render plain text:
update.js.slim
|
alert('hi');
Everything after the pipe character shows up in SublimeText 2 color-coded and I still get access to Ruby code and variables within #{}. So far, this seems like my best and only option if I want to insist on keeping things in Slim format, but since it doesn't give me any of the benefits of Slim, I almost feel like just using .js.erb for these files might be better, since it has native support for this sort of thing.
Is there a better way of doing it?

How to register rb as a template handler in rails

Well since I am using a lot of helper methods in my view files and I avoid using html in most of my view files.
Example
myview.html.erb
<%=myhelper #myobject%>
so I end up using,the erb processing tags each time for each file.
<%=%>
I want to register .rb as a template handler or any other extention for that matter.
So my templates look like
myview.html.rb
myhelper #myobject
I am clueless on how to go ahead.
I found it,it seems railscasts already covered that part.
Its show notes,worth checking out.
https://github.com/railscasts/379-template-handlers/blob/master/store-after/config/initializers/ruby_template_handler.rb
Things without ruby are easy to read and render without those erb tags.

Textmate - render different types of code in different highlighting

I have an .html.erb document that has a javascript section at the bottom. Ordinarily Textmate recognizes the <script> tag and converts everything inside to its Javascript(Rails) format.
To cut down on <script> tags I enclosed this js code in:
# lots of html.erb above...
<% content_for :footer_js do %>
lots of javascript
<% end %>
How can I tell Textmate to show the correct highlighting for this Javascript(Rails) section of code?
https://gist.github.com/1018085/1049531ca917fa71141dc5e3b7bd7016adaef37d Shows how to do it. You'll just need to change the regex that matches :inline_js to your :footer_js
I have also looked for this answer, but after much research, I'm not sure it's possible unless you edit a bundle to add this functionality.

Rails and markdown and editor

Has the mark down editor been ported to a rails app? (the same one used on this SO)
What about parsing the markdown markup?
Most WYSIWYG editors should be pretty easy to integrate into your app without the need for Rails-specific gems/plugins. Here's an editor that supports Markdown:
http://livepipe.net/control/textarea
You could also try WYSIHAT if you don't mind putting a little work into customizing your editor:
https://github.com/37signals/wysihat
http://www.80beans.com/blog/development/2009/10/01/introducing-wysihat-engine/
For just parsing the markdown markup, you can try RedCarpet gem.
Also according to this configuration example, just need to add a helper in ./app/helper/application_helper.rb like:
def markdown(text)
render_options = {
# add options here
renderer = Redcarpet::Render::HTML.new(render_options)
extensions = {
# add extensions here
}
Redcarpet::Markdown.new(renderer, extensions).render(text).html_safe
end
Then render any text with:
<%= markdown YOURTEXT %>

Resources