parent
2d888dcb52
commit
cb22aaa87c
19 changed files with 708 additions and 106 deletions
@ -0,0 +1,284 @@ |
||||
package org.springblade.lims.controller; |
||||
|
||||
import cn.afterturn.easypoi.entity.ImageEntity; |
||||
import cn.afterturn.easypoi.word.WordExportUtil; |
||||
import com.alibaba.nacos.common.utils.CollectionUtils; |
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.documents4j.api.DocumentType; |
||||
import com.documents4j.api.IConverter; |
||||
import com.documents4j.job.LocalConverter; |
||||
import org.apache.pdfbox.pdmodel.PDDocument; |
||||
import org.apache.pdfbox.printing.PDFPrintable; |
||||
import org.apache.pdfbox.printing.Scaling; |
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
||||
import org.springblade.lims.entry.Entrust; |
||||
import org.springblade.lims.entry.Simple; |
||||
import org.springblade.lims.utils.PrintToPdfUtil; |
||||
import org.springblade.system.cache.DictBizCache; |
||||
import org.springblade.system.enums.DictBizEnum; |
||||
|
||||
import javax.imageio.ImageIO; |
||||
import javax.print.PrintService; |
||||
import javax.print.attribute.HashPrintRequestAttributeSet; |
||||
import javax.print.attribute.standard.Sides; |
||||
import javax.servlet.ServletResponse; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.awt.image.BufferedImage; |
||||
import java.awt.print.Book; |
||||
import java.awt.print.PageFormat; |
||||
import java.awt.print.Paper; |
||||
import java.awt.print.PrinterJob; |
||||
import java.io.*; |
||||
import java.text.DateFormat; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.*; |
||||
|
||||
import static org.springblade.lims.utils.GoogleBarCodeUtils.getBarCode; |
||||
import static org.springblade.lims.utils.GoogleBarCodeUtils.insertWords; |
||||
|
||||
public class WordToPDF { |
||||
|
||||
// public static void main(String[] args) throws Exception {
|
||||
// DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
||||
// String format = sdf.format(new Date());
|
||||
// String spath = "D:/ceshi/uploadPath/";
|
||||
// String path = spath + format + "/";
|
||||
// File file1 = new File(path);
|
||||
// //如果该路径没有,就创建该路径
|
||||
// if (!file1.exists()) {
|
||||
// file1.mkdirs();
|
||||
// }
|
||||
//
|
||||
// // 图片路径集合
|
||||
// List<String> list = new ArrayList<>();
|
||||
//
|
||||
// for (int i = 0; i < 24; i++) {
|
||||
// StringBuilder stringBuilder = new StringBuilder();
|
||||
// stringBuilder.append("编号:").append("aaa").append(i).append(" ")
|
||||
// .append("方式:").append("eee").append(i).append(" ");
|
||||
// System.out.println(stringBuilder.toString());
|
||||
//
|
||||
// //设置条形码表尾信息
|
||||
// BufferedImage image = insertWords(getBarCode("sdsj-" + i), stringBuilder.toString());
|
||||
//
|
||||
// String pathpicture = path + format + "-(" + i + ").png";
|
||||
// System.out.println(pathpicture);
|
||||
// list.add(pathpicture);
|
||||
// //生成指定的图片
|
||||
// ImageIO.write(image, "jpg", new File(pathpicture));
|
||||
// }
|
||||
// //将图片存放到pdf中
|
||||
//// PrintToPdfUtil.toPdf(path, spath + format + ".pdf");
|
||||
//
|
||||
// // 生成word文档
|
||||
// simpleHandlePrint(list);
|
||||
//
|
||||
// // 首先将word文件转化为pdf文件
|
||||
//// documents4jWordToPdf("C:\\Users\\AAA\\Desktop\\txm.docx", "C:\\Users\\AAA\\Desktop\\txm.pdf");
|
||||
//
|
||||
// // 然后打印pdf文件
|
||||
//// String pdfFile = "C:\\Users\\AAA\\Desktop\\txm.pdf";//文件路径
|
||||
//// File file = new File(pdfFile);
|
||||
//// PDFprint(file, "HP Color LaserJet MFP M281fdw");
|
||||
//
|
||||
// // 删除PDF文件
|
||||
//// if (file.exists() && file.isFile()) {
|
||||
//// file.delete();
|
||||
//// }
|
||||
// }
|
||||
|
||||
/** |
||||
* 将生成的条形码图片放到word文档中 |
||||
*/ |
||||
public static void simpleHandlePrint(List<String> list) { |
||||
Map<String, Object> result = new HashMap<>(); |
||||
List<Map<String, Object>> resultList = new ArrayList<>(); |
||||
|
||||
// 获取所有图片
|
||||
int count = 0; |
||||
if (list.size() % 3 == 0) { |
||||
count = list.size() / 3; |
||||
} else { |
||||
count = (list.size() / 3) + 1; |
||||
} |
||||
|
||||
for (int i = 0; i < count; i++) { |
||||
Map<String, Object> map = new HashMap<>(); |
||||
for (int j = 0; j < 3; j++) { |
||||
if ((i * 3) + j <= list.size() - 1) { |
||||
String s = list.get((i * 3) + j); |
||||
ImageEntity farView1 = new ImageEntity(); |
||||
farView1.setHeight(124);//设置高度
|
||||
farView1.setWidth(240);//设置宽度
|
||||
farView1.setType(ImageEntity.Data);//类型
|
||||
FileInputStream fis = null; |
||||
try { |
||||
fis = new FileInputStream(new File(s)); |
||||
byte[] bytes = readInputStream(fis); |
||||
farView1.setData(bytes); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
map.put("p" + j, farView1); |
||||
} |
||||
} |
||||
resultList.add(map); |
||||
} |
||||
|
||||
result.put("list", resultList); |
||||
//模板地址
|
||||
String handleUrl = "C://Users//AAA//Desktop//ceshi.docx"; |
||||
|
||||
// String url = DictBizCache.getKey(DictBizEnum.PRINT_URL.getName(), "simpleHandlePrint");
|
||||
XWPFDocument doc = null; |
||||
FileOutputStream fos = null; |
||||
try { |
||||
doc = WordExportUtil.exportWord07(handleUrl, result); |
||||
fos = new FileOutputStream("C://Users//AAA//Desktop//txm.docx"); |
||||
String filename = "测试.docx"; |
||||
// response.setContentType("application/octet-stream");
|
||||
// response.setHeader("content-disposition", "attachment;filename=12344.docx");
|
||||
|
||||
// doc.write(response.getOutputStream());
|
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} finally { |
||||
if (null != doc) { |
||||
// doc.close();
|
||||
try { |
||||
doc.write(fos); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
if (null != fos) { |
||||
try { |
||||
fos.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
if (null != doc) { |
||||
try { |
||||
doc.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 读输入流 |
||||
*/ |
||||
private static byte[] readInputStream(InputStream inStream) throws Exception { |
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
||||
byte[] buffer = new byte[1024]; |
||||
int len = 0; |
||||
while ((len = inStream.read(buffer)) != -1) { |
||||
outStream.write(buffer, 0, len); |
||||
} |
||||
inStream.close(); |
||||
return outStream.toByteArray(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 通过documents4j 实现word转pdf |
||||
* @param sourcePath 源文件地址 如 /root/example.doc |
||||
* @param targetPath 目标文件地址 如 /root/example.pdf |
||||
*/ |
||||
public static void documents4jWordToPdf(String sourcePath, String targetPath) { |
||||
File inputWord = new File(sourcePath); |
||||
File outputFile = new File(targetPath); |
||||
try { |
||||
InputStream docxInputStream = new FileInputStream(inputWord); |
||||
OutputStream outputStream = new FileOutputStream(outputFile); |
||||
IConverter converter = LocalConverter.builder().build(); |
||||
converter.convert(docxInputStream) |
||||
.as(DocumentType.DOCX) |
||||
.to(outputStream) |
||||
.as(DocumentType.PDF).execute(); |
||||
outputStream.close(); |
||||
} catch (Exception e) { |
||||
System.out.println("[documents4J] word转pdf失败:{}"); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 这里传入的文件为word转化生成的pdf文件 |
||||
*/ |
||||
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 = null; |
||||
//匹配指定打印机
|
||||
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(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
public static Paper getPaper() { |
||||
Paper paper = new Paper(); |
||||
// 默认为A4纸张,对应像素宽和高分别为 595, 842
|
||||
int width = 595; |
||||
int height = 842; |
||||
// 设置边距,单位是像素,10mm边距,对应 28px
|
||||
int marginLeft = 10; |
||||
int marginRight = 0; |
||||
int marginTop = 10; |
||||
int marginBottom = 0; |
||||
paper.setSize(width, height); |
||||
// 下面一行代码,解决了打印内容为空的问题
|
||||
paper.setImageableArea(marginLeft, marginRight, width - (marginLeft + marginRight), height - (marginTop + marginBottom)); |
||||
return paper; |
||||
} |
||||
} |
||||
@ -0,0 +1,122 @@ |
||||
package org.springblade.lims.utils; |
||||
|
||||
import com.alibaba.cloud.commons.lang.StringUtils; |
||||
import com.google.zxing.BarcodeFormat; |
||||
import com.google.zxing.EncodeHintType; |
||||
import com.google.zxing.WriterException; |
||||
import com.google.zxing.client.j2se.MatrixToImageWriter; |
||||
import com.google.zxing.common.BitMatrix; |
||||
import com.google.zxing.oned.Code128Writer; |
||||
|
||||
import java.awt.*; |
||||
import java.awt.image.BufferedImage; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @description: 谷歌条形码打印 |
||||
* @author: Administrator |
||||
* @date: 2019-03-07 14:46 |
||||
*/ |
||||
public class GoogleBarCodeUtils { |
||||
/** 条形码宽度 */ |
||||
private static final int WIDTH = 600; |
||||
|
||||
/** 条形码高度 */ |
||||
private static final int HEIGHT = 150; |
||||
|
||||
/** 加文字 条形码 */ |
||||
private static final int WORDHEIGHT = 300; |
||||
/** |
||||
* 设置 条形码参数 |
||||
*/ |
||||
private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() { |
||||
private static final long serialVersionUID = 1L; |
||||
{ |
||||
// 设置编码方式
|
||||
put(EncodeHintType.CHARACTER_SET, "utf-8"); |
||||
} |
||||
}; |
||||
/** |
||||
* 生成 图片缓冲 |
||||
* @author fxbin |
||||
* @param vaNumber VA 码 |
||||
* @return 返回BufferedImage |
||||
*/ |
||||
public static BufferedImage getBarCode(String vaNumber){ |
||||
try { |
||||
Code128Writer writer = new Code128Writer(); |
||||
// 编码内容, 编码类型, 宽度, 高度, 设置参数
|
||||
BitMatrix bitMatrix = writer.encode(vaNumber, BarcodeFormat.CODE_128, WIDTH, HEIGHT, hints); |
||||
return MatrixToImageWriter.toBufferedImage(bitMatrix); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
/** |
||||
* 把带logo的二维码下面加上文字 |
||||
* @author fxbin |
||||
* @param image 条形码图片 |
||||
* @param words 文字 |
||||
* @return 返回BufferedImage |
||||
*/ |
||||
public static BufferedImage insertWords(BufferedImage image, String words){ |
||||
// 新的图片,把带logo的二维码下面加上文字
|
||||
if (StringUtils.isNotEmpty(words)) { |
||||
|
||||
BufferedImage outImage = new BufferedImage(WIDTH, WORDHEIGHT, BufferedImage.TYPE_INT_RGB); |
||||
|
||||
Graphics2D g2d = outImage.createGraphics(); |
||||
|
||||
// 抗锯齿
|
||||
setGraphics2D(g2d); |
||||
// 设置白色
|
||||
setColorWhite(g2d); |
||||
|
||||
// 画条形码到新的面板
|
||||
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); |
||||
// 画文字到新的面板
|
||||
Color color=new Color(0, 0, 0); |
||||
g2d.setColor(color); |
||||
// 字体、字型、字号
|
||||
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 30)); |
||||
//文字长度
|
||||
int strWidth = g2d.getFontMetrics().stringWidth(words); |
||||
//总长度减去文字长度的一半 (居中显示)
|
||||
int wordStartX = (WIDTH - strWidth) / 2; |
||||
//height + (outImage.getHeight() - height) / 2 + 12
|
||||
int wordStartY = HEIGHT + 30; |
||||
|
||||
// 画文字
|
||||
g2d.drawString(words, wordStartX, wordStartY); |
||||
g2d.dispose(); |
||||
outImage.flush(); |
||||
return outImage; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 设置 Graphics2D 属性 (抗锯齿) |
||||
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制 |
||||
*/ |
||||
private static void setGraphics2D(Graphics2D g2d){ |
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
||||
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT); |
||||
Stroke s = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER); |
||||
g2d.setStroke(s); |
||||
} |
||||
|
||||
/** |
||||
* 设置背景为白色 |
||||
* @param g2d Graphics2D提供对几何形状、坐标转换、颜色管理和文本布局更为复杂的控制 |
||||
*/ |
||||
private static void setColorWhite(Graphics2D g2d){ |
||||
g2d.setColor(Color.WHITE); |
||||
//填充整个屏幕
|
||||
g2d.fillRect(0,0,600,600); |
||||
//设置笔刷
|
||||
g2d.setColor(Color.BLACK); |
||||
} |
||||
} |
||||
@ -0,0 +1,81 @@ |
||||
package org.springblade.lims.utils; |
||||
|
||||
import com.lowagie.text.*; |
||||
import com.lowagie.text.pdf.PdfWriter; |
||||
|
||||
import javax.imageio.ImageIO; |
||||
import java.awt.image.BufferedImage; |
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* 将多张图片合并转为PDF;需要用到iTextpdf包, |
||||
* |
||||
* @author 浴缸 |
||||
* |
||||
*/ |
||||
public class PrintToPdfUtil { |
||||
/** |
||||
* |
||||
* @param imageFolderPath |
||||
* 图片文件夹地址 |
||||
* @param pdfPath |
||||
* PDF文件保存地址 |
||||
* |
||||
*/ |
||||
public static void toPdf(String imageFolderPath, String pdfPath) { |
||||
try { |
||||
// 图片文件夹地址
|
||||
// String imageFolderPath = "D:/Demo/ceshi/";
|
||||
// 图片地址
|
||||
String imagePath = null; |
||||
// PDF文件保存地址
|
||||
// String pdfPath = "D:/Demo/ceshi/hebing.pdf";
|
||||
// 输入流
|
||||
FileOutputStream fos = new FileOutputStream(pdfPath); |
||||
// 创建文档
|
||||
Document doc = new Document(null, 0, 0, 0, 0); |
||||
//doc.open();
|
||||
// 写入PDF文档
|
||||
PdfWriter.getInstance(doc, fos); |
||||
// 读取图片流
|
||||
BufferedImage img = null; |
||||
// 实例化图片
|
||||
Image image = null; |
||||
// 获取图片文件夹对象
|
||||
File file = new File(imageFolderPath); |
||||
File[] files = file.listFiles(); |
||||
// 循环获取图片文件夹内的图片
|
||||
for (File file1 : files) { |
||||
if (file1.getName().endsWith(".png") |
||||
|| file1.getName().endsWith(".jpg") |
||||
|| file1.getName().endsWith(".gif") |
||||
|| file1.getName().endsWith(".jpeg") |
||||
|| file1.getName().endsWith(".tif")) { |
||||
// System.out.println(file1.getName());
|
||||
imagePath = imageFolderPath + file1.getName(); |
||||
System.out.println(file1.getName()); |
||||
// 读取图片流
|
||||
img = ImageIO.read(new File(imagePath)); |
||||
// 根据图片大小设置文档大小
|
||||
doc.setPageSize(new Rectangle(img.getWidth(), img |
||||
.getHeight())); |
||||
// 实例化图片
|
||||
image = Image.getInstance(imagePath); |
||||
// 添加图片到文档
|
||||
doc.open(); |
||||
doc.add(image); |
||||
} |
||||
} |
||||
// 关闭文档
|
||||
doc.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} catch (BadElementException e) { |
||||
e.printStackTrace(); |
||||
} catch (DocumentException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue