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

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

Related

Unable to upload images from Ruby to Wordpress using XMLRPC - damaged image

I am using a gem called rubypress to post content from ruby to wordpress using XMLRPC. Everything is working except for the image uploading part. I converted the image to base64 encoded format but after upoloading, all i get is a grey image and not the one that i had intended to upload(I am using smaller images around 100kb size to upload for testing). What I am i doing wrong?
Here's the code from rubypress github page(https://github.com/zachfeldman/rubypress) for uploading:
FILENAME='myFile.png'
wp.uploadFile(:data => {
:name => FILENAME,
:type => MIME::Types.type_for(FILENAME).first.to_s,
:bits => XMLRPC::Base64.new(IO.read(FILENAME))
})
Try:
wp.uploadFile(:data => {:name => File.basename(FILENAME),
:type => MIME::Types.type_for(FILENAME).first.to_s,
:bits => XMLRPC::Base64.new(File.open(FILENAME).read)
})
I was finally able to solve it by changing the Base64 encoding code as follows:
$wp.uploadFile(:data => {
:name => File.basename(imgname),
:type => MIME::Types.type_for(imgname).first.to_s,
:bits => XMLRPC::Base64.new(File.open(FILENAME,"rb").read),
:post_id =>postid
})
The file apparently needs to be "opened" first rather than be read using IO.read(FILENAME).
Here is my way of doing it, works pretty well:
image = MiniMagick::Image.open(this_image.path)
image.format('jpg')
image.combine_options do |c|
c.strip
end
image.write(this_image.path)
# most efficient resize as suggested by https://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/
MiniMagick::Tool::Mogrify.new do |mogrify|
mogrify.filter('Triangle')
mogrify.define('filter:support=2')
mogrify.thumbnail('960x')
mogrify.unsharp('0.25x0.08+8.3+0.045')
mogrify.dither('None')
mogrify.posterize('136')
mogrify.quality('82')
mogrify.define('jpeg:fancy-upsampling=off')
mogrify.interlace('none')
mogrify.colorspace('sRGB')
mogrify << this_image.path
end
type = image.mime_type
bits = XMLRPC::Base64.new(IO.read(this_image.path))
begin
result = wp.uploadFile(
data: {
name: image_name,
type: type,
bits: bits,
post_id: post_id,
overwrite: true
})
rescue StandardError => e
puts e
end

How to combine Images to PDF using Prawn?

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.

How to Insert text in existing pdf using PRAWN gem in rails

I am working on 1 application and trying to open and edit pdf using rails PRAWN gem,
i have tried some ways like
#id1 = "Name"
#id2 = "age"
#id3 = "birthday"
Prawn::Document.generate "/home/test.pdf" do |#fname|
data = [["#{#id1} | #{#id2} | #{#id3}"], ["Address", "Country"]]
{:border_width => 0}.each do |property, value|
#fname.table(data, :column_widths => [470, 70, 100], :cell_style => {property => value})
end
#fname.stroke_horizontal_rule
#fname.move_down(20)
end
send_file("/home/test.pdf", :type => 'application/pdf', :disposition => 'inline')
it is adding content but the contents are overlapping on the existing content
Is there anyone who can help me to insert content at to top of the first page without overlapping that content on the existing one
The existing content should shift down.
Ideas other then PRAWN are also invited.

Error generating pdf using prawn

I am trying to use tags to give some styling for a pdf being generated using prawn. But, there seems to be an error.
require 'rubygems'
require 'prawn'
require 'prawn/layout'
require 'prawn/format'
Prawn::Document.generate "example.pdf" do
tags:h1=>{ :font_size => "16pt", :font_weight => :bold }
text"<h1>Student Details</h1>"
end
I get the following error -
/usr/lib/ruby/gems/1.8/gems/prawn-format-0.2.3/lib/prawn/format/text_object.rb:91:in `%': can't convert nil into Float (TypeError)
Any help is greatly appreciated.
Cheers!!
Shouldn't it be:
tags[:h1] = { :font_size => "16pt", :font_weight => :bold }
?
Also please note that:
As of Prawn 0.7, prawn-format is completely unsupported, and will not
work with versions of Prawn 0.7+. Feel free to fork and fix, of
course.
Consider using methods from Prawn::Text
http://rubydoc.info/gems/prawn/0.12.0/Prawn/Text
EDIT
For example:
require 'rubygems'
require 'prawn'
Prawn::Document.generate('font_calculations.pdf') do
font "Courier", :size => 16, :style => :bold
text "Student details"
font "Courier", :size => 12, :style => :normal
text "normal text"
text "this is normal, <b>but this is bold</b>", :inline_format => true
text "normal <font size='18'>bigger</font> normal", :inline_format => true
end
That's just one of many ways of doing this.

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