How to combine Images to PDF using Prawn? - ruby

The below code is able to combine the images but they are not completely fitting in the PDF pages. Is there a setting in Prawn which allows to combine images fitting the page size and exported as PDF ?
require 'prawn'
require 'fastimage'
Prawn::Document.generate("hello.pdf", :page_layout => :landscape) do
(1..40).each do|i|
size = FastImage.size("./java/sl#{i}.jpg")
start_new_page(:size => size,:layout => :landscape)
image "./java/sl#{i}.jpg"
end
end

try to add some location using at attribute for the image
for example:
image "./java/sl#{i}.jpg", :at => [x,y]
specify the values of x,y as per your need.

Related

Image with axlsx gem

i am trying to add the images to excel. am able to send the different images when calling the method res_data. but after the execution i see the last image overwrites all the previous images captured.
here is my code. please help .am using axlsx gem in ruby
class abc
##row=1
##f = File.open('result.xlsx', 'w+')
##p = Axlsx::Package.new
##ws = ##p.workbook.add_worksheet
##ws.name="result"
def res_data(path)
##ws.add_image(:image_src => path, :noSelect => true, :noMove => true) do |image|
image.width=1366
image.height=768
image.start_at 0, ##row
end
##row=##row+42
##p.serialize(##f)
end
end
calling the method as (using capybara methods along with this)
require 'axlsx'
obj = abc.new
path='c:\test\image_file.png'
visit "http://google.com
save_screenshot(path, :full => true) # google home page screenshot is captured
obj.res_data(path)
click_button "btnI"
save_screenshot(path, :full => true) #clicks "i'm feeling lucky " and capture the screenshot again
obj.res_data(path)
am able to see the 'c:\test\image_file.png' file gets updated with the new image (when save screenshot is called)
but when i see the 'result_xlsx" file, it shows only the last image in all images.

Identical hashes returned from lambda behaving differently

I'm using Paperclip for uploads to a Rails 4 image server. We want to track different image sizes and be able to generate new sizes on the fly if an existing style with the requested size does not exist.
To do this we have, in addition to a Photo model, a Size model belonging to Photo. I'm using a lambda to generate a hash object from photo.instance.sizes and pass that into Paperclip's :styles parameter. The hash gets generated just fine and when I log it to standard output I see a hash with the default size in Paperclip's desired format. {:medium=>"300x300#"} (worth noting the keys in the generated hash are symbolic, Paperclip doesn't like string keys)
When this hash is being returned from the lambda, no additional styles are generated and only the original resolution is saved. However, when I return a hard-coded hash (default var in code below) with the same contents, the additional style "medium" is generated (along with the original).
I made sure the hard coded hash was exactly the same as the one generated from photo.instance.sizes by logging an === comparison which returns true.
Here's the has_attached_file from the Photo model:
has_attached_file :asset,
:styles => lambda { |photo|
# Empty hash will contain paperclip formatted style options
styles = {}
# Get instance of model and iterate over its sizes
photo.instance.sizes.each do |size|
# Paperclip style format { :style_name => "WIDTHxHEIGHT#" } trailing pound means crop and retain aspect ratio
styles[size.name.to_sym] = "#{size.width}x#{size.height}#"
end
# default = { medium: "300x300#" }
styles
},
:storage => :s3,
:path => "/assets/:idhash/:style.:extension",
:s3_credentials => {:bucket => ENV["AWS_BUCKET"], :access_key_id => ENV["AWS_ACCESS_KEY_ID"], :secret_access_key => ENV["AWS_ACCESS_KEY_SECRET"]}
Any insight (or an alternative) would be greatly appreciated!

How can I change the orientation of existing pdf using prawn?

I have a pdf file. I want to rotate all of its pages 90 degrees to the right. How can I achieve this using Prawn gem? When I try to use an existing pdf as a template and try rotate on it, it does not work. I tried the following in vain.
require 'prawn/core'
require 'prawn/layout'
require 'prawn/measurement_extensions'
pdf = Prawn::Document.new(:page_size => [4.in, 6.in], :template => 'orig.pdf', :layout => 'potrait') do |p|
p.rotate(90)
end
pdf.render_file("./test1.pdf")
pdf = Prawn::Document.new(:page_size => [4.in, 6.in], :template => 'orig.pdf', :layout => 'potrait', :rotate => 90)
pdf.render_file("./test2.pdf")
use :page_layout instead of layout... for mote info please follow this tutorial http://prawn.majesticseacreature.com/docs/0.11.1/Prawn/Document.html

