Ruby PDF Label Printer New Line Not Working - ruby-on-rails

Please be patient with me, this is the first time that I have ever messed with Ruby. Basically here is my issue. I have a huge site that is just needing a single thing modified however I'm running into issues.
I am trying to get a PDF output for a lapel printer to wrap down to the next line. Here is my code.
def print_labels
quantity = params[:quantity].to_i
hashed_label = ActiveSupport::JSON.decode(params[:label])
p = InventoryItem.labels_pdf do |pdf|
(1..quantity).each do |i|
pdf.text("#{hashed_label['inventory_item_code']} #{hashed_label['label_description']} #{hashed_label['inventory_item_size']}", :style => :bold)
pdf.text("#{hashed_label['ingredients']}", :size => 9, :font => :serif)
#pdf.text(params[:label]);
pdf.start_new_page if i < quantity
end
end
send_data(p, :type => "application/x-pdf", :filename => "labels.pdf")
end
Here is the output in the PDF.
inventory_item_code label_description inventory_item_size
I am trying to get them to break between each item.

Is this using Prawn? You should be able to put newlines in all you want:
pdf.text("Here is some\ntext with\nnewlines")
So, a newline after the ingredients would just be:
pdf.text("#{hashed_label['ingredients']}\n", :size => 9, :font => :serif)

Related

last element of the hash

I am sending several parameters for the printing of tickets in pdf
Parameters: {"utf8"=>"✓", "list"=>{"client_0801541"=>"0801541", "client_0801554"=>"0801554", "client_0801554"=>"0801554"}, "subaction"=>"print selected clients"}
I need to print each client on a different page, for this I use a start_new_page at the end of showing the client's code, but in that way after the last client leaves a blank page. How could I make it so that for each client it verifies if it is the last one of the array? I have tried the following:
def client
#client.each do |(c,client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == #client.map{|e|[e.c, e.client_id]}.client_id.last
end
end
You can try:
#clients.each do |(c, client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == #clients.values.last
end
You could also try:
#clients.each_with_index do |(c, client_id), i|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless i == #clients.length - 1
end
As Tadman suggests, you could also speed this up by doing:
def client
#clients_length = #clients.length - 1
#clients.each_with_index do |(c, client_id), i|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless i == #clients_length
end
end
Your original code:
def client
#client.each do |(c,client_id)|
draw_text "#{client_id}", :at => [0,22], :size => 5, :style => :bold
start_new_page unless client_id == #client.map{|e|[e.c, e.client_id]}.client_id.last
end
end
Seems banged up, minimally, because:
You're using map on every iteration, which seems wasteful.
In #client.map{|e|[e.c, e.client_id]}, .c and .client_id aren't methods on Array.
.client_id isn't a method on Array.
Probably other stuff.
BTW, last two keys and values in your list are identical. I don't know if that's an error. But, if that's what you intend, then you may have other problems.
Also BTW, the title of your question says 'last element of the array', but you're working with a hash, not an array.

is there any way to add outlines(bookmarks) to existed pdf in ruby

I have few pdf files and could combine into single pdf called combined.pdf with the following code.
pdf = CombinePDF.new
pdf << CombinePDF.load("1.pdf")
# one way to combine, very fast.
pdf << CombinePDF.load("2.pdf")
pdf << CombinePDF.load("3.pdf")
pdf << CombinePDF.load("4.pdf")
pdf.save "combined.pdf"
Now I want to add outlines to this combined.pdf.
Trying with the prawn, but no luck. Here is the code that i have tried
Prawn::Document.generate("full_template.pdf", :template => filename) do
text "THis content is written on the first page of the template", :align => :center
outline.define do
section("Slaves", :destination => 1) do
page :title => "Page 2", :destination => 2
page :title => "Page 3", :destination => 3
end
end
end
https://github.com/yutayamamoto/pdfoutline
I made a library which helps you adding an outline to a PDF file though it's not written in ruby.

Roo getting number instead of text

I am using roo to parse out an excel sheet like this
worksheet.parse(
:partNo => "PM_PartNo",
:salePartNo => "PM_SalesPartNo",
:appSearchInclude => "PM_AppSearchInclude",
:desc => "PM_WebApp_Description",
:brand => "PM_Brand",
:appSegment => "PM_ApplicationSegment",
:group => "PM_ProductGroup",
:design => "PM_ProductDesign",
:material => "PM_Material",
:line => "PM_ProdLine",
:baseSeries => "PM_BaseSeries",
:colorCode => "PM_ColorCode",
:series => "PM_Series",
:weightType => "PM_oz_gram",
:appRim => "PM_ApplicationRim",
:coating => "PM_Coating",
:pcs => "PM_PCSconversion",
:clean => true
) do |hash|
However, Roo keeps giving me a number 200275577.0 for the PM_PartNo column. In the excel sheet, this column has all cells formatted as text. What is should return in the parse is "200275577" as text, not 200275577.0 as a number. Is there a way to ensure it adheres to the excel formatting?
This is an open issue with roo. There is a general workaround contributed by a user in the issue, or you can just convert the value yourself with .to_i.to_s.

