Printing a database report
Steve Steinitz
steinitz at datatactics.com.au
Wed Apr 11 02:10:31 PDT 2007
Hello Andy,
Thanks for your input. I'm going to briefly answer your questions
and also show a solution that a friend of mine offered, having seen my
struggles with expressing my question.
On Tuesday, 10 April 2007 at 8:11 pm, Andy Lee wrote:
> Have you measured to be sure it's the fetches that are taking too
> long and not some accidentally slow drawing code? You're probably
> right -- I'm just asking as a sanity check.
Yes. But its a good point. Its not so much that each fetch takes
very long but I end up doing hundreds of them.
> More importantly, *why* do you have to refetch on every page? Is it
> because you don't know in advance where the page breaks will be?
Yes. I load objects grouped by a couple of attributes and print them
with pretty labels at the top of each page based on the attributs.
Its hard to know how big it will be and also hard to 'keep my place'
for the next page.
> I'm
> wondering why you can't fetch once at the beginning and then
> precompute everything that will be needed for all twenty pages.
That's more or less what I want to do. But rather than 'compute
everything that will be needed', which sounds hard, I want to just
image it all somewhere and print bits of it as requested by the
printing system.
> Aside from the question of when you execute drawing commands to
> render those twenty pages -- is there some data structure you could
> use to store their content, so you don't have to refetch on every
> page?
Yes, I'm sure there is but that's a little project that neither I
nor the client want to pay for at this point. I confess laziness.
> For example, if you dataset is a million numbers, and each
> page contains a different weighted average of those numbers, you
> could compute the 20 weighted averages up front and keep them in an
> array, instead of refetching the million numbers each time you print
> a page.
I may have misled you with earlier statements about computation.
Its more formatting and pagination (my own) -- What titles to draw
for subgroups, whether or not to start a new page after a group
page number, date, headers...
> It might help if we understood something about the computations you
> are doing.
That might be so if I was asking for a way to redo my report, but I'm
not. Practical matters dictate that I am finished with the report, I
just want to print it faster.
> > I have a nebulous idea that I could image the
> > whole report into a second view or perhaps to pdf or even to a
> > low-level graphics-system object... and then, when the printing system
> > calls my drawRect, I return rects of that intermediate object, page by
> > page.
>
> From this it sounds like you might indeed have enough data to render
> the whole report with just one fetch?
Yes that's true and that's my desire -- if I understand you. But I'm
wondering if I'm missing something in what you're saying because I
don't understand the 'enough data to' part of it.
> If so, sure, you could create a big view up front that is the size
> of 20 pages, and pre-image the whole report.
Yes, now you are singing my song :)
> But that view will be expensive memory-wise. I don't think it buys
> you anything in terms of performance.
That's possible. Each page now takes about 5 seconds to print on a
1Ghz G4. I'd like to get it down to three seconds to image and 1
second per page. Even if virtual memory were getting bashed that
would seem achievable.
Now, having addressed your comments to some degree I will present a
solution presented by my friend, Ross, for your perusal. Note especially the
part about creating a PDF. That is along the lines of what I am
seeking and indeed may be 'the go' (as they say here in Oz).
Thanks,
Steve
---------------------------------------------------------------------
Hi Steve,
I had a look at the thread for your printing problem.
My thoughts: Uli Kusterer's first reply to your message seems like the
correct answer to me: When your print view is instantiated cache all
the data your going to need for the report printing.
So can you cache the fetches? Or is this just too much data.
You could (as you suggest) paginate the entire report into a PDF
document, then, on demand, composite individual parts from the PDF
into your print view but that doesn't seem like the right way to go
(to me anyway).
If you do want to create a PDF from a NSView have a look at NSView's
dataWithPDFInsideRect. This will give you a big chunk of PDF data for
your view. Create a NSImage using this data. In your print loop you'll
need to calc the current page offset and draw just the required
portion of the NSImage. Try
[NSImage drawInRect:fromRect:operation:fraction]
with the fromRect set to the correct offset.
Hope this helps.
Ross
More information about the MacOSX-dev
mailing list