Prawn PDF: create a template without the default page

I want to create a document with prawn one or more pages long that uses a different template for each page.
Prawn::Document.generate("test.pdf") do
doc.faces.each do |face|
start_new_page(:template => face.background_path)
end
end
This works and creates a document, however the first page is a blank letter sized page and then my pages added with start_new_page show up. Is there a way to have prawn not generate that first page?
thanks!
pdf = Prawn::Document.new(:skip_page_creation => true)
doc.faces.each do |face|
pdf.start_new_page(:template => face.background_path)
< your page building code here >
end
should work if I'm reading the docs correctly.
My controller code looks like:
def pdf
#person = Person.find(params[:id])
send_data #person.as_pdf, :filename => "#{#person.pdfName}.pdf", :type => "application/pdf"
end
and the as_pdf method in person.rb looks like
def as_pdf(type = 'short')
pdf = Prawn::Document.new(:margin => [36, 36, 36, 36] )
driver = self.pdf_layout_info
driver.each do |element|
< lots of ugly layout logic >
end
pdf.render
end

Watermarking with Prawn (using templates)

I need to be able to watermark a document that was created from a template. I have the following code right now:
# Note: the raw PDF text (body variable below) is sent from a remote server.
Prawn::Document.new(:template => StringIO.new(body), :page_size =>
'A4') do |document|
# ... including other pages and sections to the template here ...
# watermark
d.page_count.times do |i|
d.go_to_page i
d.stroke_line [d.bounds.left, d.bounds.bottom], [d.bounds.right, d.bounds.top]
d.draw_text "Watermark", :rotate => 45, :at => [100,100], :size => 100
end
end
This is ignoring the templated pages for some reason that I can't comprehend. Now here's where the plot thickens: if the server adds a watermark, then this code will work as expected (e.g. straight Ruby code = no overlaying text on the non-prawn-generated pages, yet watermarking works on a pre-watermarked template). My only guess is there is some way to create a z-index/layer that the server is doing but Prawn itself cannot.
Here is a portion of the code from the server that does the PDF
generation itself, this does the watermarking using iText:
PdfStamper stamper = new PdfStamper(...);
PdfContentByte over = stamper.GetOverContent(i + 1);
over.BeginText();
over.SetTextMatrix(20, 40);
over.SetFontAndSize(bf, 20);
over.SetColorFill(new Color(97, 150, 58));
over.ShowTextAligned(Element.ALIGN_CENTER,
watermarkText,
document.PageSize.Width / 2,
document.PageSize.Height / 2,
55);
over.EndText();
over.Stroke();
If that runs before I use the raw data in Prawn I can watermark, go
figure.
So my questions are:
Anyone know how I can achieve the same effect using prawn instead
of a hybrid? I'd rather handle the watermarking locally.
Is there a basic equivalent to GetOverContent() in Prawn?
Is there a better way to get a string of raw PDF data into Prawn
without using :template and StringIO? (I saw the #add_content method
but that didn't work)
TL;DR: I need to float text above the Prawn templated text a la
watermarking the document.
Any insight or paths I can research will be appreciated. If this
makes no sense I can clarify.
You could try stamping the document.
create_stamp("watermark") do
rotate(30, :origin => [-5, -5]) do
stroke_color "FF3333"
stroke_ellipse [0, 0], 29, 15
stroke_color "000000"
fill_color "993333"
font("Times-Roman") do
draw_text "Watermark", :at => [-23, -3]
end
fill_color "000000"
end
end
stamp_at "watermark", [210, 210]
create_stamp("stamp") do
fill_color "cc0000"
text_rendering_mode(:fill_stroke) do
transparent(0.5){
text_box "WATERMARK",
:size => 50,
:width => bounds.width,
:height => bounds.height,
:align => :center,
:valign => :center,
:at => [0, bounds.height],
:rotate => 45,
:rotate_around => :center
}
end
end
repeat (:all) do
stamp("stamp")
end

Resources