why ruby prawn draw from bottom - ruby

I am using prawn in ruby and draw a sample text, but why it render from bottom instead of from top?
require 'prawn'
require 'prawn/measurement_extensions'
def self.generate
pdf = Prawn::Document.new(page_size: 'A4', margin: [5.mm, 10.mm, 5.mm, 10.mm])
pdf.draw_text "Hello world", at: [0, 0]
pdf.dash 2, space: 3
pdf.vertical_line 0, 100, :at => 0
pdf.line_width = 0.5
pdf.stroke
pdf.render_file "#{__dir__}/test.pdf"
end
Result:

This is because, as stated in the manual - https://github.com/prawnpdf/prawn/blob/c504ae4e683017d7afadece084734a9190230cd8/manual/basic_concepts/origin.rb#L5, PDF documents have their origin (0,0) at the bottom left of the page. Therefore when you specifically tell something to draw at [0,0] it will draw at the bottom left of its encapsualating bounding box, which in your case is the page.

Related

Hexapdf: trying to draw white box, but its not appearing

When I run my code, my white box does not appear. I need a white box to cover existing images, text, so I can add new text. If I change the color to background_color: [255,255,180], the box is a transparent yellow. However, I need a non-transparent white.
require 'hexapdf'
require 'pry'
doc = HexaPDF::Document.open('template.pdf')
pages = doc.pages
box = HexaPDF::Layout::Box.create(
width: 500, height: 500, content_box: true,
background_color: [255,255,255]
)
pages.each do |p|
canvas = p.canvas(type: :underlay)
box.draw(canvas, 20, 100)
end
doc.write("template_with_white_box.pdf")
You need to use canvas = p.canvas(type: :overlay) for this to work because the underlay canvas draws beneath the existing page whereas the overlay canvas draws above the existing page.

How to store the barcode only as a .png instead of on a A4 page using rghost?

I'm using rghost-barcode to generate an Aztec barcode. But every time I create a barcode it get's saved on a full A4 page with the barcode at the top left.
Why is this happening? How do I save the barcode only to the PNG with the dimensions I want (720px x 720px).
Here's what I'm currently trying:
doc = RGhost::Document.new
doc.barcode_azteccode('This is Aztec Code',{:text=>{:size=>8}, :format=>"full"})
doc.render :png, :resolution => 100, :filename => "my_barcode.png"
And this is the result:
The gem uses postscript/ghostsript as backend, so it is always producing documents, and then inserts barcodes into this document. And these document seem to have A4 as default.
According the rghost wiki, you can specify how big the document should be:
doc = RGhost::Document.new :paper => [15,10]
I'm afraid the dimension is in inches (the wiki does not state the dimension)
You have to set the paper size and position your barcode. Notice that the y positioning of the barcode is from the bottom of the page. After a bit of trial and error, this works fine for example:
#paper is x , y
doc=RGhost::Document.new :paper => [4.4, 3.2]
#y is from bottom of page
doc.barcode_ean13('2112345678900',
{:text=>{:size=>7},
:enable=>[:text],
:scale=>[1,1],
:x => 0.5, :y => 0.4})
doc.render :png, :resolution => 100, :filename => "ean13.png"
If you want to increase the resolution of the code, just doubling the scale, as well as all the others parameters (paper size, and barcode x, y) should work.

RMagick transparency not working when compositing one image over another

In the following code I'm trying to overlay a transparent square over the image of some mountains. I thought it would work, but by setting background_color = 'none' it doesn't make the image transparent!
The result is a black square over the top left corner - desired result is the black square should be transparent.
require 'open-uri'
require 'RMagick'
image_url = 'http://farm9.staticflickr.com/8446/7937080514_62d7749860.jpg'
bg = Magick::ImageList.new
open(image_url, 'rb') do |f|
bg.from_blob(f.read)
end
layer = Magick::Image.new(200, 200) {
self.background_color = 'none'
}
bg.each do |frame|
frame.composite!(layer, 0, 0, Magick::OverCompositeOp)
frame.strip!
end
bg.write('out.jpg')
Here's my output image:
Edit: I'm on Mac, Lion, ruby 1.9.3p125, ImageMagick 6.7.5-7
Edit 2: This works fine on Heroku! But not on my machine. Heroku has the same version of ImageMagick. Strange :|
For some reason layer.alpha? == false. So I did sq.alpha(Magick::ActivateAlphaChannel) and then it worked!
Reference: http://www.imagemagick.org/RMagick/doc/image1.html#alpha

