|
@ -3,6 +3,8 @@ package org.nl.wms.util; |
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
import cn.hutool.core.date.DateUtil; |
|
|
import cn.hutool.core.date.DateUtil; |
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
import com.deepoove.poi.XWPFTemplate; |
|
|
import com.deepoove.poi.XWPFTemplate; |
|
@ -12,12 +14,18 @@ import com.deepoove.poi.policy.HackLoopTableRenderPolicy; |
|
|
import com.spire.doc.Document; |
|
|
import com.spire.doc.Document; |
|
|
import com.spire.ms.Printing.PrinterSettings; |
|
|
import com.spire.ms.Printing.PrinterSettings; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.nl.common.exception.BadRequestException; |
|
|
|
|
|
import org.nl.config.SpringContextHolder; |
|
|
|
|
|
import org.nl.system.service.param.dao.Param; |
|
|
import org.nl.system.service.param.impl.SysParamServiceImpl; |
|
|
import org.nl.system.service.param.impl.SysParamServiceImpl; |
|
|
import org.nl.wms.sch.group.service.dao.SchBaseVehiclematerialgroup; |
|
|
import org.nl.wms.sch.group.service.dao.SchBaseVehiclematerialgroup; |
|
|
|
|
|
import org.nl.wms.sch.task_manage.GeneralDefinition; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
import java.io.*; |
|
|
import java.io.*; |
|
|
|
|
|
import java.net.HttpURLConnection; |
|
|
|
|
|
import java.net.URL; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.util.Arrays; |
|
|
import java.util.Arrays; |
|
@ -73,73 +81,41 @@ public class PrintUtil { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException { |
|
|
|
|
|
//加载Word文档
|
|
|
|
|
|
Document document = new Document(); |
|
|
|
|
|
// document.loadFromFile("D:\\znxj\\downloadPath\\中.docx");
|
|
|
|
|
|
document.loadFromFile("C:\\Users\\LENOVO\\Desktop\\1.docx"); |
|
|
|
|
|
//创建PrinterSettings对象
|
|
|
|
|
|
PrinterSettings printerSettings = new PrinterSettings(); |
|
|
|
|
|
//指定物理打印机名称
|
|
|
|
|
|
printerSettings.setPrinterName("NPI269576"); |
|
|
|
|
|
//设置打印份数
|
|
|
|
|
|
printerSettings.setCopies((short) 1); |
|
|
|
|
|
//设置打印范围
|
|
|
|
|
|
printerSettings.setFromPage(2); |
|
|
|
|
|
printerSettings.setToPage(4); |
|
|
|
|
|
// 设置打印的起始页和结束页为文档的第一页和最后一页
|
|
|
|
|
|
printerSettings.setFromPage(1); |
|
|
|
|
|
printerSettings.setToPage(document.getPageCount()); |
|
|
|
|
|
//应用打印设置
|
|
|
|
|
|
document.getPrintDocument().setPrinterSettings(printerSettings); |
|
|
|
|
|
//执行打印
|
|
|
|
|
|
document.getPrintDocument().print(); |
|
|
|
|
|
|
|
|
|
|
|
/*XWPFTemplate template = null; |
|
|
public static String imageToByteAndWriteToFile(String imageUrl, String outputFilePath) { |
|
|
ByteArrayOutputStream wordOut = new ByteArrayOutputStream(1024); |
|
|
if (StrUtil.isEmpty(imageUrl)) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
File file = new File(outputFilePath); |
|
|
|
|
|
if (!file.exists()){ |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
String fileName = imageUrl.replaceAll(".*/", ""); |
|
|
try { |
|
|
try { |
|
|
File file = new File("C:\\Users\\LENOVO\\Desktop\\2.docx"); |
|
|
URL url = new URL(imageUrl); |
|
|
FileInputStream fileInputStream = new FileInputStream(file); |
|
|
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); |
|
|
//设置占位符:
|
|
|
httpURLConnection.setRequestMethod("GET"); |
|
|
Configure config = Configure.builder() |
|
|
|
|
|
.buildGramer("${", "}") //设置模板中的占位符${}, 默认是{{}}
|
|
|
try (InputStream inputStream = httpURLConnection.getInputStream(); |
|
|
.setValidErrorHandler(new Configure.AbortHandler()) //若模板中占位符与填充属性不对应,则报错;
|
|
|
FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath + "/" + fileName)) { |
|
|
.bind("list", new HackLoopTableRenderPolicy()) //设置模板中表格的参数属性
|
|
|
|
|
|
.build(); |
|
|
byte[] buffer = new byte[1024]; |
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
int bytesRead; |
|
|
jsonObject.put("vehicleCode","G001"); |
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) { |
|
|
List<JSONObject> jsonObjectList = new ArrayList<>(); |
|
|
fileOutputStream.write(buffer, 0, bytesRead); |
|
|
JSONObject jsonObject1 = new JSONObject(); |
|
|
} |
|
|
jsonObject1.put("orderCode","23451y4967"); |
|
|
|
|
|
jsonObject1.put("number","328458732"); |
|
|
|
|
|
jsonObject1.put("qty","22"); |
|
|
|
|
|
jsonObject1.put("next","111-11"); |
|
|
|
|
|
jsonObject1.put("dueDate","2023-12-12 04:00:00"); |
|
|
|
|
|
jsonObject1.put("index","1"); |
|
|
|
|
|
JSONObject jsonObject2 = new JSONObject(); |
|
|
|
|
|
jsonObject2.put("orderCode","23451y496"); |
|
|
|
|
|
jsonObject2.put("number","32845873"); |
|
|
|
|
|
jsonObject2.put("qty","23"); |
|
|
|
|
|
jsonObject2.put("next","111-12"); |
|
|
|
|
|
jsonObject2.put("dueDate","2023-12-12 05:00:00"); |
|
|
|
|
|
jsonObject2.put("index","2"); |
|
|
|
|
|
jsonObjectList.add(jsonObject1); |
|
|
|
|
|
jsonObjectList.add(jsonObject2); |
|
|
|
|
|
jsonObject.put("list",jsonObjectList); |
|
|
|
|
|
//填充文本域:
|
|
|
|
|
|
template = XWPFTemplate.compile(fileInputStream, config).render(jsonObject); |
|
|
|
|
|
// template.write(wordOut);
|
|
|
|
|
|
template.writeToFile("C:\\Users\\LENOVO\\Desktop\\1.docx"); |
|
|
|
|
|
}catch (RenderException | IOException ex){ |
|
|
|
|
|
throw ex; |
|
|
|
|
|
} finally { |
|
|
} finally { |
|
|
try { |
|
|
httpURLConnection.disconnect(); |
|
|
if (template != null){ |
|
|
} |
|
|
template.close(); |
|
|
} catch (IOException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
} catch (IOException ex) { |
|
|
SysParamServiceImpl sysParamService = SpringContextHolder.getBean(SysParamServiceImpl.class); |
|
|
|
|
|
Param localHostPost = sysParamService.findByCode(GeneralDefinition.LOCALHOST_POST); |
|
|
|
|
|
if(ObjectUtil.isEmpty(localHostPost)){ |
|
|
|
|
|
throw new BadRequestException("图片ip端口号未填写"); |
|
|
} |
|
|
} |
|
|
}*/ |
|
|
return localHostPost.getValue() + "/" + fileName; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|