PDF could not be generated!” with wkhtmltopdf in rails - ruby-on-rails

I am using wkhtmltopdf(0.10.0 rc2) AND Wicked_pdf(0.9.4) for generating pdf report.
I am getting below error
Failed to execute:
"/usr/bin/wkhtmltopdf" -q --footer-left "[page]" --footer-right "Powered by AMP" --footer-font-size 7 --margin-top 5 --margin-bottom 10 --margin-left 10 --margin-right 10 --page-size "A4" "file:////tmp/["wicked_pdf", ".html"]29083-0" "/tmp/["wicked_pdf_generated_file", ".pdf"]29083-0"
Error: PDF could not be generated!
my controller:
render :pdf => "#{#note_name}",:layout => '/layouts/pdf_template.html.erb', :template=>'/leasing_activity/lease_act_pdf.html.erb'
gemfile:
gem "wicked_pdf","0.9.4"
gem "wkhtmltopdf-binary"
initializers/wicked_pdf.rb:
WickedPdf.config = {
:exe_path => '/usr/bin/wkhtmltopdf'
}

Related

Paperclip 5.1.0 - Rails 5 - appears missing, after the image upload

if I try to upload a picture message appears:
"New Content
1 error prohibited this content from being saved:
That cover has contents are not what they are reported to be "
and then shows "missing"
in Git Bash, appears:
"[Paperclip] Content Type Spoof: Filename goku.jpg (image / jpeg from Headers, [" image / jpeg "] from Extension), content type discovered from file command:. See documentation to allow this combination.
   (0.0ms) begin transaction
Command :: -b --mime file "C: /Users/xxxxxx/AppData/Local/Temp/1151fe66500e3084eef890162322a11020170115-9100-1xumnlr.jpg"
[Paperclip] Content Type Spoof: Filename juegos-vestir-goku.jpg (image / jpeg from Headers, [ "image / jpeg"] from Extension), content type discovered from file command:. See documentation to allow this combination.
   (0.0ms) rollback transaction
  Rendering contents / new.html.erb Within layouts / application
  Rendered contents / _form.html.erb (4.0ms)
  Rendered contents / new.html.erb Within layouts / application (30.0ms)
  Rendered layouts / _header2.html.erb (2.0ms)
  Rendered layouts / _footer.html.erb (0.0ms)
Completed 200 OK in 627ms (Views: 511.2ms | ActiveRecord: 0.0ms) "
Also when I installed paperclip appeared the message:
"Post-install message from paperclip:
##################################################
# NOTES FOR UPGRADING FROM 4.3.0 OR EARLIER #
##################################################
Paperclip is now compatible with aws-sdk> = 2.0.0.
If you are using S3 storage, aws-sdk> = 2.0.0 requires you to make a few small
changes:
* You must September the `s3_region`
* If you are Explicitly setting permissions anywhere, such as in an initializer,
  notes That the format of the permissions changed from using an underscore to
  using a hyphen. For example, `: public_read` needs to be changed to
  `Public-read`.
For a walkthrough of upgrading from 4 to 5 and aws-sdk> = 2.0 you can watch
http://rubythursday.com/episodes/ruby-snack-27-upgrade-paperclip-and-aws-sdk-in-prep-for-rails-5 "
perform operations to install paperclip:
- Downloaded magemagick for windows
- Insert in Gemfile gem "paperclip", "~> 5.0.0"
- Bundle install
- In the app >> model >> content.rb I entered:
has_attached_file: cover, styles {medium: "300x>" thumb "100x>"}
validates_attachment_content_type: cover, content_type: /\Aimage\/.*\z/
- Rails generate paperclip content cover
- Rake db: migrate
- I added to my code in the app> views> contents> _form,:
 <%= form_for #content, html: { multipart: true } do |f| %>
<Div class = "field">
    <% = F.label: cover%>
    <% = F.file_field: cover%>
  </ Div>
- App> controllers> content, I entered:
def content_params
      params.require (: content) .permit (: title: description: price,: cover)
end
- App> views> content> show I entered:
<% = # Image_tag content.cover.url (: thumb)%>
- App> views> content> index> I entered:
<Td> <% = image_tag content.cover.url (: medium)%> </ td>
Versions used:
-Win10 x64
-ruby 2.3.3p222
-Rails 5.0.1
-paperclip 5.1.0
Tests carried out, but not decisive:
Added in app> model> content.rb:
:default_url => "/images/:style/missing.png"
Added in config / environments / development.rb:
Paperclip.options [: command_path] = "/ usr / local / bin /"
Paperclip.options [: command_path] = 'C: \ Program Files (x86) \ GnuWin32 \ bin
Added in - Added in app> model> content.rb:
Paperclip.options [: command_path] = 'C: \ Program Files (x86) \ GnuWin32 \ bin; C: \ Program Files \ ImageMagick-7.0.4-Q16 '
Paperclip.options [: swallow_stderr] = false

Rails - Issues Downloading S3 File using Fog/Carrierwave

I have a Rails application where I am storing a file on S3 using Fog/Carrierwave. When I go to download the file, I do not get the expected results. My code is the following:
v_file_name = "https://s3.amazonaws.com/{bucket}/{file}.pdf"
data = open(URI.parse(URI.encode(v_file_name)))
send_data data, :type => 'application/pdf', :disposition => 'attachment', :filename => "{filename}.pdf"
I expect to get the download file dialog, but I get redirected in the browser to a page that begins with:
%PDF-1.3 %���� 1 0 obj << /Creator /Producer >> endobj 2 0 obj....
And ending in:
..... << /Size 10 /Root 2 0 R /Info 1 0 R >> startxref 25361 %%EOF
This code had previously been working, but has suddenly stopped working.
Any help would be greatly appreciated!
Found the error. Problem was that I had implemented Turbolinks mid-project. Apparently Turbolinks breaks send_file & send_data in Rails. You can disable Turbolinks by adding "data-turbolinks='false'" to the link. This fixed it!

