Setting header size in PDFKit with Ruby on Rails? - ruby-on-rails-3

I need a repeating image to be shown on every page of the pdf. The only solution to this I've found so far is putting it into the header using
%meta{:name => 'pdfkit-header_html', :content => pdf_header_url}
Works fine but the image is bigger than the header size. Is there any way I can increase the height of the header or put a repeating image somehow outside the header?
Thanks!

You'll need to tweak the margin-top and header-spacing values. For example:
%meta{:name => 'pdfkit-header_html', :content => pdf_header_url}
%meta{:name => 'pdfkit-margin_top', :content => '3cm' }
%meta{:name => 'pdfkit-header_spacing', :content => '10' }
From the wkhtmltopdf docs:
--header-spacing * <real> Spacing between header and content in mm
(default 0)
-T, --margin-top <unitreal> Set the page top margin (default 10mm)
You can put any of the wkpdftohtml options in the meta tags and PDFKit will pass them through.

I ended up adding this code
PDFKit.configure do |config|
config.default_options = {
:page_size => 'Letter',
:margin_top => '3.6in',
:margin_right => '0.3in',
:margin_bottom => '1.3in',
:margin_left => '0.3in'
}
end
to /config/initializers/pdfkit.rb. Worked like charm after I restarted the server.

Related

Wicked PDF, generating PDF from database table- images and style issues

