PrintService关于PDF的生成以及打印机打印
之前通过网上查询java实现打印机打印功能的方法大部分都是通过SimpleDoc类构件打印机的打印文件属性,通过PrintJob的Print()实现打印,但是打印出来的都是乱码。
首先考虑能不能打印文件,因为程序部署在服务器端,需要直接控制前端设备打印,所以需要无界面化操作,我使用的代码:
public static void PDFprint(File file, String printerName) throws Exception {PDDocument document = null;try {document = PDDocument.load(file);PrinterJob printJob = PrinterJob.getPrinterJob();printJob.setJobName(file.getName());if (printerName != null) {// 查找并设置打印机// 获得本台电脑连接的所有打印机
// PrintService[] printServices = PrinterJob.lookupPrintServices();
// if (printServices == null || printServices.length == 0) {
// System.out.print("打印失败,未找到可用打印机,请检查。");
// return;
// }PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
// // 匹配指定打印机
// for (int i = 0; i < printServices.length; i++) {
// System.out.println(printServices[i].getName());
// if (printServices[i].getName().contains(printerName)) {
// printService = printServices[i];
// break;
// }
// }if (printService != null) {printJob.setPrintService(printService);} else {System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");return;}}// 设置纸张及缩放PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.ACTUAL_SIZE);// 设置多页打印Book book = new Book();PageFormat pageFormat = new PageFormat();// 设置打印方向pageFormat.setOrientation(PageFormat.PORTRAIT);// 纵向pageFormat.setPaper(getPaper());// 设置纸张book.append(pdfPrintable, pageFormat, document.getNumberOfPages());printJob.setPageable(book);printJob.setCopies(1);// 设置打印份数// 添加打印属性HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();pars.add(Sides.DUPLEX); // 设置单双页printJob.print(pars);} finally {if (document != null) {try {document.close();} catch (IOException e) {e.printStackTrace();}}}}
生成pdf很简单,直接使用POI,这里展示一个PDFUTIL:
package com.bhne.utils;import java.awt.Color;
import java.io.FileOutputStream;import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;public class PDFUtil {/*** document对象*/private static Document document = null;/*** 创建一个书写器,布局文本位置* @param leftSize 居左* @param rightSize 居右* @param onSize 居上* @param underSize 居下* @param path 存储位置* @throws Exception 初始化PDF错误*/public PDFUtil(Integer leftSize , Integer rightSize , Integer onSize , Integer underSize, String path) throws Exception {try{// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。document = new Document(PageSize.A4, leftSize, rightSize, onSize, underSize);// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));// 打开文件document.open();}catch (Exception e){e.printStackTrace();System.out.println("PDF初始化错误");}}/*** 书写每一个段落选择的字体** @param fontType* 0 //楷体字* 1 //仿宋体* 2 //黑体* 字体需要可在追加* @return* @throws IOException* @throws DocumentException*/public BaseFont addFontType(Integer fontType) {BaseFont baseFont = null;try{switch (fontType){case 0://楷体字baseFont = BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);break;case 1://仿宋体baseFont = BaseFont.createFont("c://windows//fonts//SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);break;case 2://黑体baseFont = BaseFont.createFont("c://windows//fonts//SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);break;}return baseFont;}catch (Exception e){System.out.println("选择字体异常");e.printStackTrace();}return baseFont;}/*** 添加段落 - 段落位置( 0 居左 1 居中 2 居右)* @param fontType 选择字体* 0 //楷体字* 1 //仿宋体* 2 //黑体* @param fontSize 字体大小* @param color 字体颜色* @param alignment 0 居左 1 居中 2 居右* @param text 文本内容*/public void addParagraph(Integer fontType , Integer fontSize,Integer alignment ,String text){try{BaseFont chinese =addFontType(fontType);Font font = new Font(chinese, fontSize, com.lowagie.text.Font.COURIER);Paragraph paragraph =new Paragraph(text,font);//居中显示paragraph.setAlignment(alignment);document.add(paragraph);}catch (Exception e){e.printStackTrace();System.out.println("添加段落异常");}}/*** 添加段落 - 首行缩进* @param fontType 选择字体* 0 //楷体字* 1 //仿宋体* 2 //黑体* @param fontSize 字体大小* @param color 字体颜色* @param index 首行缩进* @param text 文本内容*/public void addTextIndent(Integer fontType , Integer fontSize,Color color ,Integer index ,String text){try{BaseFont chinese =addFontType(fontType);Font font = new Font(chinese, fontSize, Font.COURIER,color);Paragraph paragraph =new Paragraph(text,font);//设置首行缩进paragraph.setFirstLineIndent(index);document.add(paragraph);}catch (Exception e){e.printStackTrace();System.out.println("添加段落异常");}}/*** 添加新的一页*/public void addPage(){try{document.newPage();}catch (Exception e){e.printStackTrace();System.out.println("添加段落异常");}}/*** 换行* 传入1是一行,以此递增* @param lineNum 换的行数*/public void newLine(Integer lineNum) {try{for(int i =0 ; i<lineNum ; i++){document.add(new Paragraph("\n"));}}catch (Exception e){e.printStackTrace();System.out.println("换行错误");}}/*** 关闭文档*/public void close (){// 关闭文档document.close();}
}
问题一:服务器端部署打印服务,util方法中使用的字体是否存在;
问题二:服务器端需要安装打印机驱动,系统才能正确调用到打印机,但是windowsserver一般很少有打印机驱动的支持。
问题三:程序打印的是PDF,此方法通过POI生成的word是否可以打印?
PrintService关于PDF的生成以及打印机打印相关推荐
- js pdf文件 如何调用打印机打印_可以使用Javascript打开PDF文件的打印对话框吗?...
繁花不似锦 我通常使用一种类似于如何使用JavaScript打印PDF(eHow.com)的方法iframe.一个容纳打印触发器的功能......function printTrigger(eleme ...
- linux下打印pdf文件很慢,打印机打印pdf文件特别慢怎么解决
[转载]Solr4+IKAnalyzer的安装配置 转载:http://www.cnblogs.com/madyina/p/4131751.html 一.下载Solr4.10.2 我们以Windows ...
- 【项目管理】Java使用pdfbox调用打印机打印PDF文件
[项目管理]Java使用pdfbox调用打印机打印PDF文件 1.项目前言 2.项目实现 3.关键代码 4.效果演示 5.问题处理 5.1 安装对应字体 5.2 修改对应代码 系统:Win10 Jav ...
- itext通过pdf模板填充数据并调用打印机打印
使用itext5对生成的pdf模板进行编辑填充数据,一种是通过绝对定位去做,简单的可以,通用性太差,第二种是通过pdf域来填充字段,通用性很强 在使用过程中遇到的问题: 网上都是通过这种字体设置中文的 ...
- java 调用打印机 打印pdf方案
网上检索到的Java Printing Service方案 类似的方法的确会向打印机发送打印任务,但是打印机无反应不响应打印 //1.得到一个文件的输入流 FileInputStream fiStre ...
- java生成pdf_Java实现PDF文件生成并且打印pdf文件 demo
## Java实现PDF生成并且打印pdf文件(附demo) #### 目录: 0. 效果预览 1. 准备环境 2. Java如何调用打印机进行打印 3. Java如何生成pdf打印文件 4. 实现p ...
- python连接打印机打印文档、图片、pdf文件等
引言 python连接打印机进行打印,可能根据需求的不同,使用不同的函数模块. 如果你只是简单的想打印文档,比如office文档,你可以使用ShellExecute方法,对于微软office的文档.p ...
- Java调用打印机打印PDF文档的两种方法
最近,由于项目需求,需要增加系统调用打印机打印PDF文档功能.以前在Asp.Net项目中做过套打,但是现在的需求直接文档打印,下面是实现代码调用打印机的两种方法. 1.Java Print Servi ...
- python打印机打印图片_python连接打印机实现打印文档、图片、pdf文件等功能
引言 python连接打印机进行打印,可能根据需求的不同,使用不同的函数模块. 如果你只是简单的想打印文档,比如office文档,你可以使用ShellExecute方法,对于微软office的文档.p ...
最新文章
- java sessionid放入cookie_sessionID和cookie
- Github带来的不止是开源,还有折叠的认知
- 香港中文大学(深圳)吴保元教授课题组博士后招聘
- React 组件性能优化之 PureComponent 的使用
- curl上传图片的大坑
- 孙鑫VC学习笔记:第十二讲 (四) 用API函数、CFile类操作文件
- redis的持久化(RDBAOF的区别)
- Cadence SPB安装及重装
- oracle 怎么重启数据库,oracle数据库重启
- debian查询端口进程_Linux 查看端口占用情况
- RDL 报表 - 查询条件使用下拉框
- 结构光三维重建(二)线结构光三维重建
- 图解:什么是图?(以“图”话图)
- WORD 分栏后 页码混乱
- 基于Citespace和vosviewer文献计量学可视化SCI论文高效写作方法
- javaweb接口开发
- JavaWeb JavaBean,MVC三层架构
- 网络协议—三要素与五层网络协议
- DPDK入门(环境搭建以及小demo)
- CSP:重庆八中宏帆初级中学校初一编程社C2024HF700寒假集训总结——Day1