PDFkit gem table of contents

Using version 0.8.2 of the PDFKit gem, passing in toc as indicated in the docs throws an error:
format.pdf {
html = render_to_string(:action => 'pdf.html.haml')
kit = PDFKit.new(html, {toc: true})
send_data kit.to_pdf, :filename => "thing.pdf", :type => 'application/pdf'
}
The generic error, which I'm showing so you can see that toc is being passed as wkhtmltopdf requires, and as the specs of the PDFkit gem suggest, without the --:
command failed (exitstatus=0): /Users/Emma/.rvm/gems/ruby-2.2.1/bin/wkhtmltopdf --page-size Letter --margin-top 0.75in --margin-right 0.75in --margin-bottom 0.75in --margin-left 0.75in --encoding UTF-8 toc - -
You can try
format.pdf {
html = render_to_string(:action => '<controller_name>/action_name')
kit = PDFKit.new(html.to_s, toc: '')
send_data kit.to_pdf, :filename => "thing.pdf", :type => 'application/pdf'
}

Paperclip AV Transcoder not working on remote server

I am able to upload videos locally. The videos are processed using paperclip and all the meta data is saved correctly, as well. When I tried to upload a video using our remote server, I received the error:
Av::UnableToDetect (Unable to detect any supported library)
I have installed ffmpeg using LinuxBrew. It says everything is installed correctly (checking which brew and which ffmpeg, as well as checking if the gem is appropriately installed).
When I have styling in my model for the video (which is what enables the meta information to be stored and to have control over how the video is uploaded) it doesn't work remotely.
has_attached_file :video, path: "/posts/:id/:style.:extension",
:styles => {
:medium => { :geometry => "493x877", :format => 'flv' },
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 },
# :mobile => {:geometry => "640X480", :format => 'mp4', :streaming => true}
}, :processors => [:transcoder]
However, when I remove this from my model and have:
has_attached_file :video, path: "/posts/:id/:style.:extension"
The video is uploaded to S3 (without the data or styling that I need).
Any help would be greatly appreciated. I think AV is having trouble finding ffmpeg but I am not sure why or how to go about fixing it. Thanks in advance for any advice.
I had the same issue just this past week - Try this!
Video model:
has_attached_file :video, styles: {
:medium => {
:geometry => "640x480",
:format => 'mp4'
},
:thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
}, :processors => [:transcoder]
validates_attachment_content_type :video, content_type: /\Avideo\/.*\Z/
Make sure you already bundled:
gem 'paperclip', '~> 4.3.1'
gem 'aws-sdk', '< 2.0'
gem 'paperclip-av-transcoder'
gem "paperclip-ffmpeg", "~> 1.2.0"
Run the paperclip migration:
rails g paperclip model video
Be sure to add in post_controller.rb:
private
def bscenes_params
params.require(:post).permit(:video)
end
Upload form:
<%= f.file_field :video %>
Show page:
<%= video_tag bscene.video.url(:medium), controls: true, style: "max-width: 100%;" %>
At this point you should get this error:
Av::UnableToDetect (Unable to detect any supported library):
For Mac
Go to your terminal and type in:
brew options ffmpeg
Then run the following to install ffmpeg:
For older versions of brew recipe:
brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libas
For newer versions of brew recipe:
brew install ffmpeg --with-fdk-aac --with-sdl2 --with-freetype --with-frei0r --with-libass
For Linux Mint / Ubuntu / Debian based Linux
Open a terminal (Ctrl + Alt + T) and execute following commands one by one to install ffmpeg.
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install ffmpeg
At this point Video uploads will work locally
Now for remote uploads you will need to setup https://devcenter.heroku.com/articles/buildpacks
This should now bring you to your error
Av::UnableToDetect (Unable to detect any supported library)
You will need to create a Procfile in the root of your app directory more information about Procfile here: https://devcenter.heroku.com/articles/procfile
touch Procfile
Hope this helps!

Wicked PDF RuntimeError

I have installed Wicked PDF on rails 4, and now I have this issue:
RuntimeError in ClientsController#show
Error: Failed to execute: ["C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe", "--footer-center", "Center", "--footer-left", "Left", "--footer-right", "Right", "file://C:/Users/Rashid/AppData/Local/Temp/wicked_pdf20141017-9664-18xoryq.html", "C:/Users/Rashid/AppData/Local/Temp/wicked_pdf_generated_file20141017-9664-zo89le.pdf"] Error: PDF could not be generated! Command Error: Loading pages (1/6) [> ] 0% [======> ] 10% Error: Failed loading page file://c/Users/Rashid/AppData/Local/Temp/wicked_pdf20141017-9664-18xoryq.html (sometimes it will work just to ignore this error with --load-error-handling ignore) Exit with code 1 due to network error: ContentNotFoundError
def show
respond_to do |format|
format.html
format.pdf do
#example_text = "some text"
render :pdf => "file_name",
:template => 'clients/show.pdf.erb',
#:layout => 'pdf',
:footer => {
:center => "Center",
:left => "Left",
:right => "Right"
}
end
end
end
I was getting the same error and fixed it by following directions here
https://github.com/mileszs/wicked_pdf/issues/157
Namely I switched to tempory fix of gem by changing gemfile entry to:
gem 'wicked_pdf', github: 'mileszs/wicked_pdf'
I believe it has to do with using file:// instead of file:///.
Find wicked_pdf.rb in path/to/gem/wkhtmltopdf/bin, on line 64 replace file:// with file:///.

Resources