I'm trying to learn how to expand Prawn to support a new PDF object
(actually I need a little object tree, but let's stick to a single
object now). As I understand I have to call ref() on the
Prawn::Document object but is that enough? I can't arrive at
generating any objects this way in resulting pdf.
Best
Piotr
Calling ref() is all you should need to do. Make sure you pass it a ruby
object that can be translated into a corresponding PDF object.
What type of object are you adding and what makes you think it's not
ending up in the generated PDF? For it to have any effect, make sure it
is referenced from another object that's part of the document. Adding an
'orphan' object will have no impact.
-- James Healy <jimmy-at-deefa-dot-com> Mon, 03 Aug 2009 23:18:08 +1000
How your new object fits into the PDF depends on what kind of object it
is.
Assuming my_function() is within the scope of a PDF::Document object, it
can access the current page object via the @current_page instance
variable (see lib/prawn/document.rb).
Also check out the @pages, @root and @info instance variables,
initialised in lib/prawn/document.rb. These are all objects near the top
of the PDF object tree and *may* be suitable places to link your object.
If you want to add content to a page, skip creating an object and use
the add_content() method, defined in lib/prawn/document/internals.rb.
-- James Healy <jimmy-at-deefa-dot-com> Tue, 04 Aug 2009 00:14:15 +1000
> P.S.
> I'd gladly take a look at
> http://prawn.lighthouseapp.com/projects/9398/tickets/122-hacking-guide
> however it does not seem publicly available.
You're doing interesting low level things, so no worries about
discussing them here.
The HACKING file only describes how to do basic things (get code from
git, etc). I need to write a better one that describes low level
features.
-greg
No worries, it's nice to see someone willing to put in the work to get
up to speed on PDFs.
It might make things easier if you outline what it is your trying to add
to the first page. Text? an image? graphics?
-- James Healy <jimmy-at-deefa-dot-com> Tue, 04 Aug 2009 22:50:48 +1000
Assuming DeviceN graphics are possible, they're probably a good first
step. Adding graphics to a page is generally much simpler than embedding
images and other objects.
Can you explain DeviceN to me? I grok RGB and CMYK colour spaces, but
that's about the limit of my colour knowledge. A better understanding of
DeviceN will help me interpret the relevant parts of the PDF spec and
see what's possible.
To add content to a page using the add_content() method you pass a series
of arguments, followed by an operator. Using set_fill_color as an
example, when setting an RGB colour we pass "arg1 arg2 arg3 rg".
arg1 arg2 and arg3 are the RGB components of the colour and 'rg' is the
operator that uses the arguments to set the active *RGB* colour. The 'k'
operator performs a similar function for CMYK colours and takes 4
arguments.
The trick will be to work out if there's operators for alternative
colour spaces.
-- James Healy <jimmy-at-deefa-dot-com> Wed, 05 Aug 2009 09:16:23 +1000
It *sounds* like a feature that would make prawn useful to a new segment
of people (people in printing, etc), so thanks for investigating it.
It sounds like you're close to a working example.
I'ev covered how to add args and operators to the content stream for
current page.
How are you going with adding objects to the page resources? Something
like the following is probably what you want:
page_resources[:ColorSpace] = {:MyCS => ref(...)}
page_resources() is a helper method defined in
lib/prawn/document/internals.rb
You could also emulate the page_fonts() or page_xobjects() helpers like
so:
def page_colorspaces
page_resources[:ColorSpace] ||= {}
end
Then add your new colour space with something like:
page_colorspaces[:MyCS] = ref(...)
-- James Healy <jimmy-at-deefa-dot-com> Thu, 06 Aug 2009 00:06:33 +1000
I'm happy to keep answering any questions you have on prawn or PDF file
structure though!
-- James Healy <jimmy-at-deefa-dot-com> Fri, 07 Aug 2009 14:07:40 +1000