javascalaprintingxhtmlrenderer

Unable to print in correct resolution


I'm trying to print labels using xhtmlrenderer and java printing. However I don't seem to be able to set the resolution correct so the image is printed all edgy. This is my printing code:

val paperWidth = 62.0f
val paperHeight = 70.0f
def print(printable: Printable) {
  val printJob = PrinterJob.getPrinterJob
  printJob.setPrintService(printService)

  val pageFormat = printJob.defaultPage
  pageFormat.setOrientation(PageFormat.LANDSCAPE)
  val labelPaper = pageFormat.getPaper
  labelPaper.setImageableArea(mm2points(0f), mm2points(0f), mm2points(paperWidth), mm2points(paperHeight))
  labelPaper.setSize(mm2points(paperWidth), mm2points(paperHeight))
  pageFormat.setPaper(labelPaper)

  val book = new Book
  book.append(printable, pageFormat)
  printJob.setPageable(book)

  val attributeSet = new HashPrintRequestAttributeSet
  attributeSet.add(new PrinterResolution(300, 300, ResolutionSyntax.DPI))
  printJob.print(attributeSet)
}

... and this is my generation code:

def apply(url:URL):Printable = {
  val renderer = new XHTMLPanel(UserAgent)
  renderer.getSharedContext.setDPI(300f)
  renderer.getSharedContext.setUserAgentCallback(UserAgent)
  renderer.getSharedContext.setCss(new StyleReference(UserAgent))
  renderer.setDocument(url.toExternalForm)
  new XHTMLPrintable(renderer)
}

I'm at loss at what's missing here. I try to set dpi both on xhtmlrenderer source and printer target, but nothing seem to make any impact.

BTW: I'm trying to print on a Brother QL-560 on Mac OS X. I'm able to print the same image from preview in the same size in very good quality.

Update: Added image example.Difference between print from Mac Preview and java print api

Update: Using xhtmlrenderer to create pdf and then printing through pdf-renderer (http://java.net/projects/pdf-renderer/) did the trick. The print is now perfect.


Solution

  • Hard to say what the problem is without an example. For example, which image do you try to print? What resolution does it have?

    To make sure it's not the printer driver, I suggest to print to a PDF. Does that help?

    If not: Print the preview to a PDF. How does that look? What happens when you zoom in?