Two Questions; PNG and Colors

3 views
Skip to first unread message

JS

unread,
Feb 14, 2008, 10:10:25 AM2/14/08
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

Gregory Brown

unread,
Feb 14, 2008, 10:17:13 AM2/14/08
to pdf-w...@googlegroups.com
On Thu, Feb 14, 2008 at 10:10 AM, JS <the....@gmail.com> wrote:

I'll have to look at these other issues later (or wait for others to
help), but....

> 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.

This is a known issue:

http://stonecode.svnrepository.com/ruby_pdf/trac.cgi/ticket/1

The current workaround is to call stop_page_numbering before rendering
your document. I think this causes the numbering to behave properly.

We'll be trying to squeeze out another PDF::Writer release in the next
month or so, and can revisit this and other bug reports.

-greg

JS

unread,
Feb 14, 2008, 10:27:01 AM2/14/08
to PDF::Writer


On Feb 14, 3:17 pm, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:
> On Thu, Feb 14, 2008 at 10:10 AM, JS <the.jo...@gmail.com> wrote:
>
> I'll have to look at these other issues later (or wait for others to
> help), but....

Much appreciated. If there's something else I'm doing wrong, please
feel free to point it out.

> > 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.
>
> This is a known issue:
>
> http://stonecode.svnrepository.com/ruby_pdf/trac.cgi/ticket/1
>
> The current workaround is to call stop_page_numbering before rendering
> your document. I think this causes the numbering to behave properly.
>
> We'll be trying to squeeze out another PDF::Writer release in the next
> month or so, and can revisit this and other bug reports.

Thanks for the heads up on this one!

/JS

JS

unread,
Feb 14, 2008, 10:45:46 AM2/14/08
to PDF::Writer


On Feb 14, 3:10 pm, JS <the.jo...@gmail.com> wrote:
> 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?

Just got an email from David suggesting it might be the transparency,
so I tried the same image with transparency on and off, and David's
right. The one without transparency works. Which is quite annoying as
one reason to use PNG over JPEG, in my case at least, is to use
transparency.

Oh, well... at least I'll be able to export the images as I want
now. :) Thanks David!

/JS

JS

unread,
Feb 14, 2008, 11:27:20 AM2/14/08
to PDF::Writer
Guys,

I've updated the example. You can download a ZIP from here:
http://madhouse.selincite.com/js/pdf_color/
You can also see the PDF without downloading the ZIP.

In the PDF, the first footer is black, and subsequent ones are gray.
They're gray because I've used gray in the header. If I move the call
to the footer, below the call to the header, all footers will be gray.

IMO, they should all be black, as they're defined to be black,
regardless of when they're called.

Strangely if I view the PDF in Adobe Reader, there's a blank first
page, if I view it in Apple's Preview, then it displays as I'd expect
it to display, obviously with the above color issue.

Hope this is of any help in helping me.

Cheers,

/JS

JS

unread,
Feb 15, 2008, 7:30:03 AM2/15/08
to PDF::Writer


On Feb 14, 4:27 pm, JS <the.jo...@gmail.com> wrote:
> Strangely if I view the PDF in Adobe Reader, there's a blank first
> page, if I view it in Apple's Preview, then it displays as I'd expect
> it to display, obviously with the above color issue.

I found the problem; it's the line
self.restore_state
in the footer method that doesn't have a corresponding self.save_state
call. So if you simply comment that line out, the PDF looks well in
Acrobat Reader too. My bad!

/JS

JS

unread,
Feb 15, 2008, 11:08:32 AM2/15/08
to PDF::Writer
Here I am talking to myself again... :D

On Feb 14, 4:27 pm, JS <the.jo...@gmail.com> wrote:
> Guys,
>
> I've updated the example. You can download a ZIP from here:http://madhouse.selincite.com/js/pdf_color/
> You can also see the PDF without downloading the ZIP.
>
> In the PDF, the first footer is black, and subsequent ones are gray.
> They're gray because I've used gray in the header. If I move the call
> to the footer, below the call to the header, all footers will be gray.
>
> IMO, they should all be black, as they're defined to be black,
> regardless of when they're called.

