Java: how to use xulrunner to convert html to postscript?

59 views
Skip to first unread message

danieloli...@gmail.com

unread,
Feb 19, 2014, 12:28:14 PM2/19/14
to
Hello,

I wonder if is possible,use xulrunner to convert html to postscript or pcl in java? and how would I do this implementation with java 7, firefox 26 and xulrunner 1.80.0.1 26? Thanks in advance

Daniel Oliveira

Christian Sell

unread,
Feb 20, 2014, 5:50:00 AM2/20/14
to
what makes you think that xulrunner can be used to convert to postscript or pcl? Its a web browser, not a printer driver..

in addition, XULRunner is what you use for embedding (if thats what you want to do). Firefox is the standalone browser. Both share the same version numbering, so if you want the XULRunner engine corresponding to Firefox 26, you'll want XULRunner 26.

Finally: embedding XULRunner is not easy. IF you are really sure that it can help you with regard to postscript or pcl (which I am not), then I suggest you look for some open source code that already covers this

Daniel Oliveira

unread,
Feb 20, 2014, 10:16:50 AM2/20/14
to
I believe that when you have to print a document in browser, it did not just send that document to the printer, it converts into a common format printing (PCL believe) because there are printers (as is my case) not acceptance any MIME Types therefore I needed of a converser an html document to postscript so that I can manage this impression. But I found a plausible solution, I'll post the code below, briefly I incorporated xulrunner10 using eclipse SWT and converted the result into a png image for printing. My job now is to find a more current version of xulrunner compatible with Eclipse SWT 4.3 to interpret the latest features of html.

Download eclipse SWT (http://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/11.0/runtimes/)
and add in eclipse project

Download Xulrunner 10 (http://www.eclipse.org/swt/)
and extract to C:\xulrunner


package core;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.apache.commons.io.IOUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import core.ImageConverter;

public class BrowserSWT {

public BrowserSWT(){

final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("My Browser");
shell.setSize(500, 500);
shell.setLayout(new GridLayout(1,false));

Browser browser = new Browser(shell, SWT.MOZILLA);

InputStream input = null;
String html = null;
try {

input = new FileInputStream(new File("src/teste.html"));
html = IOUtils.toString(input,"UTF-8");

} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

browser.setText(html);

browser.setLayoutData(new GridData(GridData.FILL_BOTH));

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();

Image img = new Image(display, 500, 500);
GC gc = new GC(img);
browser.print(gc);
gc.dispose();

BufferedImage imageNew = null;

ImageConverter converter = new ImageConverter();

imageNew = converter.convertToAWT(img.getImageData());

JLabel label = new JLabel(new ImageIcon(imageNew));

JOptionPane.showMessageDialog(null, label, "Ingresso",JOptionPane.PLAIN_MESSAGE, null);
}


public static void main(String[] args){

System.setProperty("org.eclipse.swt.browser.XULRunnerPath", "C:\\xulrunner");//-Dorg.eclipse.swt.browser.XULRunnerPath=C:\xulrunner

BrowserSWT browser = new BrowserSWT();
}
}

############### Converter SWT Image to AWT BufferImage ####################

package core;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

public class ImageConverter {

public BufferedImage convertToAWT(ImageData data) {
ColorModel colorModel = null;
PaletteData palette = data.palette;
if (palette.isDirect) {
colorModel = new DirectColorModel(data.depth, palette.redMask,
palette.greenMask, palette.blueMask);
BufferedImage bufferedImage = new BufferedImage(colorModel,
colorModel.createCompatibleWritableRaster(data.width,
data.height), false, null);
WritableRaster raster = bufferedImage.getRaster();
int[] pixelArray = new int[3];
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
int pixel = data.getPixel(x, y);
RGB rgb = palette.getRGB(pixel);
pixelArray[0] = rgb.red;
pixelArray[1] = rgb.green;
pixelArray[2] = rgb.blue;
raster.setPixels(x, y, 1, 1, pixelArray);
}
}
return bufferedImage;
} else {
RGB[] rgbs = palette.getRGBs();
byte[] red = new byte[rgbs.length];
byte[] green = new byte[rgbs.length];
byte[] blue = new byte[rgbs.length];
for (int i = 0; i < rgbs.length; i++) {
RGB rgb = rgbs[i];
red[i] = (byte) rgb.red;
green[i] = (byte) rgb.green;
blue[i] = (byte) rgb.blue;
}
if (data.transparentPixel != -1) {
colorModel = new IndexColorModel(data.depth, rgbs.length, red,
green, blue, data.transparentPixel);
} else {
colorModel = new IndexColorModel(data.depth, rgbs.length, red,
green, blue);
}
BufferedImage bufferedImage = new BufferedImage(colorModel,
colorModel.createCompatibleWritableRaster(data.width,
data.height), false, null);
WritableRaster raster = bufferedImage.getRaster();
int[] pixelArray = new int[1];
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
int pixel = data.getPixel(x, y);
pixelArray[0] = pixel;
raster.setPixel(x, y, pixelArray);
}
}
return bufferedImage;
}
}

