RAILS - Generate PDF from HTML with GROVER - ruby-on-rails

I want to use GROVER to export same ERB/HTML pages from my application as PDF.
It works, but the generated PDF seems to be missing the styles and formatting, no CSS seems to be processed.
Here my code within the controller:
html_string = render_to_string(
{
template: 'users/show.html.erb',
locals: { id: params[:id] }
})
pdf = Grover.new(html_string, format: 'A4').to_pdf
respond_to do |format|
format.html
format.pdf do
send_data(pdf, disposition: 'inline', filename: "Show_ID_#{params[:id]}", type: 'application/pdf')
end
end
My question is, how I can persuade GROVER also to process the CSS files ?

I had the same problem where my layout was rendered without the CSS. I was able to resolve the issue by adding the display_url option in Grover.
In local development this would be for example:
pdf = Grover.new(html_string, format: 'A4', display_url: "http://localhost:3000").to_pdf
You need to change your display_url depending on your environment.
If this does not work, make sure the html_string that you are rendering actually includes the correct markup. You can render out the html_string as HTML to verify all markup is included.

Related

How to render excel sheet using erb template used by wicked_pdf in ruby on rails?

I am using wicked_pdf to render a PDF document; it works like a charm. The template (.erb file) used by wicked_pdf contains multiple HTML tables which are rendered perfectly on the PDF.
Now, I would like to just reuse the .erb template to render an excel spreadsheet. How can I achieve this?
If the HTML table is simple enough, you can render the html.erb response with an Excel extension & content type, and it should open and work in Excel.
def show
respond_to do |format|
format.pdf do
render pdf: 'mypdf', template: 'show.html.erb'
end
format.xlsx do
render template: 'show.html.erb',
content_type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
end
end
end
Otherwise, you'll likely need to author a specific template, using something Axlsx

Rails automatically adds ".csv" to download name for format CSV, but not for others (e.g. ".xlsx")

