wicked_pdf is not rendering header - ruby-on-rails

render :pdf => "file_name",
:layout => 'pdf.html.erb',
:template => 'transactions/show.pdf.erb',
:wkhtmltopdf => WICKED_PDF_BIN,
:show_as_html => true,
:layout => 'pdf.html.erb',
:header => {:html => { :template => 'shared/header.pdf.erb'}}
PDF is generated fine, unfortunately I do not see the header. I can stick the header in the main layout and it works fine too. It seems to me that the header line above is not being processed. The filename 'header.pdf.erb' does not seem to matter. I can point it to a file that does not exist and it throws no error.
This is Mac OS, Rails 3.2.1, ruby 1.9

You may want to simply render the template as a string and assign it to the header's content. Try this out:
header: {
content: render_to_string(template: 'header.pdf.haml')
}
It works well for me.

Related

Rails 4, How to add an s3 image to wicked pdf

In rails 4, I am using wicked_pdf gem for .pdf file download. I have to add an image inside this pdf, right now image is rendering through wicked_pdf_image_tag in development but in test environment image(s3) is not rendering.
Used Gems are,
gem 'wicked_pdf', '1.0.3'
gem 'wkhtmltopdf-binary', '0.9.9.3'
In initializers,
class WickedPdf
wkhtmltopdf_path = Rails.env.production? ? "#{Rails.root}/bin/wkhtmltopdf-amd64" : "#{Rails.root}/bin/wkhtmltopdf-amd64"
WICKED_PDF = {
:exe_path => wkhtmltopdf_path,
:wkhtmltopdf => wkhtmltopdf_path
}
end
In controller,
respond_to do |format|
format.html {
render :pdf => "sample",
:margin => {:top => 10, :bottom => 10, :left => 10, :right => 10},
:orientation => 'Portrait', # default , Landscape,
:no_background => true
}
end
In views, I have tried to load through
<%= Rails.env.development? ? wicked_pdf_image_tag("img/logo.png") : wicked_pdf_image_tag("#{Rails.root}/public/assets/img/logo.png") %>
<%= Rails.env.development? ? wicked_pdf_image_tag("img/logo.png") : wicked_pdf_image_tag("#{Rails.root}/assets/img/logo.png") %>
<%= image_tag(ActionController::Base.helpers.asset_path('img/logo.png')) %>
How can I load s3 image in pdf file?
You can put the image in a s3 bucket and make it public. After that try it like below. If you use the below code no need to use separate syntax for different environments, it will works for all .Hope it works.
<%= wicked_pdf_image_tag('//s3.amazonaws.com/bucket_name/image.png') %>
I have worked on the similar functionality but using PDFKit gem. But I think rendering logic will be almost similar.
Below is the code where I rendered my partial in controller
def print_batch
batch = Batch.find(params[:id])
respond_to do |format|
format.pdf {
html = render_to_string("_batch",:formats => [:html], layout: false , locals: { batch: batch })
Rails.logger.debug(html.inspect)
kit = PDFKit.new(html)
send_data(kit.to_pdf, :filename => "file_name_#{batch.id}.pdf", :type => 'application/pdf') and return
}
format.html
format.json { render json: {id: batch.id, processed: batch.processed?} }
end
end
In _batch.html.haml. You can see I have used person.s3_logo for rendering the pdf image.
- logo_image_pdf = person.logo.present? ? person.s3_logo : default_credit_logo
- logo_image_html = person.logo.present? ? image_path(Person.logo.thumb('100x100').url) : image_path('default_credit_logo.png')
- logo_image = params[:format] == 'pdf' ? logo_image_pdf : logo_image_html
.bucks
%img.logo{src: logo_image }
In Person.rb model. Anyway we cannot directly render image in PDF file directly from s3. So any gem will first download it in /tmp folder and render it. This is how I have done it in my model file.
def s3_logo
file = open(self.logo.remote_url)
file.path if file
end

Wicked_pdf page width in multipage pdf

Cheers! I'm using wicked_pdf to generate pdf docs from views:
pdf = WickedPdf.new.pdf_from_string(
render_to_string(
:layout => "pdf_report.haml",
:handlers => [:haml],
:formats => [:pdf, :haml],
:orientation => 'Landscape',
:encoding => "utf8",
:page_width => '2000',
:dpi => '300'
)
)
It's all ok, if pdf has one page:
But if pdf doc has more than one page, then width of the page is broken:
I wasn't able to reproduce your "only has one page" scenario with the code you posted, however there is a problem with the options you are passing. They aren't even getting to wkhtmltopdf, so it is probably deciding for you what kind of options to use.
render_to_string will silently discard any options it doesn't understand, but is not part of wicked_pdf.
pdf_from_string takes two params, the first being the string to pdf-ify, the second is a hash of pdf options from the README.
I've added your issue to the wicked_pdf_issues project here to reproduce and debug it:
https://github.com/unixmonkey/wicked_pdf_issues/commit/b722e8a06c42e1f2bcbb98281915d1e94b4fe2c9
You should get the results you are looking for by changing your code to something like this:
string = render_to_string(
template: 'pages/issue_330',
formats: [:pdf],
handlers: [:erb]
)
options = {
orientation: 'Landscape',
page_width: '2000',
dpi: '300'
}
pdf = WickedPdf.new.pdf_from_string(string, options)