How to generate Excel file with passing params from AJAX search?

I'm performing AJAX search in my Rails application. Here is code from controller:
def show
#website = Website.find(params[:id])
if (current_user.id != #website.user_id)
redirect_to root_path
flash[:notice] = 'You are not owner!'
end
if params[:report] && params[:report][:start_date] && params[:report][:end_date]
#performance_reports = #website.performance_reports.where("created_at between ? and ?", params[:report][:start_date].to_date, params[:report][:end_date].to_date)
else
#performance_reports = #website.performance_reports
end
but when I'm trying to generate Excel document it alway goes to branch without params, because there are no params in URL.
One man reccomend me to use this post. I tried to implement it, but couldn't.
I don't understand this post enough, I just can't get where data is passing(spreadsheet gem)
Here is code:
def export
#website = Website.last
#data = #website.performance_reports
report = Spreadsheet::Workbook.new
spreadsheet = StringIO.new
contruct_body(spreadsheet, #data)
report.write spreadsheet
send_data spreadsheet.string, :filename => "yourfile.xls", :type => "application/vnd.ms-excel"
end
and it gives me error:
undefined method `contruct_body'
Code from view:
<%= form_tag( url_for, :method => :get, :id => "report") do%>
...show action posted above...
<% end %>
<%= link_to export_path do%>
<b>Export</b>
<% end %>
...working code without AJAX...
<%= link_to url_for(request.parameters.merge({:format => :xls})) do%>
<b>Export</b>
<% end %>
Please tell me where is my mistake or suggest ano
For the first problem, you need to show the view code and the path ajax is taking. Give us more information how the excel is being called.
For the second issue, you need to define that method. Specify how you will populate the spreadsheet with the data. Here is the guide. https://github.com/zdavatz/spreadsheet/blob/master/GUIDE.txt
== Writing is easy
As before, make sure you have Spreadsheet required and the client_encoding
set. Then make a new Workbook:
book = Spreadsheet::Workbook.new
Add a Worksheet and you're good to go:
sheet1 = book.create_worksheet
This will create a Worksheet with the Name "Worksheet1". If you prefer another
name, you may do either of the following:
sheet2 = book.create_worksheet :name => 'My Second Worksheet'
sheet1.name = 'My First Worksheet'
Now, add data to the Worksheet, using either Worksheet#[]=,
Worksheet#update_row, or work directly on Row using any of the Array-Methods
that modify an Array in place:
sheet1.row(0).concat %w{Name Country Acknowlegement}
sheet1[1,0] = 'Japan'
row = sheet1.row(1)
row.push 'Creator of Ruby'
row.unshift 'Yukihiro Matsumoto'
sheet1.row(2).replace [ 'Daniel J. Berger', 'U.S.A.',
'Author of original code for Spreadsheet::Excel' ]
sheet1.row(3).push 'Charles Lowe', 'Author of the ruby-ole Library'
sheet1.row(3).insert 1, 'Unknown'
sheet1.update_row 4, 'Hannes Wyss', 'Switzerland', 'Author'
Add some Formatting for flavour:
sheet1.row(0).height = 18
format = Spreadsheet::Format.new :color => :blue,
:weight => :bold,
:size => 18
sheet1.row(0).default_format = format
bold = Spreadsheet::Format.new :weight => :bold
4.times do |x| sheet1.row(x + 1).set_format(0, bold) end
And finally, write the Excel File:
book.write '/path/to/output/excel-file.xls'

Formatting Tables in Prawn (this seems to be a common problem)

I"m having no luck formatting a table in a PDF served from a Rails action using Prawn.
Here's the code, but it doesn't apply any of the formatting:
p = Prawn::Document.new(:page_size => "A4")
data = []
data << ["alpha", "brava", "charlie"]
data << ["delta", "echo", "foxtrot"]
p.table(data) do
row(0).style(:background_color => 'dddddd', :size => 9, :align => :center, :font_style => :bold)
cells[0,0].background_color = '999999'
end
p.render
Any clues?
Which version of prawn are you using? I had all sorts of issues with things not working as expected, then I upgraded to prawn 0.11.1.pre and it sorted it all.
See Prawn Tables: Block is not executing which is pretty much the same question.

Resources