无法以正确的分辨率打印
我正在尝试使用 xhtmlrenderer 和 java 打印来打印标签。然而,我似乎无法设置正确的分辨率,因此图像打印得非常尖锐。这是我的打印代码:
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)
}
...这是我的生成代码:
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)
}
我对这里缺少的内容感到茫然。我尝试在 xhtmlrenderer 源和打印机目标上设置 dpi,但似乎没有任何影响。
顺便说一句:我正在尝试在 Mac OS X 上的 Brother QL-560 上进行打印。我能够以相同的尺寸以非常好的质量从预览中打印相同的图像。
更新:添加了图像示例。
更新:使用xhtmlrenderer创建pdf,然后通过pdf-renderer打印(http://java.net/projects/pdf-renderer/)成功了。现在打印已经很完美了。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有例子很难说问题是什么。例如,您尝试打印哪张图像?它有什么分辨率?
为了确保不是打印机驱动程序的问题,我建议打印为 PDF。这有帮助吗?
如果不是:将预览打印为 PDF。看起来怎么样?放大时会发生什么?
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?