render method in rails outputs wrong script tag

so I have this piece of line in my haml file in views
%script {:charset => "utf-8", :src => "http://widgets.twimg.com/j/2/widget.js"}
but when I load it and check the javascript console in chrome, it is outputting this instead.
{:charset => "utf-8", :src => "http://widgets.twimg.com/j/2/widget.js"}
What could be hte problem?
You have a space after %script. Remove that, and it'll work fine:
%script{:charset => "utf-8", :src => "http://widgets.twimg.com/j/2/widget.js"}

Is there a way for acts_as_flying_saucer to save a file without calling render_pdf?

As the title says I use the Rails gem that works good but I want to separately save pdf's to a folder. Is there a way to do this without calling render_pdf?
This is what does not work:
render_pdf :template => 'caseprinttemplates/create_pdf_to_print.erb', :pdf_file => "/home/mattias/www/Inkasso/public/uploadedfiles/" + results[:title]
redirect_to new_interventionreminder_path(:case_id => #interventionreminder.case_id, :saved => "1")
return
This does work on another page:
render_pdf :template => 'caseprinttemplates/create_pdf_to_print.erb', :layout => nil, :send_file => { :filename => "samladutskrift.pdf"}
The acts_as_flying_saucer plugin is just a wrapper around the Flying Saucer java library. In the source, you can see that only a single wrapper method is defined: render_pdf().
https://github.com/dagi3d/acts_as_flying_saucer/blob/master/lib/acts_as_flying_saucer_controller.rb
You can, however, use render_pdf() to render to a file, like the examples found here:
# Renders the template located at '/foo/bar/pdf.html.erb' and stores the pdf
# in the temp path with a filename based on its md5 digest
render_pdf :file => '/foo/bar/pdf.html.erb'
# renders the template located at 'app/views/foo.html.erb' and saves the pdf
# in '/www/docs/foo.pdf'
render_pdf :template => 'foo', :pdf_file => '/www/docs/foo.pdf'
# renders the 'app/views/foo.html.erb' template, saves the pdf in the temp path
# and sends it to the browser with the name 'bar.pdf'
render_pdf :template => 'foo', :send_file => { :filename => 'bar.pdf' }

Apotomo: How do you render a state/view that's in HTML format from another view that's in another format?

For reasons beyond this problem, I need to respond with JSON when creating an image:
<%=
raw({
...
:url => #image.url(:width => #width, :height => #height),
:edit_link => render({:state => :image_edit_table_cell, :template_format => :html}, #image),
# btw, I've also tried passing :format => :html
:delete_link => link_to_destroy_image(#image, :remote => true, :path => url_for_event(:destroy, :image_id => #image)),
...
}.to_json)
%>
However, in that JSON, I want to include some pretty fancy HTML. To include that, I render a state/view (as you can see in the :edit_link portion). This is the image_edit_table_cell.html.haml view of the widget:
= link_to_edit_image #image, :remote => true
# ... doesn't really matter - as long as you know this is html.haml
However, when I create an image, and the JSON is rendered, I get a Missing template error:
ActionView::Template::Error (Missing template cell/rails/image_edit_table_cell with {:handlers=>[:builder, :rjs, :rhtml, :erb, :haml, :rxml], :locale=>[:en, :en], :formats=>[:json]} in view paths "/home/ramon/source/unstilted/app/widgets", "/home/ramon/source/unstilted/app/widgets/layouts" and possible paths image/table/image_edit_table_cellapplication/image_edit_table_cellapotomo/widget/image_edit_table_cellcell/rails/image_edit_table_cell):
6: :caption => #image.caption || "",
7: :text_tag => #image.text_tag(:width => #width, :height => #height),
8: :url => #image.url(:width => #width, :height => #height),
9: :edit_link => render({:state => :image_edit_table_cell, :template_format => :html}, #image),
10: :delete_link => link_to_destroy_image(#image, :remote => true, :path => url_for_event(:destroy, :image_id => #image)),
11: :success => true
12: }.to_json)
app/widgets/image/table_widget.rb:37:in `image_edit_table_cell'
app/widgets/image/table/new_image.json.erb:9:in `_app_widgets_image_table_new_image_json_erb___457172591_99648970_0'
app/widgets/image/table_widget.rb:16:in `add_image'
app/widgets/image/uploader_widget.rb:22:in `upload'
It seems like the problem is that since I'm rendering it from a JSON file, it doesn't know to look for an HTML file (thus the format it's looking for is json. That's why you see the :template_file => :html option (I got that from here) in the first block of code above.
I'm using these gems:
'rails', '3.0.10'
'cells', '3.6.2'
'apotomo', "1.1.2"
Apparently, this is a Rails issue, not an Apotomo issue. This answer did it for me! Key is setting the format in your view file before you render your html view:
<% self.formats = ["html"] %>

Resources