Undefined method size in PRAWN rails 3 - ruby-on-rails

Hi i'm getting error in this line of code:
pdf.font.size = 13
The name of the file is "show.pdf.prawn"
I'm trying to generate a page into pdf using the prawn gem.
But changing the font size isn't working.
Any alternatives or workaround? Thanks.
BTW: I'm following a simple tutorial from railstips.org : http://railstips.org/blog/archives/2008/10/13/how-to-generate-pdfs-in-rails-with-prawn/

Your tutorial seems to be a little bit old. You should have a look at this manual : http://prawn.majesticseacreature.com/manual.pdf and/or check the railscasts episode : http://railscasts.com/episodes/153-pdfs-with-prawn-revised.
You can use the font_size property to define the size of your text like this :
font_size(25) { text "Even bigger!" }
You can also do it like this :
text "Single line on 20 using the :size option.", :size => 20
Or you can use a block to apply different font properties to your text :
font("Courier", :size => 10) do
text "Yeah, using Courier 10 courtesy of the font method."
end

Related

Rails 6 Axlsx Excel Gem - Images not displaying

I don't know if it's my fault or if it just doesn't work with Rails 6, but my images in the file are not displaying:
I tried:
img = File.expand_path(Rails.root+'app/assets/images/logo.jpg')
and also like described in the examples:
img = File.expand_path('../logo.jpg', __FILE__)
sheet.add_image(:image_src => img, :noSelect => true, :noMove => true) do |image|
image.start_at 5, 20
image.end_at 7, 22
end
The image is found (at least I don't get an error for this) but the only thing I see in the generated file (the rest of the creation works fine) is:
Could it be the image size not fitting in the cells? Or is it resizing automatically?
Any suggestions or ideas? I don't know what's wrong or what else I could try
Did you try setting height and width for the image? Have a look at this old answer https://stackoverflow.com/a/47982814/1796645 (potential duplicate question)
If you found a bug, then you could report it in https://github.com/caxlsx/caxlsx/issues

Arabic word in rails prawn shows reverse order

When I try to create a pdf in Arabic Language, I'm getting the letters in reverse order.
I use the following code to generate a pdf file.
note_section = {content: "#{order_item.try(:notes).try(:connect_arabic_letters)}" ,
size: 8,
font: "app/assets/fonts/ufonts.com_arial-unicode-ms.ttf",
text_color: "313131"}
Expected text is:
In prawn it looks like:
Gem version => Prawn version:2.2.2 Rails version:5.1.6
Any help is highly appreciated, thanks!

Arabic Prawn Gem

Since the Arabic letters appeared as unknown characters while using prawn gem alone , I installed the Arabic-Prawn gem 0.0.1 to print data in arabic .
I used as a test the below code
Prawn::Document.generate('hello.pdf') do
font "#{Prawn::BASEDIR}/data/fonts/artro.ttf"
font_size 16
self.text_direction = :rtl
long_text = "مرحبا يا العالم"
text long_text.fix_arabic_glyphs
end
First it displays the below error:
undefined method `fix_arabic_glyphs'
so I included the prawn in the class , the error disappeared but the Arabic letters are still not shown (represnted as _).
Someone can help me?
I had the same problem, and this solved it for me:
https://github.com/prawnpdf/prawn/issues/921
I have solved the problem by using another font only.
Some characters are not defined for some fonts .
So using a font that defines all characters for arabic language is the best solution

Aptana Studio 3 Snippet Around Selection

So I have recently switched from Dreamweaver to Aptana Studio 3 and I have been playing around with the whole custom snippet feature. For the life of me though I cannot figure out how to take a selection/highlighted text and wrap it with my own custom code and/or text. I have looked around the internet for three days now and cannot find anything regarding snippets. I have found some things using commands and key combinations, but I am wanting to create and use a snippet and trying to modify what I have found is not producing good fruit.
I have been able to create my own category and some basic snippets that insert straight text, but nothing that uses a selection.
I have absolutely NO experience with Ruby so forgive me if what follows is completely atrocious. I have more experience with PHP, HTML, Javascript, Java, etc. Here is what I have so far.
snippet "Selection Test" do |snip|
snip.trigger = "my_code"
snip.input = :selection
selection = ENV['TM_SELECTED_TEXT'] || ''
snip.expansion = "<test>$selection</test>\n"
snip.category = "My Snippets"
end
I haven't done much with custom Snippets, but if it helps, there is an example in the HTML bundle of a snippet that surrounds the selected text with <p></p> tags when you do Ctrl + Shift + W. You can see the code for it in snippets.rb in the HTML bundle:
with_defaults :scope => 'text.html - source', :input => :none, :output => :insert_as_snippet do |bundle|
command t(:wrap_selection_in_tag_pair) do |cmd|
cmd.key_binding = "CONTROL+SHIFT+W"
cmd.input = :selection
cmd.invoke do |context|
selection = ENV['TM_SELECTED_TEXT'] || ''
if selection.length > 0
"<${1:p}>${2:#{selection.gsub('/', '\/')}}</${1:p}>"
else
"<${1:p}>$0</${1:p}>"
end
end
end
end
I fiddled around with putting it into the PHP bundle for a few minutes under CTRL + Shift + P and got it working in HTML files, which was not my goal... but was progress. I may play around with it some more later, but in the meantime, maybe you know enough after all of your research to get something put together. I would be interested to see your results if you get this figured out.

Rails 3 and PDFKit. How to specify page size?

I have been looking in the documentation but can't find the answer. How can I specify the page size of my pdf document and what are the available page sizes? I keep on looking and looking but I can't find good documentation. Please point me to a URL or let me know how can I code some page size into my PDF document.
Oh and I don't want to do that on any config file because I need to generate PDf documents of different sizes.
NOT in config file...
PDFKit.configure do |config|
config.wkhtmltopdf = `which wkhtmltopdf`.to_s.strip
config.default_options = {
:encoding=>"UTF-8",
:page_size=>"A4", #or "Letter" or whatever needed
:margin_top=>"0.25in",
:margin_right=>"1in",
:margin_bottom=>"0.25in",
:margin_left=>"1in",
:disable_smart_shrinking=>false
}
end
You can set the page size when creating a new PDF like this:
kit = PDFKit.new(source, :page_size => "Legal")
PDFKit uses WKHTMLTOPDF which in turn uses QPrinter. You can find the available sizes in the QPrinter documentation (there's a bunch), but its pretty safe to say that any size paper you want is available. Also, you can set a custom size if you can't find what you need.
NB: If you don't set a default option for page_size in a config somewhere AND don't supply one in your method call, PDFKit will use its internal default (Letter). See line 10 of lib/pdfkit/configuration.rb
Pdkit accepts custom sizes:
PDFKit.configure do |config|
config.wkhtmltopdf = `which wkhtmltopdf`.strip
config.default_options = {
:page_width => '1682',
:page_height => '2378'
}
end
The sizes must be in milimeters (wkthmltopdf documentation).
Since it's using wkhtmltopdf to generate the PDFs I'm assuming you can use the same options that it supports. In a wkhtmltopdf manual I found, it mentions the following site for a list of sizes:
http://doc.trolltech.com/4.6/qprinter.html#PaperSize-enum
To set the page size, you can use the :page_size option like so:
PDFKit.new(html, :page_size => 'Letter')

Resources