Users can download data from my Rails project in CSV or XLSX format.
Here's the action:
def index
respond_to do |format|
format.xlsx { render xlsx: 'index', filename: filename_for_export(#project, export_type, :xlsx) }
format.csv { render csv: collection, filename: filename_for_export(#project, export_type, :csv) }
end
end
private
def filename_for_export(project, type, format)
"#{project.customer} - #{project.name} (#{type}, #{t 'org.name'}, #{Date.today.to_s :db}).#{format}"
end
Interestingly, when opening .csv, Rails seems to add .csv again to the filename (which already has the format in it).
When opening .xlsx, this doesn't happen:
Interestingly, when removing the file extension from the generated name, then both have one single correct extension, although XLSX shouldn't have any extension now (in my opinion).
It looks like you are forcing the extension out for xlsx files by using render xlsx: 'index'.
If you change that line to render xlsx: 'index.xlsx', you should get a consistent behavior. I imagine that you won't have to set the extension in filename_for_export anymore.

Wicked_PDF/wkhtmltopdf different font and page width sizes from pdfs in different controllers

Setup: Rails 4.1.1 application using gem 'wicked_pdf', '~> 0.11.0' and a manually installed wkhtmltopdf binary version 0.12.1 (with patched qt)
I have two controllers generating similar pdf reports which share several pages. I have partials for the pages that are shared between the two report types.
controller code:
# GET /assessments/1
def show
respond_to do |format|
format.html
format.pdf do
render pdf: "Individual_Report-#{Date.today}",
disposition: "inline",
layout: "pdf",
dpi: "300",
encoding: "UTF-8",
show_as_html: params[:debug].present?
end
end
end
# GET /reports/1
def show
respond_to do |format|
format.html
format.pdf do
render pdf: "Team_Report-#{Date.today}",
disposition: "inline",
layout: "pdf",
dpi: "300",
encoding: "UTF-8",
show_as_html: params[:debug].present?
end
end
end
view code:
# layouts/pdf.slim
doctype html
html
head
meta charset="utf-8"
title
= content_for?(:title) ? yield(:title) : 'Main Title'
== wicked_pdf_stylesheet_link_tag "application", :media => 'all'
== wicked_pdf_stylesheet_link_tag "print", :media => 'all'
== wicked_pdf_javascript_include_tag 'vendor/modernizr'
== csrf_meta_tags
body data-action=action_name data-controller=controller_name data-format="pdf"
main
== yield
== wicked_pdf_javascript_include_tag 'application'
javascript:
window.jQuery || alert("false");
# assessments/show.pdf.slim
= render "shared/cover", m: #assessment, t: "Individual"
= render "shared/about"
= render "shared/team", report: #report
# etc. etc. this is where the reports differ
# reports/show.pdf.slim
= render "shared/cover", m: #report, t: "Team"
= render "shared/about"
= render "shared/team", report: #report
# etc. etc. this is where the reports differ
However the report from the assessments controller is generating a pdf that has a slightly narrower content width and accordingly smaller text size. Rendering the html with the debug param gives identical pages and inspecting the css shows no differences. I'm using Foundation as a css framework but I don't think that is important.
I read some about wkhtmltopdf's --disable-smart-shrinking option but I'm not sure if that is used by wicked_pdf and if so, how to/if it can be changed in the wicked_pdf.rb initializer. I also set the dpi and different values to no effect. Adjusting the page margins helps some but doesn't fix the underlying issue. I can add screenshots if needed but I think the problem should be clear enough without them.
Thanks for any help on how to fix the issue or adjust wkhtmltopdf options using wicked_pdf.
I figured this one out by accident. It seems the issue was being caused by something else I was doing with Chart.js and a < canvas > element that was sized larger than it's container.
I don't precisely understand what was causing the issue. It may have also been connected to some CSS from the Foundation framework which I am also using. Nevertheless, I'll post this answer in case it helps others solve the same issue.
I won't accept this as the answer since it doesn't specifically answer the question and doesn't address how to
"adjust wkhtmltopdf options using wicked_pdf."
If anyone else can reply to that I'll accept it.

Rails Controller - format.png

Trying to get a controller block in rails to work. When responding to a url I want to show the .png file associated with the model.
respond_to do |format|
format.html { render :blank }
format.json { render json: #play }
format.png do
send_data data, :type => "image/png", :filename => 'screenshot.png'
end
Data is working fine and is a 200Kb file. The issue is when I go to the url:
http://localhost:8080/plays/testing1.png
it brings me to a blank page (I created show.png.erb and have nothing in there). Not sure how to get the png file to either be shown inline or downloadable.
Thanks for any help
Try using Conent-Disposition, you can either set it to attachment or inline.
Old question but i've been struggling with the same problem for a few hours so in-case anyone finds this in the future:
I was also getting the same problem where a document was being generated in Chrome with a broken image and tag i.e. <img style="-webkit-user-select: none" src="http://localhost:3000/open/28">. Also in Firefox I was getting an error saying there was a problem with the image.
The solution: I changed send_data to send_file which gave me the following code:
respond_to do |format|
format.png { send_file img ,type: "image/png", disposition: 'inline' }
end
More info about send_file here
After that, the routes responded by serving the PNG images without the broken image tags.
Hope that helps someone out.

Is it possible to render a js file with Rails and minify the output?

I'm generating some js output for each user using a template named example.js.erb. This is basically a javascript file where I have to put in the user's unique details. I'm using this inside of a widget for the user.
Is there any way to minify the js as it's being rendered? Right now the javascript is being rendered correctly, but it's in it's long form and I'd like to minify the output in order to reduce file size and increase download speed.
Rails Javascript compression/minification on respond_to javascript response?
respond_to do |format|
format.js { self.response_body = minify(render_to_string) }
end
Try something like this, in your controller:
def action_name
js_code = render_to_string 'your_js_code_view'
js_code.gsub!(/\r\n|\r|\n/, '') #and/or any other minimization you like
render 'the_view_wich_will_show_the_minimized_js_code'
end
For Rails 4:
render js: Uglifier.new.compile(render_to_string)
For Rails 5:
render js: Uglifier.new(harmony: true).compile(render_to_string)

Resources