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

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.

Related

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

Understanding Tk Listbox in ruby

I'm trying to make a small program to mung some data into usable form. One thing I'd like it to do is to be able to select some files and perform actions on them, so I thought i'd use the listbox object in Tk to do that. I want to be able to open a file and see its filename displayed in the listbox. As far as I've read this is precisely what using listvariable in the listbox is for. Yet when I run my code the listbox is never updated (although items already in the listvariable variable are displayed fine).
So here's a close to MWE for this. What am I doing wrong, and what fundamental idea have I misunderstood?
require 'tk'
require 'tkextlib/tile'
$path_list = []
$populate_list = TkVariable.new( $path_list )
def get_file
file = Tk.getOpenFile
file = open(file) unless file.empty?
path = File.basename(file, ".out")
if $path_list.include?(path)
Tk.messageBox(
'type' => "ok",
'icon' => "warning",
'title' => " - Minimum Working Example - ",
'message' => "This file has already been added! Nothing was added to the list"
)
else
$path_list.push(path)
end
end
root = TkRoot.new {title "- Minimum Working Example -"}
frame = Tk::Tile::Frame.new(root) {padding "3 3 12 12"}.grid( :sticky => 'nsew') # 'north south east west'
TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root, 0, :weight => 1
$file_listbox = Tk::Listbox.new(frame) {
listvariable $populate_list}.grid( :column => 1, :row => 0, :rowspan => 6)
Tk::Tile::Button.new(frame) {
width 15; text 'Open file...'; command {get_file}}.grid( :column => 0, :row => 1)
Tk.mainloop
Do I maybe have to write it in some other order?
Just add one line of code:
$populate_list.value = $path_list
under this one:
$path_list.push(path)
It works for me, although looks weird.
TkVariable create a proxy for you ruby variable, thus bridge your ruby var references with Tk widgets. But i don't know why changes in proxy var don't affect the var it points to. I'm not sure whether it should do that automatically.

Formatting a cell as Text using the axlsx spreadsheet ruby gem?

I'm using the axlsx ruby gem to create Excel-compatible .xlsx files. I can't figure out how to override the cell type that is generated by it's automatic type detection. For Active Record model attributes of type string the gem is setting the Excel cell format to General, but I want it to use Text explicitly. That way I can avoid stripping leading zeros off of zip codes, etc.
Anybody know how to accomplish this?
You can override the type of data using the types option on add row.
Something like:
worksheet.add_row ['0012342'], :types => [:string]
Grab me on irc (JST) if you need any help getting that to work.
Best
randym
edit --
I've added an example for this to examples/example.rb in the repo.
wb.add_worksheet(:name => "Override Data Type") do |sheet|
sheet.add_row ['dont eat my zeros!', '0088'] , :types => [nil, :string]
end
https://github.com/randym/axlsx/blob/master/examples/example.rb#L349
format_code: '#' will work for you. Please find below code for reference.
def default_data_type_as_string
#xlsx_package = Axlsx::Package.new
#workbook = #xlsx_package.workbook
#worksheet = #workbook.add_worksheet(:name => "Introduction")
default_style = #workbook.styles.add_style({ format_code: '#' })
row_data_array = ['1', '2%', '3$']
#worksheet.add_row row_data_array, :style => [nil, default_style, nil]
#xlsx_package.serialize('default_data_type_as_string.xlsx')
end
For gem versions gem 'axlsx', '2.1.0.pre', gem 'axlsx_rails' in order to have the file columns in text type should specify both style and type
default_style = worksheet.styles.add_style({ format_code: '#' })
worksheet.add_row ['0012687'], :types => [:string], :style => [default_style]

Setting a hyperlink text color in axlsx

I'm trying to set the foreground color of text in a hyperlink cell but it doesn't seem to work.
Using something like: sheet["A1"].color = "0000FF" works fine for a normal cell, but not for a hyperlinked cell
This code simply creates a link to cell D1 on the "Log" sheet (which works fine) but A1 never turns blue!
sheet.add_hyperlink :location => "'Log'!D1", :target => :sheet, :ref => "A1"
sheet["A1"].color = "0000FF"
Thanks!
There are two important things to do before applying a color to a link:
You have to define the color within a style, and
You have to know the exact address of the cell in question.
Styles are normally applied to rows, but in this case you want to apply it to a specific cell. This is possible, but you need to address the cell directly through the Sheet Object. Also, and somewhat counter intuitively, the 'add_hyperlink' method is available to the Sheet object, not the Cell. So beware of that as well.
Here is an example of how to apply a style to a cell containing a link:
p = Axlsx::Package.new
p.workbook do |wb|
wb.styles do |s|
blue_link = s.add_style :fg_color => '0000FF'
wb.add_worksheet(:name => "Anchor Link Test") do |sheet|
sheet.add_row ['Title', 'Link']
# Define the row here, we will use that later
row = sheet.add_row ['Google', 'Click to go']
# Add the hyperlink by addressing the column you have used and add 1 to the row's index value.
sheet.add_hyperlink :location => "http://www.google.com", :ref => "B#{row.index + 1}"
sheet["B#{row.index + 1}"].style = blue_link
end
s = p.to_stream()
File.open("anchor_link_test.xlsx", 'w') { |f| f.write(s.read) }
end
end
Final note: You might note that I have written this spreadsheet using the methods
s = p.to_stream()
File.open("anchor_link_test.xlsx", 'w') { |f| f.write(s.read) }
There is evidence presented on the Axlsx Github Issues Page which shows that this means of writing out the file is significantly faster than
p.serialize
Just thought that deserved mention somewhere on StackOverflow!
This seems to work:
require 'axlsx'
p = Axlsx::Package.new
ws = p.workbook.add_worksheet
ws.add_row ['hoge-hoge']
ws['A1'].color = '0000FF'
ws.add_hyperlink :location => 'F6', :target => :sheet, :ref => 'A1'
p.serialize 'where_is_my_color.xlsx'
Can you post a larger example of your code that does not set the color?
Apparently Axlsx is only applying custom styles to String data types. Fixed this by setting each column to type :string like this:
Sheet.add_row [ "1", "2", "3" ], :types => [:string, :string, :string]
Thanks Randy!

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