Forum How do I...?

Retrieve page information through JavaScript API

John
hello,

is it possible to find out the defined margins of a document, or more precisely, each page of a PDF document through the JavaScript API?

I need to position some elements and can't be sure whether the defined page margins stay the same through the whole element.

greetings,
John
mikeday
You can check the page area with the JavaScript box tracking API, but only after layout has finished and the PDF has been generated, so it requires a second conversion if you want to make changes.
John
hello mike,

I have already set this up, so I'm already doing two-step conversion process. Is there smthg like a page object? Or do you mean the coordinates of the first "box" on a page give away the margins? I still don't know the available height though, since dimensions can differ from page to page, no?

greetings,
john
mikeday
You can access PDF.pages[0], a box object with the usual x/y/w/h fields that give the size of the available page area, from which you can infer the margins. :)
John
That was quick. Thanks Mike! :)
mrwarper
That PDF.pages[0] thing sounds just like what I was asking for in this other thread. However, I haven't been able to find any documentation on it in the website. Could you please link to that object reference/specifications?
TIA,
mikeday
It is not yet officially documented, although there are examples on the forum, such as this sample showing how to use the API to create changebars.
mrwarper
Cool :)

From what I see in that link and this one the whole thing seems to be focused on the rendered PDF, which would be very useful for two-+ pass processes indeed. Still, I believe at least some data should be available from the get go.

I mean, if you don't use "@page { size:" in CSS (I normally don't), PDF generation is started based either on some default or on the --page-size switch, i.e. a value set one single time at invocation time, and all subsequent work initially depends on that. Doesn't it make sense to have that information available from the beginning? (Besides, I'm guessing that bit would be easy to take care of when implementing any APIs.) How would a suggestion in that direction be taken? ;)
mikeday
It would be theoretically possible to expose that information yes, we just don't have a JavaScript API for doing so at the moment. (Although you can use CSS media queries to apply different style rules based on the page size, which can be helpful for some things).
mrwarper
Of course. It's just, as you can read in the first thread I linked, that I find it rather annoying not to be able to use precise media properties* which would be undoubtedly useful. I believe Prince was born to address limitations in CSS support and sometimes in the specs themselves (hence its extensions), so this is my $0.02 in that regard.

*The CSS specs makers think it's good to make media queries with min- or max- height or width, but not with plain vanilla dimensions? We can always make convoluted queries like "@media (min-height: target value) and (max-height: same value)", so why not let us do what we are already doing, in a better, simpler way? Makes no sense to me :)