Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How to improve rendering when printing?

I'm using CSSBox to render a web page HTML and CSS and then print it. The application must deal with specific sites. The idea is to make some sites that aren't designed to be printable actually print and look readable. This is done by removing menus, headers, etc. centering content, overriding some styles.

My current approach is to render the site to a component that is part of the CSSBox API called BrowserCanvas. I adjust the width of the canvas for each supported site to ensure that paragraphs look good in each case. This BrowserCanvas descends from JPanel and has the paint methods that I use to render it to the printer Graphics object.

The problem is that the printer canvas is usually huge, think 300 ppp, 600 ppp, etc. What I'm done now is set a RenderingHints to force Bicubic interpolation to scale the document up to fill all space in the printed page.

The problem is that you can see that the documents is like an up scaled screenshot, and does not look very good in the printed page.

Is there a better approach?

Print Method

Access to printer is obtained by the PrinterJob class.

Then I override the BrowserCanvas print method that accepts a page number. According to that number I set scale and translation on the graphic object, taking care that if the last line of text cannot fit entirely in the page it is moved to the next page. Then I call super.print() passing as parameter the graphic object that has all the scaling and translation applied.

I thought the scaling would be soft, like doing everything happen at a higher resolution, like font sizes, but it looks more like draw everything in a base resolution then upscaling the result to the desired one.

Example of a modified page (using the CSSBox documentation site for testing):

page 1 out-0.png


page 2 out-1.png

like image 515
Hatoru Hansou Avatar asked Nov 09 '22 21:11

Hatoru Hansou


1 Answers

I believe you could try rendering a new printable webpage instead.Since you are overriding the styles maybe you could add new media queries or a print stylesheet like this:

@media print { 
 /* All your print styles go here */
 #header, #footer, #nav { display: none !important; } 
}

Or,In the old way:

<link href="/print.css" rel="stylesheet" media="print" type="text/css" />

and then you can use the print dialog from any browser of the OS

like image 159
Koustav Ray Avatar answered Nov 15 '22 10:11

Koustav Ray