2

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.

thoredge
  • 12,237
  • 1
  • 40
  • 55

1 Answers1

1

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?

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • The two prints on the image above uses the same image (first with java print, second with Mac Preview) with noticable differences. Also printing to a regular laser printer yields the same problem. That makes me believe its xhtmlrenderer that is behaving strangely. I've not gotten round to print to pdf yet. – thoredge May 04 '11 at 11:04
  • The lower print out looks like a vector image (not a pixel image). How did you create that? – Aaron Digulla May 06 '11 at 10:25
  • It is actually the exact same image in resolution 341x108 (with anti-aliasing) which the printer seems to be able to use very efficiently – thoredge May 06 '11 at 10:32