-
Notifications
You must be signed in to change notification settings - Fork 74
HTML CSS
On CoffeeBitmap sample you can see how to render html/css content with Flying Saucer third library and then print the image content on escpos-coffee
The library Flying Saucer is open source and can work with css 2.1. It isn't like css 3 pattern but is good enough.
how to read one html content:
1 - configure maven
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.20</version>
</dependency>
2 - create String html content or create file directly:
String html = "<!DOCTYPE html>\n" +
"<html>\n" +
"<body>\n" +
"\n" +
"<h2>Hello Html</h2>\n" +
"\n" +
"</body>\n" +
"</html>\n";
3 - Create file with html content
tmpStreamxhtml.write(html.getBytes("utf-8"));
tmpStreamxhtml.close();
4 - Get url of the file
String tmpFileURL = temp.toURI().toURL().toExternalForm();
String tmpBaseURL = temp.getParent();
5 - discover the printer area width of your printer reading printer documentation ( in my case is 576 max width) and then make rendering to buffer image
Java2DRenderer render = new Java2DRenderer(tmpFileURL, tmpBaseURL, 576);
BufferedImage image = render.getImage();
6 - Create ImageHelper with same width (576)
ImageHelper imageHelper = new ImageHelper(576,48);
7 - Choose the imageWrapper implementation (escpos algorithm to print images in printer) maybe the printer work better with one (print quickly) or doesn't recognize one or other algorithm then, is better test. Read more on Wiki Images (session - The ImageWrapperInterface implementation)
BitImageWrapper imageWrapper = new BitImageWrapper();
// or
RasterBitImageWrapper imageWrapper = new RasterBitImageWrapper();
// or
GraphicsImageWrapper imageWrapper = new GraphicsImageWrapper();
8 - Choose the bitonal implemantation See if your image work better with bitonal threshold or ordered dither implementation, read more on Wiki Images (session - The Bitonal implementation algorithm)
Bitonal algorithm = new BitonalThreshold();
// or
Bitonal algorithm = new BitonalOrderedDither();
9 - print the image
imageHelper.write(escpos, new CoffeeImageImpl(image),imageWrapper,algorithm);