Can't get gravity to work with RMagick and 'caption'

I'm using RMagick 2.12.2 with ImageMagick 6.5.6-10 on Snow Leopard. I'm trying to put captions on a collection of photos, and I'm getting the caption to work (i.e. it appears on the image), but I can't get the gravity parameter to work correctly.
No matter what I set it to, I end up with some variation on NorthGravity.
For instance: Setting it to SouthWestGravity gives me NorthWestGravity. Setting it to SouthEastGravity gives me NorthEastGravity. Setting it to CenterGravity gives me NorthGravity.
In other words, I can't get the caption to come down off the top of the image.
I'd consider using "annotate," but I need "caption" so the lengthy caption text for each image will wrap.
What am I doing wrong?
Here's the code:
#!/usr/bin/env ruby
require "rubygems"
require "yaml"
require "RMagick"
include Magick
base_dir = "/Users/mike/Desktop/caption_test"
photo_log = File.open("#{base_dir}/photo_log.yaml" )
YAML::load_documents(photo_log) do |doc|
caption = doc["photo-caption"]
filename = doc["file"]
canvas = ImageList.new.from_blob(open("#{base_dir}/#{filename}") { |f| f.read } )
canvas << Magick::Image.read("caption:#{caption}") {
self.gravity = SouthWestGravity
self.size = "#{canvas.first.columns}"
self.font = "Helvetica Neue"
self.pointsize = 12
self.background_color = "#fff"
}.first
canvas.flatten_images.write("#{base_dir}/images/#{filename}")
end
You've probably long since moved on, but the I found a pretty simple answer to this--use
canvas.append(true).write("#{base_dir}/images/#{filename}")
In other words, you want the append option (use 'true' to stack vertically).
I think your problem is that you're applying the gravity to the dimensions of the caption image rather than the dimensions of the underlying image. Your caption will East/West align itself within its width but because it makes it own height, North/South always just mean North.
You probably want to specify the gravity on the flatten_images call instead which looks possible...

Ruby and Google Charts - using gchartrb

I'm having my first go at using Google charts using a Ruby wrapper called gchartrb. My first attempt was to draw a bar chart, which works fairly well, except that the largest value (Trend 2 - 800) hits the top of the y axis. I'm guessing this is a Google Charts issue, but I was wondering if there was a way around this problem so all values scale correctly?
labels = [1, 2, 3, 4]
GoogleChart::BarChart.new('800x200', "Bar Chart", :vertical, false) do |bc|
bc.axis :x, :labels => labels,
:color => '000000' # Months
bc.axis :y, :labels => [0, 100, 200, 300, 400, 500, 600, 700, 800, 900],
:color => '000000' # Number of books
bc.data "Trend 1", [100,200,300,400], '0000ff'
bc.data "Trend 2", [500,600,700,800], 'ff0000'
bc.width_spacing_options :bar_width => 50, :bar_spacing => 0, :group_spacing => 0
puts "\nBar Chart"
puts bc.to_url
end
Gave up on this gem and ended up using Google Charts directly.
for scaling issue, there is an option called 'range' for bc.axis attribute like,
bc.axis :y, :color => '000000', :range => [0,max_scale_value]
you can set max_scale_value to me maximum value of your data values.
1) If you are displaying bars side by side, you can set max_scale_value to 800, as per your data. i.e. max value from both of your arrays.
2) If you are displaying stacked bars(one above the other), then you should set max_scale_value to be maximum of [(100+500),(200+600),(300+700),(400+800)]
3) you need to remove labels property for y -axis,because it will automatically scale as per the values and give you labels.
thus, you wont have the scaling issue.
Hope it helps :)

Resources