Rails 6.1.4 Wicked PDF - ruby-on-rails

I am attempting to implement Wicked PDF (https://github.com/mileszs/wicked_pdf/) in a Rails 6.1.4 project and am continually getting the following error messages on the console.
My configuration is as simple as it gets. The controller looks like:
class Api::V1::ProductsController < Api::V1::BaseController
def datasheet
#product = Product.active.friendly.find params[:id]
respond_to do |format|
format.pdf {
render "products/datasheet", pdf: "datasheet-#{#product.name}"
}
end
end
end
and the datasheet.haml file looks like:
%html
%head
%body
= "This and that"
The file encoding for the datasheet.haml is UTF-8, but this shouldn't matter because the text contains no special characters.
The Gemfile has both wicked_pdf and wkhtmltopdf-binary.
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
I have executed the command rails generate wicked_pdf and it did generate the wicked_pdf.rb initializer, as expected.
I have tried putting Mime::Type.register "application/pdf", :pdf into the mime_types.rb file, but expect that this wouldn't matter because I am running Rails 6.
The response generated is:
We have another application running Rails 6.1.4 using wicked_pdf in, "ahem...", the exact same way successfully. Obviously something is different, I just cannot find out what...
Any suggestions on what I am overlooking? Your help is much appreciated!

Related

Wicked pdf, failed to load PDF document

I am trying to implement the gem wicked_pdf . After initial difficulties (fatal error (exception reentered)), I managed to start the server. I set up a pdf with the content "Hello world" for the test, but every time I want to open it, I get a "failed to load PDF document" notification
Controller
def index
respond_to do |format|
format.html
format.pdf do
render pdf: "index.pdf.haml",
layout: 'pdf.html.haml',
page_size: 'A4',
disposition: 'inline'
end
end
end
index.pdf.haml
Hello world
config/initializers/wicked_pdf.rb
# WickedPDF Global Configuration
#
# Use this to set up shared configuration options for your entire application.
# Any of the configuration options shown here can also be applied to single
# models by passing arguments to the `render :pdf` call.
#
# To learn more, check out the README:
#
# https://github.com/mileszs/wicked_pdf/blob/master/README.md
class WickedPdf
module PdfHelper
remove_method(:render)
end
end
WickedPdf.config = {
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: '/usr/local/bin/wkhtmltopdf',
# or
# exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.haml',
# Using wkhtmltopdf without an X server can be achieved by enabling the
# 'use_xvfb' flag. This will wrap all wkhtmltopdf commands around the
# 'xvfb-run' #command, in order to simulate an X server.
# use_xvfb: true,
}
config/initializers/mime_types.rb
# frozen_string_literal: true
# Be sure to restart your server when you modify this file.
# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
Mime::Type.register "application/pdf", :pdf
Link to Screenshot: https://drive.google.com/file/d/1ujt8ANq16SrmSHJk45APMXXxm1l5q5ic/view?usp=sharing
Ok, so I downgraded wicked_pdf to version 1.4.0 and I removed:
class WickedPdf
module PdfHelper
remove_method(:render)
end
end
From config/initializers/wicked_pdf.rb, and now it works.

compile in Rails 6 Webpacker doesn't make changes to send_data with methods defined in model

I recently upgraded to Rails 6 with webpack. This is when I started to notice this issue. I have a send_data method in a controller that works with either a csv or pdf (Prawn) format. Currently experiencing this issue with a csv.
In development, I made a method for self.to_csv in models/user.rb and used it to download some data into a CSV file. I deployed the file to my production server. And downloaded the file there.
Then I made a change to the columns that would be printed in that file. I saw the changes immediately in development. I deployed these changes to production, but still get the old columns. I checked the files on the server and they have the updates.
In Rails 5, I would see these changes immediately in production, too. Is there a way I can speed the server along here? Re-cache the files? Etc.
Here are my files.
In controllers/users_controller.rb:
def index
respond_to do |format|
format.csv {
send_data User.all.to_csv
}
end
end
In models/user.rb:
def self.to_csv
attributes = %w{first_name last_name id}
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |obj|
csv << [obj.first_name, obj.last_name, obj.id]
end
end
end
I originally had it just print first and last name to the CSV. That's what I still get in production.
And here is my deployment pipeline:
$ yarn install --check-files
$ rails db:migrate
$ RAILS_ENV=production rails assets:precompile
$ touch tmp/restart.txt
Running Rails 6.0.2.2, Webpack 4.42.0, and Ruby 2.5.5 on Passenger. Hosted by Dreamhost.
This worked for me and was thanks to max's suggestion in the comments.
I placed a stale? block around the send_data line and when I made an update it showed up instantly.
The new controllers/users_controller.rb looks like this:
def index
#users = User.all
respond_to do |format|
format.csv {
if stale?(#users)
send_data #users.to_csv
end
}
end
end
This block came from here:
https://thoughtbot.com/blog/take-control-of-your-http-caching-in-rails

roo gem file .xlsx does not exist

I use cloud9 ide to use ruby on rails!
I'm testing the gem 'roo' to bring excel file to my DB. before I do it, I wanted to test this gem work.
gem doc : https://github.com/roo-rb/roo
but there is a problem to bring file!
The error message is like this
IOError in MersmapController#index
file ../assets/test.xlsx does not exist
And here is my code!
require 'roo'
class MersmapController < ApplicationController
def index
xlsx = Roo::Excelx.new("../assets/test.xlsx")
#show = xlsx.info
end
end
and in index.erb
<h1> <%= #show %> </h1>
I test this path using my "images.jpg" (the image file)
when I write path of an image file in index.erb it definitely works!!
I tried
xlsx = Roo::Excelx.new("../assets/excel/test.xlsx")
xlsx = Roo::Excelx.new("../../app/assets/test.xlsx")
xlsx = Roo::Excelx.new("../../app/assets/excel/test.xlsx")
......
All the things!!
but finally I couldn't figure out what is the problem...
I appreciate if you help me out!!
You can use Rails.root to get the path name of your file:
xlsx = Roo::Excelx.new(Rails.root.join('app', 'assets', 'excel', 'test.xlsx'))
I remember that once happened to me, check if this solves your problem:
xlsx = Roo::Excelx.new(url_to_file, file_warning: :ignore)

Rails - Serve static CSV file as assets in Development with Thin

I'm simply trying to write to a CSV file in dev with thin, but I am continually getting an error that says
No such file or directory - http://localhost:3000/assets/tplan_log.csv
However, my config looks like this:
development.rb
config.serve_static_assets = true
And my controller looks like this:
def csv
#this returns a valid path
log_path = view_context.asset_path 'plan_log.csv'
#error occurs here
CSV.open(log_path) do |csv|
csv << ["row","row1","row2"]
end
respond_to do |format|
format.js {render json: #diaggroup.errors, status: :unprocessable_entity}
end
end
Rails 3.2.12
Edit: To clarify, I'm looking for an answer on how to serve this file in development so that it is accessible. I know it's being served with asset pipeline, but there is no get method associated with it, so it's not able to be accessed.
This ended up being an issue with reference.
I was using a reference such as:
http://path/to/file.jpg
Rails wants:
/path/to/file.jpg

How to install Wicked PDF gem in rails 3.0.0

Right now I am using Rails 3.0.0 version. I want to install Wicked pdf gem. I don't know how to install that gem. Please tell me that step by step procedure.
It's really easy!
Add this to Gem file!
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary', '~> 0.12.3'
Run Bundle install and run rails generate wicked_pdf
In config/initializers/mime_types.rb add this (or Uncomment)
WickedPdf.config = {
exe_path: '/usr/local/bin/wkhtmltopdf'
}
In your controller (show action)
def show
respond_to do |format|
format.pdf do
render :pdf => "file_name.pdf", :template => 'file_directory/file_name.html.erb', :encoding => 'utf-8'
end
format.html
end end
Create a erb.html file and put your pdf code in it.
In view you have to use the route controller#show for your link_to tag!
Good Luck.
use the command gem install wicked_pdf .See the documentation here. http://rubygems.org/gems/wicked_pdf & http://rubydoc.info/gems/wicked_pdf/0.7.9/frames
Reminder to restart you server after changing you config/initializers/mime_types.rb file. Other than that Afsanefda's answer worked for me.

Resources