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?
Related
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.
I've an app which works fine in development and on my current production server.
I want to move it to FREE heroku (basic config: 1 dyno, 1 worker).
Unfortunately, the pdf generation (using PdfKit) is ok BUT without the pictures defined in my CSS.
I've followed a lot of tips including:
http://blog.mattgornick.com/using-pdfkit-on-heroku
http://jguimont.com/post/2627758108/pdfkit-and-its-middleware-on-heroku
http://code-fu.pl/blog/2011/05/17/pdfkit-heroku
Thoughts?
Found a workaround but I am still eager to know a better option:
I duplicated my view: one dedicated for html, another for pdf.
I removed all css using pics and put it in a separate file, included only in the view dedicated for html
finally, I inserted the css in the view dedicated to the pdf:
.foo { background-image:url(<%= Rails.root %>/public/images/bar.png) }
Very Ugly but works so please tell me if you've better
It's probably an issue with the way the url's are specified in the css. As I recall, they should be file system absolute paths. What does your css look like?
Here is how I answered my needs with:
Just one single view file
Just one css file
The trick was to pass the proper base_url to the css file dynamically, given I expected a pdf or html.
I decided to use LESS. Style compiles css in a different manner, given the base-url I provide in the DOM. This base-url is generated by a helper.
Here were my steps:
changed my style.css to style.less
Added to my view:
<%= stylesheet_link_tag "style.less", :rel => "stylesheet/less" %>
<script id="base_url" type="text/javascript" data="<%= assets_path %>"></script>
<%= javascript_include_tag "less.min.js" %>
In my helper:
def assets_path
if request.fullpath.include? ".pdf"
"#{Rails.root.join('public',"images","pictos")}"
else
"#{request.protocol}#{request.host_with_port}/images/pictos"
end
end
and in my style.less:
#base_url: `document.getElementById('base_url').getAttribute('data')`;
.foo { background-image:~"url(#{base_url}/bar.png)" }
I am building a rails app to display test results for an application that is already written.
The application generates an xml file. I would like to have rails read the xml file, it's associated xsl file and render an html partial.
I'm able to get it to render it as a file using <% render :file => 'file_path_not_in_views/file.xml' %> and the contents of the text of the xml file is displayed, but none of the styling is applied.
Thank you.
a
I am trying to make a URL like Link Title from within my application. What is the file that I need to edit to link this URL to a controller and then a view?
Also, I am still working my way through this tutorial:
http://guides.rubyonrails.org/layouts_and_rendering.html
and I am wondering when they do something like this:
<%= stylesheet_link_tag "http://example.com/main.css" %>
is that supposed to live in the application.html.erb file or the index.html.erb file?
What is the file that I need to edit to link this url to a controller and then a view?
routes.rb
See the rails guide
is that supposed to live in the application.html.erb file or the index.html.erb file?
The simple answer is: application.html.erb, inside the head section. There are ways of injecting view template stuff into the head, but if you're just starting out, stick with application.html.erb.
Is it possible to define a .js.erb file as a partial? I have several .js.erb that have code that could just be moved to a common file like a partial, and then I would just call render on it.
How can I do this?
Same rules as normal partials: name the file beginning with _yourfile.js.erb and in your main file call it with <%= render partial: "yourjsfile" %>
Although, being javascript a programming language, you might want to think to refactor your code instead of resorting to this