I have an uploader (internal use only) that will upload an HTML document to a binary column of a table in my client-facing website. The client facing site has an index that allows the user to view the page as a normal website (using send_data h_t.html_code, :type => "html", :disposition => "inline"). I also want to give the user the ability to download a PDF of the page. For that I'm using wicked_pdf.
The entire problem seems to stem from the fact that the data is stored in the database. As strange as it sounds, it is vital to business operations that I get formatting exact. The issue is I can't see any image, and the stylesheets/style tags don't have any effect.
What I've tried-
Gsub-
def show
html = HtmlTranscript.find(params[:id])
html_code = html.html_code.gsub('<img src="/images/bwTranscriptLogo.gif" alt="Logo">','<%= wicked_pdf_image_tag "bwTranscriptLogo.gif" %>')
html_code = html_code.gsub('<link rel="StyleSheet" href="" type="text/css">','<%= wicked_pdf_stylesheet_link_tag "transcripts.css" %>')
transcript = WickedPdf.new.pdf_from_string(html_code)
respond_to do |format|
format.html do
send_data transcript, :type => "pdf", :disposition => "attachment"
end
##### i never could get this part figured out, so if you have a fix for this...
# format.pdf do
# render :pdf => "transcript_for_#{#html.created_at}", :template => "html_transcripts/show.html.erb", :layout => false
# end
end
end
Using a template-
#Controller (above, modified)
html = HtmlTranscript.find(params[:id])
#html_code = html.html_code.gsub('<img src="/images/bwTranscriptLogo.gif" alt="Logo">','<%= wicked_pdf_image_tag "bwTranscriptLogo.gif" %>')
#html_code = #html_code.gsub('<link rel="StyleSheet" href="" type="text/css">','<%= wicked_pdf_stylesheet_link_tag "transcripts.css" %>')
transcript = WickedPdf.new.pdf_from_string(render_to_string(:template => "html_transcripts/show.html.erb", :layout => false))
#view
<!-- tried with stylesheet & image link tags, with wicked_pdf stylesheet & image link tags, with html style & img tags, etc -->
<%= raw(#html_code) %>
And both will generate a transcript- but neither will have style OR image.
Creating an initializer-
module WickedPdfHelper
def wicked_pdf_stylesheet_link_tag(*sources)
sources.collect { |source|
"<style type='text/css'>#{Rails.application.assets.find_asset("#{source}.css")}</style>"
}.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe
end
def wicked_pdf_image_tag(img, options={})
image_tag wicked_pdf_image_location(img), options
end
def wicked_pdf_image_location(img)
"file://#{Rails.root.join('app', 'assets', 'images', img)}"
end
def wicked_pdf_javascript_src_tag(source)
"<script type='text/javascript'>#{Rails.application.assets.find_asset("#{source}.js").body}</script>"
end
def wicked_pdf_javascript_include_tag(*sources)
sources.collect{ |source| wicked_pdf_javascript_src_tag(source) }.join("\n").html_safe
end
end
did absolutely nothing, and I have no idea what to try next.
As a side note, the code to view the HTML version of the transcript is as follows:
def transcript_data
h_t = HtmlTranscript.find(params[:id])
send_data h_t.html_code, :type => "html", :disposition => "inline"
end
It requires no view, as the html data is stored in the database, but I get image, style, etc. Everything works with the HTML version- just not the PDF.
I'm on ruby 1.8.7 with rails 3.0.20.
Solved-
As it turns out, there was more than one issue at hand.
1- Installation of wkhtmltopdf for Ubuntu via $apt-get install does not quite do the trick for what I wanted...
see http://rubykitchen.in/blog/2013/03/17/pdf-generation-with-rails
(there may have also been an issue with having not previously run sudo apt-get install openssl build-essential xorg libssl-dev libxrender-dev, as when I did, it installed a number of components I did not previously have.)
2- The HTML files I had uploaded contained image & style code that was breaking the formatting. I fixed it with this...
def rm_by_line(which = 0, line1 = 0, line2 = 0)
h_t = HtmlTranscript.find(which)
line_by_line = h_t.html_code.split('
')
for i in line1..line2
line_by_line[i] = ''
end
line_by_line = line_by_line.join('
').strip
return line_by_line
end
Then, all I had to do was pass which lines I wanted to remove.
(I had to split the parens with a carriage return because '\n' didn't function properly when calling 'raw' on the returned string.)
3- wicked_pdf_stylesheet_link_tag and wicked_pdf_image_tag were undefined. I had to inline the style formatting I wanted into a layout I created (turns out wicked_pdf_stylesheet_link_tag used asset pipeline wich my ruby/rails did not implement, which also means I had to get rid of the javascript helpers) and created a helper for wicked_pdf_image_tag, making a switch in the layout for which image tag (image_tag or wicked_pdf_image_tag) to be used.
4- I needed both a .html.erb & a .pdf.erb for my templates, so I made both.
5- Got rid of WickedPdf.new.pdf_from_string in favor of linking to either html or pdf by using :format => 'html' or :format => 'pdf' in the link_to tag.

Google-Maps-for-Rails Direction Wiki Explaination Needed

Hi I am currently working on a trash bin/recycling bin location application using google maps for rails.
I have a recyclingbin.rb model with the address as its attributes, that itself is enough to put markers on a map that can get displayed using the gem. I believe the gem converts the model and its attributes into json data.
I am trying to implement a feature where I can input my location and get direction to the nearest marker.
I have looked at the wiki , https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Direction
Am I suppose to put this in the view ?
{ "data" => { "from" => "Paris, france", "to" => "Toulon, france" } }
})
%>
with the from to be embedded with my location for now? I understand I can pass options to this reference from google.
The wiki is quite short, can someone give me a quick explanation ?
The wiki does need some more work. I have a simple app working that shows the directions on the map from a location to another (pre-defined) location
<%= gmaps("map_options" => {"zoom" => 14}, "markers" => { "data" => #json },
"direction" => { "data" => {"from" => #location.address, "to" => "New York City"},"travelMode" => "DRIVING"}) %>
I just put this in my view. It shouldn't matter where, just as long as its there. Hope this helps

gmaps4rails detect_location is not showing marker

I am working with gem "gmaps4rails" in rails 3.2.12, I had set the detect_location = "true" in map_options, but it is not showing any marker with user current location.
Here is code:
<%= gmaps("map_options" => {"auto_adjust" => true, "detect_location" => true, "center_on_user" => true, "auto_zoom" => false, "radius" => 2500},
"markers" => {"data" => #json, "options" => {"list_container" => "markers_list","randomize" => true,"max_random_distance" => 10000, "draggable" => true, "dblclick" => "latLng"} }
How to show with marker for the user current location.
Please help me.
Thanks in Advance
With the gem "gmaps4rails",
if we give the option "detect_location" => true, when the map loads on page, it shows pop-up to user as 'allow application to use your location' and gives yes and no as options.
If user selects 'yes', then map get centered to user's location.
Now here you want to display marker for user's location, then you have to add marker by your own.
This can be done as follows:
Controller:
geo = request.location
#latitude = geo.latitude
#longitude = geo.longitude
With your page load, you can add marker for user, with this callback.
This would add marker for current user location when map loads on page.
- content_for :scripts do
%script{:type => "text/javascript"}
Gmaps.map.callback = function(){
google.maps.event.addListenerOnce(Gmaps.map.serviceObject, 'idle', function(){
/ add user pin
var home_pin = [{picture: "current_user.png", width: 32, lat: "#{#latitude}", lng: "#{#longitude}"}];
Gmaps.map.addMarkers(home_pin)
}
)};
Hope this would help you.
Let me know if you still face any issues.
Thanks.

Geodesic routes in gmaps4rails

I can currently draw polylines using gmaps4rails in Rails but I'd like to draw them as geodesic paths. According to the Google Map documentation this should be as simple as adding "geodesic: true" to the options (link). Below is my current implementation that doesn't work but simply draws a straight line on the map. Is there a correct way of setting this option in gmaps4rails or is this not currently supported?
In my controller:
#json_route =
[
[
{'lng' => 80.190262, 'lat' => 55.774252 },
{'lng' => -66.118292, 'lat' => 38.466465 }
]
]
#gmaps_options =
{
:polylines => { "data" => #json_route.to_json, "options" => { "geodesic" => true }}
}
In my html page:
<%= gmaps(#gmaps_options) %>
Thank you!

How do you have a default Gravatar that is external and that actually resizes properly?

To implement Gravatar in my Rails3 application, I'm using the gravatar_image_tag gem in a helper, but I'm having issues when mixing 2 config options:
If the user doesn't have a gravatar attached to his email a default image is rendered; but I want it to reference an external file (e.g., http://www.iconfinder.com/ajax/download/png/?id=43350&s=128 instead of :identicon or others)
I also want the image to be resized on the fly to, let's say 50px.
Independently, both options work as expected, but when I put them together:
def gravatar_for(user, options = { :default => 'http://www.iconfinder.com/ajax/download/png/?id=43350&s=128', :size => 50 })
gravatar_image_tag(user.email.downcase, :alt => user.full_name,
:class => 'gravatar',
:gravatar => options)
end
the size option is not applied, and the gravatar gets rendered in it's full size (128px in this case).
What am I doing wrong, or how can I achieve this combination?
Gravatar will not resize your default image for you. I assume that it just 302s to the ulr gave as a default if it does not find an gravatar for the email you gave it. It looks like the 's' parameter in the iconfinder url is for the size you are trying to grab but that icon does not have a size of 50px available only 128, 256, and 512
Example:
http://www.iconfinder.com/ajax/download/png/?id=43350&s=256
If you wanted a 50px and 80px versions of the icon I would save it to your applications public/image directory as default_gravatar_50.png and default_gravatar_80.png respectively and change your method like so.
end
def gravatar_for(user, options = {})
options = { :size => 50 }.merge(options)
options[:default] = image_tag("default_gravatar_#{options[:size]}.png
gravatar_image_tag(user.email.downcase,
:alt => user.full_name,
:class => 'gravatar',
:gravatar => options)
end
Or if you find an icon on icon finder that is the size(s) you like change the setting of the default option like so.
options[:default] = "http://www.iconfinder.com/ajax/download/png/?id=43350&s=#{options[:size]}"
Iconfinder here. You don't want to link to the download script. Instead just grab the URL to the image it self so you wan't get a lot of header information.