Can't generate label
joaoribeirost opened this issue · 5 comments
I've added the names example in a view with the extension .pdf.prawn, it iterates all the names but it doesn't output the labels on the page! I'm feeling a little dumb not being able to make it work.
If i have this:
require 'prawn/labels'
names = ["Jordan", "Chris", "Jon", "Mike", "Kelly", "Bob", "Greg"]
Prawn::Labels.generate("list.pdf", names, :type => "Avery7160") do |pdf, name|
puts "In"
pdf.text name
end
pdf.text names.inspect
It will print "In" for each name, and generate a pdf with the Names array. However it will not generate the labels.
Hey @joaoribeirost,
I assume you are trying to generate labels in a Rails project. For that you should refer to this example in the README:
labels = Prawn::Labels.render(names, :type => "Avery5160") do |pdf, name|
pdf.text name
end
send_data labels, :filename => "names.pdf", :type => "application/pdf"
That code should go in a controller method and doesn't need an associated view. Notice we are using Prawn::Labels.render
here rather than Prawn::Labels.generate
. Let me know if you have any questions 🐼
So if i understood correctly i don't need to call the .pdf page as usual on prawn, instead i need to call a method on a controller that will send the data, is that correct?
@joaoribeirost not sure that's exactly what I was saying. Let me try to break it down again:
Let's assume you are working in the LabelController
controller and the action name you are using is called names
. In this example the controller code would look like this:
class LabelController < ApplicationController
def names
labels = Prawn::Labels.render(names, :type => "Avery5160") do |pdf, name|
pdf.text name
end
send_data labels, :filename => "names.pdf", :type => "application/pdf"
end
end
You don't need to create any views in app/views/label
. The PDF data will be sent directly from the controller. Does that help?
Yes! It helped thank you! I was having some troubles making it work.
One last question, sorry, with this method the browser just opens the download pop-up, is there a way to make it open in the browser tab or is this my browser problem?
Great! To send the PDF inline just add the :disposition => "inline"
option to the send_data
method:
send_data labels, :filename => "names.pdf",
:type => "application/pdf",
:disposition => "inline"
Good luck!