高分请教---有谁知道怎样实现Java打印PDF,Excel,HTML等文件
这是我的源码:
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
public class PrintTestByCommand {
/**
* @param args
*/
public static void main(String args[]) throws Exception {
String filename = args[0];
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
// PrintService printService[] =
// PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
DocFlavor printerFlavor[] = defaultService.getSupportedDocFlavors();
if(printerFlavor!=null){
for(int i=0; i < printerFlavor.length; i++)
System.out.println(defaultService.getName()+" surpport type: "+i+" - "+printerFlavor[i].getMimeType());
}
if (!defaultService.isDocFlavorSupported(flavor)) {
System.out.println("Printer dosn't surpport the flavor:"+flavor.getMediaType());
}
if (defaultService != null &&defaultService.isDocFlavorSupported(flavor)) {
DocPrintJob job = defaultService.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
System.out.println("print data: "+doc.getPrintData());
System.out.println("print data attributs: "+doc.getAttributes());
job.print(doc, pras);
Thread.sleep(10000);
}else
System.out.println("Printer dosn't surpport the flavor:"+flavor.getMimeType());
System.exit(0);
}
}
但是我执行结果是
D:\TaiFook\MSS Surport\iReport\src\APP-INF\classes>java PrintTestByCommand E:\\I
TSR.pdf
hp deskjet 9300 series surpport type: 0 - image/gif
hp deskjet 9300 series surpport type: 1 - image/gif
hp deskjet 9300 series surpport type: 2 - image/gif
hp deskjet 9300 series surpport type: 3 - image/jpeg
hp deskjet 9300 series surpport type: 4 - image/jpeg
hp deskjet 9300 series surpport type: 5 - image/jpeg
hp deskjet 9300 series surpport type: 6 - image/png
hp deskjet 9300 series surpport type: 7 - image/png
hp deskjet 9300 series surpport type: 8 - image/png
hp deskjet 9300 series surpport type: 9 - application/x-java-jvm-local-objectref
hp deskjet 9300 series surpport type: 10 - application/x-java-jvm-local-objectref
hp deskjet 9300 series surpport type: 11 - application/octet-stream
hp deskjet 9300 series surpport type: 12 - application/octet-stream
hp deskjet 9300 series surpport type: 13 - application/octet-stream
Printer dosn't surpport the flavor:application
Printer dosn't surpport the flavor:application/pdf
居然说打印机(我的打印机是:hp deskjet 9300 series)不支持PDF格式哦,有点让人不能接受,平常打印PDF文件没有任何问题啊。
请各位大侠帮忙看看什么原因啊。。。。
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
public class PrintTestByCommand {
/**
* @param args
*/
public static void main(String args[]) throws Exception {
String filename = args[0];
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
// PrintService printService[] =
// PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
DocFlavor printerFlavor[] = defaultService.getSupportedDocFlavors();
if(printerFlavor!=null){
for(int i=0; i < printerFlavor.length; i++)
System.out.println(defaultService.getName()+" surpport type: "+i+" - "+printerFlavor[i].getMimeType());
}
if (!defaultService.isDocFlavorSupported(flavor)) {
System.out.println("Printer dosn't surpport the flavor:"+flavor.getMediaType());
}
if (defaultService != null &&defaultService.isDocFlavorSupported(flavor)) {
DocPrintJob job = defaultService.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
System.out.println("print data: "+doc.getPrintData());
System.out.println("print data attributs: "+doc.getAttributes());
job.print(doc, pras);
Thread.sleep(10000);
}else
System.out.println("Printer dosn't surpport the flavor:"+flavor.getMimeType());
System.exit(0);
}
}
但是我执行结果是
D:\TaiFook\MSS Surport\iReport\src\APP-INF\classes>java PrintTestByCommand E:\\I
TSR.pdf
hp deskjet 9300 series surpport type: 0 - image/gif
hp deskjet 9300 series surpport type: 1 - image/gif
hp deskjet 9300 series surpport type: 2 - image/gif
hp deskjet 9300 series surpport type: 3 - image/jpeg
hp deskjet 9300 series surpport type: 4 - image/jpeg
hp deskjet 9300 series surpport type: 5 - image/jpeg
hp deskjet 9300 series surpport type: 6 - image/png
hp deskjet 9300 series surpport type: 7 - image/png
hp deskjet 9300 series surpport type: 8 - image/png
hp deskjet 9300 series surpport type: 9 - application/x-java-jvm-local-objectref
hp deskjet 9300 series surpport type: 10 - application/x-java-jvm-local-objectref
hp deskjet 9300 series surpport type: 11 - application/octet-stream
hp deskjet 9300 series surpport type: 12 - application/octet-stream
hp deskjet 9300 series surpport type: 13 - application/octet-stream
Printer dosn't surpport the flavor:application
Printer dosn't surpport the flavor:application/pdf
居然说打印机(我的打印机是:hp deskjet 9300 series)不支持PDF格式哦,有点让人不能接受,平常打印PDF文件没有任何问题啊。
请各位大侠帮忙看看什么原因啊。。。。
...全文
相关推荐
Java实现word、excel转pdf、html
支持各种复杂样式的office文档转pdf、html实现;实例提供了核心实现方法以及相关依赖包,有问题可以留言交流;
使用java将office word pdf excel ppt文件转换成html文件
使用java将office word pdf excel ppt文件转换成html文件
SpringMVC+jspereport,实现报表分页查看、文件(pdf、excel、word)导出,以及html展示和调用浏览器打印功能实现打印
SpringMVC+jspereport,实现报表分页查看、文件(pdf、excel、word)导出,以及html展示和调用浏览器打印功能实现打印,报文都是采用ireport5.6.0设计
easypoi的使用demo,包含Excel模板导出,html导出Excel,Excel导出pdf等
使用Easy POI的API实现POI对Excel的所有操作,并且它的封装类可以让你快速上手,无需编写大量的Excel格式设置等复杂代码,高效,完善,高并发
JasperReport与Java程序,导出不同格式的报表,包括pdf,excel,html,报表打印等功能
使用开源报表jasperReport,并结合irport前端报表设计器,使用Java程序实现报表的不同格式的导出,包括pdf,excel,html,打印预览等功能,程序包含所需Jar包,并有详细的注释


Java SE