A bit of progress.
In my example, I'm using two methods to define two colors (this is so
that I'll be consistent all across the documents). If I discard these
two methods and call the Color class directly, and with "normal
colorful" names instead, everything works as expected. I've changed
the two calls to fill_color to use, for example, Color::RGB::Green and
Color::RGB::Red and now everything works as advertised... :)

It even works when I put those colors into the methods. The problem
_seems_ to be with the grey color I used..?!

/JS

Gregory Brown

unread,
Feb 15, 2008, 11:32:17 AM2/15/08
to pdf-w...@googlegroups.com
On Fri, Feb 15, 2008 at 11:08 AM, JS <the....@gmail.com> wrote:
>
> Here I am talking to myself again... :D

Sorry. I'll have to take a look at this over the weekend. As much as
Mike and I will try to do what we can to help support PDF::Writer,
we're pretty well swamped with other projects, and plan to operate
more in batch mode on things like this than anything (unless we have
an obvious answer for questions right away).

For this reason, hopefully some folks who know the answers to
questions like these will speak up, but if not... we'll get to it,
just... with some delay.

-greg

Austin Ziegler

unread,
Feb 15, 2008, 12:13:10 PM2/15/08
to pdf-w...@googlegroups.com
On Fri, Feb 15, 2008 at 11:08 AM, JS <the....@gmail.com> wrote:
> It even works when I put those colors into the methods. The problem
> _seems_ to be with the grey color I used..?!

No. The color itself is fine. Try changing your objects so that they
call #save_state and #restore_state at the beginning and end; also,
change the #fill_color call to #fill_color! (PDF::Writer tries to be
smart about when it writes colour changes out with #fill_color;
#fill_color! forces a colour change).

I'd start with the #fill_color/#fill_color! change and then consider
the #save_state/#restore_state stuff.

-austin
--
Austin Ziegler * halos...@gmail.com * http://www.halostatue.ca/
* aus...@halostatue.ca * http://www.halostatue.ca/feed/
* aus...@zieglers.ca

JS

unread,
Feb 15, 2008, 12:24:43 PM2/15/08
to PDF::Writer


On Feb 15, 4:32 pm, "Gregory Brown" <gregory.t.br...@gmail.com> wrote:
Greg, sorry if that line came across as I don't appreciate your
support, I truly do. We're all swamped, so I'm not expecting my issues
to be put on high-priority. I just found it a bit funny that I kept
improving on my own problems. :P

Take your time!

/JS

JS

unread,
Feb 15, 2008, 12:30:19 PM2/15/08
to PDF::Writer


On Feb 15, 5:13 pm, "Austin Ziegler" <halosta...@gmail.com> wrote:
> On Fri, Feb 15, 2008 at 11:08 AM, JS <the.jo...@gmail.com> wrote:
> > It even works when I put those colors into the methods. The problem
> > _seems_ to be with the grey color I used..?!
>
> No. The color itself is fine. Try changing your objects so that they
> call #save_state and #restore_state at the beginning and end; also,
> change the #fill_color call to #fill_color! (PDF::Writer tries to be
> smart about when it writes colour changes out with #fill_color;
> #fill_color! forces a colour change).
>
> I'd start with the #fill_color/#fill_color! change and then consider
> the #save_state/#restore_state stuff.
>

Thanks for that, Austin. I will try your suggestions the next time I
see these 'anomalies' popping up.
I did use save_state/restore_state previously, and I still got these
errors. I removed the statements as they didn't affect the problem. I
did not know about fill_color!'s existance, so I'll definitely try
that.

Thanks!

/JS

JS

unread,
Feb 18, 2008, 10:48:44 AM2/18/08
to PDF::Writer
Just wanted to confirm that #fill_color! works very well, and to say
thanks to you all! :)

/JS
Reply all
Reply to author
Forward
0 new messages