public ImageData convertToSWT(BufferedImage bufferedImage) {
if (bufferedImage.getColorModel() instanceof DirectColorModel) {
DirectColorModel colorModel = (DirectColorModel) bufferedImage
.getColorModel();
PaletteData palette = new PaletteData(colorModel.getRedMask(),
colorModel.getGreenMask(), colorModel.getBlueMask());
ImageData data = new ImageData(bufferedImage.getWidth(),
bufferedImage.getHeight(), colorModel.getPixelSize(),
palette);
WritableRaster raster = bufferedImage.getRaster();
int[] pixelArray = new int[3];
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
raster.getPixel(x, y, pixelArray);
int pixel = palette.getPixel(new RGB(pixelArray[0],
pixelArray[1], pixelArray[2]));
data.setPixel(x, y, pixel);
}
}
return data;
} else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
IndexColorModel colorModel = (IndexColorModel) bufferedImage
.getColorModel();
int size = colorModel.getMapSize();
byte[] reds = new byte[size];
byte[] greens = new byte[size];
byte[] blues = new byte[size];
colorModel.getReds(reds);
colorModel.getGreens(greens);
colorModel.getBlues(blues);
RGB[] rgbs = new RGB[size];
for (int i = 0; i < rgbs.length; i++) {
rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF,
blues[i] & 0xFF);
}
PaletteData palette = new PaletteData(rgbs);
ImageData data = new ImageData(bufferedImage.getWidth(),
bufferedImage.getHeight(), colorModel.getPixelSize(),
palette);
data.transparentPixel = colorModel.getTransparentPixel();
WritableRaster raster = bufferedImage.getRaster();
int[] pixelArray = new int[1];
for (int y = 0; y < data.height; y++) {
for (int x = 0; x < data.width; x++) {
raster.getPixel(x, y, pixelArray);
data.setPixel(x, y, pixelArray[0]);
}
}
return data;
}
return null;
}

static ImageData createSampleImage(Display display) {
Image image = new Image(display, 100, 100);
Rectangle bounds = image.getBounds();
GC gc = new GC(image);
gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
gc.fillRectangle(bounds);
gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
gc.fillOval(0, 0, bounds.width, bounds.height);
gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
gc.drawLine(0, 0, bounds.width, bounds.height);
gc.drawLine(bounds.width, 0, 0, bounds.height);
gc.dispose();
ImageData data = image.getImageData();
image.dispose();
return data;
}

}


Thanks!

Daniel Oliveira

Christian Sell

unread,
Feb 20, 2014, 12:24:46 PM2/20/14
to
we have done something similar, actually also from within SWT/Java. Heres some caveats:

1. If you want a version of XULRunner > 10, you must move up to at least Eclipse 4.4M5. That version supports XULRunner 24. You can, however, manipulate your Eclipse target to include the 4.4M5 SWT bundles only

2. your solution won't be able to handle large images/webpages. If the generated image in itself will not blow up your app because of memory consumption, the conversion to AWT will surely do so.

Daniel Oliveira

unread,
Feb 20, 2014, 1:39:39 PM2/20/14
to
I was wrong, this solution can't solve all my problems. I necessary that the browser not appear, only need to capture the image, is this possible?

Daniel Oliveira

unread,
Feb 20, 2014, 1:40:51 PM2/20/14
to
Note: The images are small, I just need to print labels on a Zebra printer

Christian Sell

unread,
Feb 20, 2014, 1:54:28 PM2/20/14
to
browser.setVisible(false)

Daniel Oliveira

unread,
Feb 20, 2014, 2:15:30 PM2/20/14
to
Em quinta-feira, 20 de fevereiro de 2014 15h54min28s UTC-3, Christian Sell escreveu:
> browser.setVisible(false)

browser.setVisible hidden the content, but box window yet appear. maybe describing my problem can help them help. I Need receive labels in html format via webservice in a java SE application and print the same in an automated fashion by controlling the printing and return if the document is printed or not. The problem is to convert the html to a print format that the printer accepts, ie, postscript or image.
Reply all
Reply to author
Forward
0 new messages