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服务器. 登录服务器,使用 ...
随机推荐
- 上海Uber优步司机奖励政策(12月20日到12月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- JavaScript---复选框全选的多种实现
<script language=javascript> //第一种方法 function selectall1() { var a = document.getElementsBy ...
- java 泛型历史遗留问题
Map<String,Integer> hashMap = new HashMap<String,Integer>(); hashMap.put(); // hashMap.p ...
- Python 更换国内pip源
pip国内的一些镜像: 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/sim ...
- Python搭配unittest
unittest是Python的单元测试框架, 类似于Java里面的TestNG. Unittest.py: import unittest from selenium import webdrive ...
- ajax 个人理解 学习笔记
W:Ajax Q:异步网络请求.无刷新请求数据. W:ajax的实现流程如下: Q: 创建XHR对象 调用open()方法,创建请求 调用send()方法,发送请求 捕获请求状态,判断请求结果 获取数 ...
- (一)Spring Boot修改内置Tomcat端口号--解决tomcat端口被占用的问题
Spring Boot 内置Tomcat默认端口号为8080,在开发多个应用调试时很不方便,本文介绍了修改 Spring Boot内置Tomcat端口号的方法. 一.EmbeddedServletCo ...
- 反片语 (Ananagrams,UVa 156)
题目描述: #include <iostream> #include <string> #include <cctype> #include <vector& ...
- pthon web框架flask(二)--快速入门
快速入门 迫切希望上手?本文提供了一个很好的 Flask 介绍.假设你已经安装 Flask, 如果还没有安装话,请浏览下 安装 . 一个最小的应用 一个最小的应用看起来像这样: from flask ...
- 今日头条 2018 AI Camp 6 月 2 日在线笔试编程题第一道——最大连续区间和扩展
题目 给出一个长度为 n 的数组a1.a2.....ana1.a2.....an,请找出在所有连续区间 中,区间和最大同时这个区间 0 的个数小于等于 3 个,输出这个区间和. 输入描述: 第一行一个 ...