Java - How to improve print rendering?

I use CSSBox to render the HTML and CSS web page, and then print it. The application must deal with certain sites. The idea is to make some sites that are not designed for printing that actually print and look readable. This is done by removing menus, titles, etc., Centering content, overriding some styles.

My current approach is to turn the site into 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 the paragraphs look good in every case. This BrowserCanvas descends from JPanel and has drawing methods that I use to render it to a Graphics Graphics object.

The problem is that the printer canvas is usually huge, I think 300 ppp, 600 ppp, etc. Now I have done RenderingHints to force Bicubic interpolation to scale the document to fill all the space on the printed page.

The problem is that you can see that the documents look like a scaled screenshot and do not look very good on the printed page.

Is there a better approach?

Printing method

The printer is accessed using the PrinterJob class.

Then I override the BrowserCanvas print method, which takes the page number. In accordance with this number, I set the scale and translation on the graphic object, making sure that if the last line of text cannot completely fit on the page, it moves to the next page. Then I call super.print (), passing in a graphic object as a parameter, which uses all the scaling and translation.

I thought that scaling would be soft, like everything that happens at a higher resolution, such as font size, but it is more like drawing everything in basic resolution, and then scaling the result to what you want.

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

page 1 out-0.png


page 2 out-1.png

+5
source share
1 answer

I believe that instead you can try creating a new printed web page. Since you are redefining styles, perhaps you can add new media queries or a print style sheet as follows:

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

Or, as before:

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

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

+1
source

Source: https://habr.com/ru/post/1210195/


All Articles