java从远程服务器获取PDF文件并后台打印(使用pdfFox)
一、java原生方式打印PDF文件
正反面都打印,还未研究出只打印单面的方法,待解决
public static void printFile(String path) throws Exception {
File file = new File(path);
File[] fies=file.listFiles();
for(File f:fies){
System.out.println("file "+f.getName());
String fileExt=f.getName().substring(f.getName().indexOf(".")+1,f.getName().length());
if("pdf".equalsIgnoreCase(fileExt)){
String filepath=path+File.separator+f.getName();
File pdfFile=new File(filepath);
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
//PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if(defaultService!=null){
DocPrintJob job = defaultService.createPrintJob(); //创建打印作业
FileInputStream fis = new FileInputStream(pdfFile); //构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式
job.print(doc, pras); //进行文件的打印
}
f.delete();
}
}
}
public static void main(String[] args) {
//System.out.println("Value:"+test());
//打印pdf的一个方法,首先安装下PDFCreator软件
try {
printFile("D:"+File.separator);
} catch (Exception e) {
System.out.println("打印文件异常:"+e.getMessage());
e.printStackTrace();
}
}
二、使用pdfBox方式此问题解决
1.maven依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.3</version>
</dependency>
2.代码(有打印预览)
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.PageRanges;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
import org.apache.pdfbox.printing.PDFPrintable; public final class PrintUtils
{
public static void main(String[] args) {
String fileURL = "http";
String fileName = ".pdf文件";
downloadFile(fileURL,fileName);
printWithDialog(getFilePath(fileName));
} public static void printWithDialog(String filePath) {
try {
PDDocument document = PDDocument.load(new File(filePath));
PrinterJob job = PrinterJob.getPrinterJob();
job.setPageable(new PDFPageable(document));
if (job.printDialog()){
job.print();
}
} catch (IOException e) {
e.printStackTrace();
} catch (PrinterException e) {
e.printStackTrace();
}
} public static String getFilePath(String fileName){
File path = new File(System.getProperty("user.dir").concat("/downFile"));
if (!path.exists() && !path.isDirectory()) {
path.mkdir();
}
String filePath = path + "/" + fileName;
return filePath;
} public static boolean downloadFile(String fileURL,String fileName) {
try {
URL url = new URL(fileURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(getFilePath(fileName)));
byte[] buffer = new byte[];
int count = ;
while ((count = in.read(buffer)) > ) {
out.write(buffer, , count);
}
out.close();
in.close();
return true;
} catch (Exception e) {
return false;
}
} }
java从远程服务器获取PDF文件并后台打印(使用pdfFox)的更多相关文章
- SNF开发平台WinForm之十三-单独从服务器上获取PDF文件进行显示-SNF快速开发平台3.3-Spring.Net.Framework
1运行效果: 2开发实现: 如果需要单独显示PDF文件时用下面代码去实现,指定url地址. 地址: . 获取附件管理的实体对象: List<KeyValuePair<string, obj ...
- 使用nodejs和Java访问远程服务器的服务
既然这篇文章用的是nodejs和Java访问远程服务器的服务,那么咱们先用另一门编程语言,SAP的ABAP(我日常工作使用得最多的编程语言)来开发一个服务吧. 这是我用ABAP编程语言实现服务的类:Z ...
- dotnet获取PDF文件的页数
#region 获取PDF文件的页数 private int BytesLastIndexOf(Byte[] buffer, int length, string Search) { if (buff ...
- xshell终端向远程服务器上传文件方法
centos-7下在本地终端里向远程服务器上传文件,在命令行中执行的软件. 安装命令如下: 在终端里输入如下命令: 会弹出如下窗口 选择你要上传的文件即可上传成功.
- 在java程序当中怎么获取一个文件的路径
在java程序当中怎么获取一个文件的路径? * 当这个文件在类路径下的时候(在src/bin目录下的时候): String absolutePath = Thread.currentThread(). ...
- 利用SSH在本机和远程服务器之间传输文件或文件夹
1.从远程服务器上下载文件到本机 scp <服务器用户名>@<服务器地址>:<服务器中要下载的文件路径> <下载到本机的绝对路径> 2.从本机上传本地文 ...
- java ssh远程服务器并执行多条shell命令
java ssh远程服务器并执行多条命令 import java.io.BufferedReader; import java.io.IOException; import java.io.Input ...
- 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)
1.在线预览PDF文件(禁止打印.下载.复制.另存为) (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将ww ...
- git服务器创建,冲突解决,远程仓库获取指定文件
1.git服务器创建 在公司多人协作开发的情况下,不能简单地使用github,因为github是互联网公开的,这种情况公司的代码的保密性就会丧失了.这种情况下,需要创建git服务器. 登录服务器,使用 ...
随机推荐
- C/S与B/S 区别以及优缺点
1.什么是C/S结构 C/S (Client/Server)结构,即客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现 ...
- MySQL高级第五章——主从复制
一.复制的基本原理 slave会从master读取binlog(二进制日志文件)进行数据同步 步骤: 详细操作步骤请参见:http://www.cnblogs.com/luckcs/articles/ ...
- 佛山Uber优步司机奖励政策(12月14日到12月20日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 全球订单最多的成都优步推出"南北通勤线"业务
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- git 取消commit
git如何撤销上一次commit操作 1.第一种情况:还没有push,只是在本地commit git reset --soft|--mixed|--hard <commit_id> git ...
- android 学习十四 探索安全性和权限
1.部署安全性:应用程序必须使用数字证书才能安装到设备上. 2.执行期间的安全性: 2.1 使用独立进程 2.2 使用固定唯一用户ID 2.3 申明性权限模型 3数字证书 ...
- C# 委托 / 跨线程访问UI / 线程间操作无效: 从不是创建控件“Form1”的线程访问它
C# 委托 / 跨线程访问UI / 线程间操作无效: 从不是创建控件“Form1”的线程访问它 网上的代码都比较复杂,还是这个简单 见代码, 简易解决办法: 主窗体代码 using System; ...
- PLSQL-包函数存储过程
包: 包是PLSQL中多个单元的逻辑组合,他将过程组合在一个包内容,以供用户调用,使用后,不需要程序员频繁的修改程序,可以保持程序的逻辑完整性,对包中的过程重新定义或者编译,以便修改部分功能,从而更好 ...
- Ubuntu目录与权限
Ubuntu目录 / /bin /sbin /boot /etc /mnt /home d :directory - :file b :block 磁盘以块进行 l :link Ubuntu权限 U ...
- hackhttp模板的介绍
hackhttp模板:造福人类 发起get/post/ 发起http原始数据包 漏洞利用:更为快捷放放不安 #hackhttp使用方法hh=hackhttp.hackhttp() code,head, ...