Force landscape orientation for printing via CSS

707 views
Skip to first unread message

Laszlo Szilagyi

unread,
Mar 7, 2011, 8:28:48 AM3/7/11
to Flying Saucer Users
Hi,

I'm about to generate PDF files from URLs. Some of the web pages
contains dynamic tables, which can be wider than normal. So sometimes
the edge is cut off.

I need to figure out a solution to handle this situation. I can
compare the page width and the width on the PDF with this code:

renderer = new ITextRenderer(dotsPerPoint, dotsPerPixel);
renderer.setDocument(doc, url);
renderer.layout();

// width of the web page
d = renderer.getRootBox().getPaintingInfo().getOuterMarginCorner();

// width of the PDF page with the current settings
paperWidth =
((PageBox)renderer.getRootBox().getLayer().getPages().get(0)).getContentWidth(null);


Two questions:
1., How can I dynamically change the CSS for a renderer through the
API?
So when I detect that the table is too wide, I may want to switch to
another CSS, which is using landscape orientation.

2., How can I force the landscape mode for printing?
Here's what I've tried:

@page {size: 8.5in 11in landscape; margin: 0.25in;}
body{line-height:1;background-color:red;size: 8.5in 11in
landscape;margin: 0.25in;}

According to the documentation, these should work but they don't work
for me.

Thanks,
Laci

Peter Brant

unread,
Mar 9, 2011, 9:37:17 AM3/9/11
to flying-sa...@googlegroups.com
There isn't API to change the in-memory representation of the CSS
files. Your best is probably to modify the DOM and layout/render
again (although other users have found ways around this). You can use
named pages to add a bit of dynamic page size behavior though too.

Regarding (2) that should work. Do you have a sample file that
doesn't work for you?

Pete

Reply all
Reply to author
Forward
0 new messages