Error generating pdf using prawn - ruby

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.

Related

PDF Writer gem does not support special characters

Why the pdf writer does not support the special characters and russian,chinese letters using ruby
It true that pdf writer does not UTF-8 but you can implement you solution like following
Overriding PDF::Writer text method
Put the following code in a file called pdfwriter_extensions.rb (or whatever you choose to call it) in your lib directory:
CONVERTER = Iconv.new( 'ISO-8859-15//IGNORE//TRANSLIT', 'utf-8')
module PDF
class Writer
alias_method :old_text, :text
def text(textto, options = {})
old_text(CONVERTER.iconv(textto), options)
end
end
end
In your controller that handles the PDF output you add:
require 'pdf/writer'
require 'pdfwriter_extensions'
after which you can use PDF::Writer like in the tutorial:
pdf = PDF::Writer.new
pdf.select_font "Helvetica", :encoding => nil
pdf.text "User name: <b>#{#user.name}</b>", :font_size => 16, :justification => :left
send_data pdf.render, :disposition => 'inline', :filename => "user_details.pdf", :type => "application/pdf"
N.B: I have taken this solution from https://www.peterkrantz.com/2007/utf8-in-pdf-writer/

Blinking string in curses application

In the meagre documentation for ruby curses I found this method
A_BLINK
Blinking
See ::attrset
However, I don't know how to utilize it.
win1 = Window.new
win1.addstr.a_blink "Blinking" #=> error
Please don't blame me, there is literally no help on google regarding curses. Honestly, at least not for ruby.
You can set attributes with Curses::Window#attrset. Here's an example:
require "curses"
include Curses
init_screen
begin
attrs = {
A_NORMAL => 'Normal display (no highlight)',
A_STANDOUT => 'Best highlighting mode of the terminal.',
A_UNDERLINE => 'Underlining',
A_REVERSE => 'Reverse video',
A_BLINK => 'Blinking',
A_DIM => 'Half bright',
A_BOLD => 'Extra bright or bold',
A_PROTECT => 'Protected mode',
A_INVIS => 'Invisible or blank mode',
A_ALTCHARSET => 'Alternate character set',
}
attrs.each { |a, s|
attrset(a)
addstr("#{s}\n")
}
refresh
getch
ensure
close_screen
end

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

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.

Using middleman, how do you include one HAML file in another HAML file?

I'm using middleman to do some rapid prototyping and can't for the life of me figure out how to include one HAML file into another HAML file.
I can include stuff in a layout file, but can't get one non-layout file to include another non-layout file. There are blocks of HTML that I want to reuse on some pages and I think I could do this. I've tried:
- render: partial=>"shared/nav.haml"
=shared/nav.html
="shared/nav.html
and none of these work.
Am I missing a config option or plugin? This is a fresh middleman install.
ANSWER
Partials may need file names that start with an underscore. My partial is placed in a folder called shared. The full name of the file is _nav.html.haml
This worked for me.
!= haml :"shared/_nav"
Example in context:
#email.main.subscriber.resize
#bg-wrap
%div
%img{:src=>"images/backgrounds/image.png",:alt=>""}
%section#zone10
!= haml :"shared/_nav"
You may also use the format specified in the approved answer below.
I've been using HAML with MiddleMan and couldn't be happier. Here is what is working for me:
I have a file: source/_donate_buttons.h
#DonationButtons
%p= t('searching.donate_cover_costs')
%br
= partial(:paypal_donate_button, :locals => {:amount => 1,
:amount_text => t('searching.donate_1')})
This uses the partial statement shown to include a file called source/_paypal_donate_button.html.haml.
And I include the _donate_buttons.html.haml file itself in a couple of places with:
= partial "donate_buttons"
though I think this could also be:
= partial :donate_buttons
I.e. I think partial is the magic you're looking for.
And, just for completeness, here is a slightly stripped down _paypal_donate_button.haml which shows how the paramaterization works there:
-btnclass = (locals.key?(:highlight) && locals[:highlight] ? "HighlightedDonationButton" : "DonationButton")
-btnstyle = locals.key?(:button_style) && locals[:button_style]
.DonationButtonContainer
%form{:action => "https://www.paypal.com/cgi-bin/webscr", :method => "post"}
%input{:name => "business", :type => "hidden", :value => "payments#example.com"}
%input{:name => "cmd", :type => "hidden", :value => "_donations"}
%input{:name => "amount", :type => "hidden", :value => "#{amount}.00"}
%input{:name => "currency_code", :type => "hidden", :value => "USD"}
%input{:class => btnclass, :alt => t('paypal.alt_text'),
:style => "cursor: pointer; font-size: 18px; #{btnstyle}", :type => "submit", :value => amount_text}
Fwiw, I don't think the file needs to be _filename.html.haml and can instead be _filename.haml. Also, I'm localizing these, so ignore the t('tagname') and just put strings there. (I didn't want to introduce an error copy-pasting the examples so I left them in there.)
Hope this helps!

Resources