JS
unread,Feb 14, 2008, 10:10:25 AM2/14/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to PDF::Writer
Hi guys,
I've been working with PDF::Writer actively for about a week now and
I've got a few questions, and I'd also like to share with you how I'm
attempting to build my documents.
First simple question; I keep exporting my images from Photoshop but
they don't load in PDFs generated by PDF::Writer. What features should
I avoid when exporting from Photoshop? (Annoyingly the images are EPS'
which I can't load straight into PDF::Writer - but that's a limitation
I'll live with! :) )
The second question is about my fill colour not being applied. But for
this I need to explain how I create my PDFs. First I have a list of
document types that I need to create (invoices, pick-lists, etc) and
then I'll have different brandings for the clients using the system.
So for each client I'll need a template that creates their branding,
and then I need to write the invoice-data onto that document.
I've figured out that I've created a system template, a client
template and the document template. In the client template, I create
inject client specific methods into PDF::writer, and in the sytem
template I move aside PDF::Writers initialize for my own one that runs
the client specific methods. In the document type template I simply
create a new PDF object and start writing to that.
This is before I found out about PDF::Reader (and it seems it's not
ready for production anyway?).
Here's the three files (sorry if they don't work out of the box, I've
been cutting-and-pasting quite a lot).
invoice.rb
=================
require "pdf/writer"
require "client_template" # Get the currently branded PDF template
pdf = PDF::Writer.new
pdf.document_type("Invoice")
pdf.document_number("12345")
pdf.document_date("2008-02-xx")
pdf.text "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Integer orci. In hac habitasse platea dictumst. Nulla facilisi. Mauris
ultricies augue a risus. Donec nulla. Nulla tincidunt fermentum dolor.
Integer ornare nunc ac libero. Vestibulum ante ipsum primis in
faucibus orci luctus et ."
pdf.start_new_page
pdf.text "This is a piece of wrapping text. Yet more text on the
second page. Adding more and more text will eventually make this text
wrap around, and that's exactly the point of this text. That it
wraps."
pdf.start_new_page
pdf.text "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Integer orci. In hac habitasse platea dictumst. Nulla facilisi. Mauris
ultricies augue a risus. Donec nulla. Nulla tincidunt fermentum "
pdf.save_as("invoice.pdf")
=================
client_template.rb
=================
# Contains client specific stuff such as headers, footers, logos.
# Should also contain things like author etc.
require "system_template"
module PDF
class Writer
def run_on_initialize
initialize_top_bottom_margins(100, 40, 160) # Calculated from
the edges inwards
header
header_symbol
footer
page_numbering
end
def header
header_text_row_1 = "Address goes here"
header_text_row_2 = "Telephone, email, fax"
self.save_state
self.open_object do | header |
self.select_font "Helvetica"
self.fill_color discrete_color
# x, y, width, text, size, justification, angle, test
self.add_text_wrap(0, 770, 595, header_text_row_1,
10, :center)
self.add_text_wrap(0, 755, 595, header_text_row_2,
10, :center)
self.close_object
self.add_object(header, :all_pages)
end
self.restore_state
end
def header_symbol
self.open_object do | header_symbol |
# filename, x, y, width,
height, link
logo = self.add_image_from_file("a_logo.jpg", 75, 790, 450)
self.close_object
self.add_object(header_symbol, :all_pages)
end
end
def footer(footer_text = nil)
footer_text ||= "Footer " + Time.now.to_s(:db)
self.open_object do | footer |
self.select_font "Helvetica"
self.fill_color discrete_color
self.add_text_wrap(0, 30, 595, footer_text, 12, :center)
self.close_object
self.add_object(footer, :all_pages)
end
self.restore_state
end
# Sets the document type string; Invoice, Pick-list etc
def document_type(document_type)
self.save_state
self.select_font "Helvetica"
self.fill_color black
# x, y, width, text, size, justification,
angle, test
self.add_text_wrap(72, 720, 300, document_type, 22, :left)
self.restore_state
end
# Sets the document number
def document_number(number)
self.save_state
self.select_font "Helvetica"
self.fill_color black
# x, y, width, text, size, justification,
angle, test
self.add_text_wrap(72, 705, 300, "Number: <b>#{number}</b>",
12, :left)
self.restore_state
end
def discrete_color
Color::RGB::Grey50
end
def black
Color::RGB::Black
end
end
end
=================
system_template.rb
=================
module PDF
class Writer
alias_method :initialize_standard, :initialize
def initialize(options = {})
options[:paper] ||= "A4" # A4 Height: 841.89pt, Width: 595.28pt
initialize_standard(options)
self.run_on_initialize
end
def initialize_top_bottom_margins(top_margin, bottom_margin,
first_page_top_margin = nil)
self.top_margin = top_margin
self.bottom_margin = bottom_margin
if first_page_top_margin
self.y = self.page_height - first_page_top_margin
else
self.y = self.absolute_top_margin
end
end
end
end
=================
The problem I have with this setup is that I can't seem to return to
black color when I've used the grey color (discrete_color - method).
I've moved things around in run_on_initialize and it seems that if I
write all grey stuff at the end, it works, but I cannot write a grey
thing, and then a black thing. This pertains to all of the Loose
Content Objects. I've tried to manually set the color, both before
writing black, and after writing grey, but to no avail.
Another tiny niggle I've noticed is that the page numbering seems to
report + 1 total pages, possibly because I'm writing to the document
before it's actually been created.
Please ask if I've not been clear enough with my explanations.
Thanks for any help!
/JS