mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-19 01:48:47 +08:00
pdf 预览
This commit is contained in:
parent
aebbfd4de4
commit
8ef265891b
@ -23,6 +23,20 @@
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itextpdf</artifactId>
|
||||
<version>5.5.13.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>itext-asian</artifactId>
|
||||
<version>5.2.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,49 +1,139 @@
|
||||
package com.ruoyi.system.fantang.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class NumberToList {
|
||||
|
||||
public static Map<String, String> convertTo(float number, int level) {
|
||||
public static Map<String, String> convertTo(float number, String level) {
|
||||
String[] str = {"fen","jiao","yuan", "shi", "bai", "qian", "wan", "shiwan", "baiwan", "qianwan"};
|
||||
Map<String, String> map = new HashMap<>();
|
||||
// 最大转换20W以内的数值
|
||||
if (number >= 100000000 || number <= 0 )
|
||||
if (number >= 100000000 || number <= 0 ){
|
||||
return null;
|
||||
}
|
||||
long ret = (long) (number*100);
|
||||
boolean flag = false;
|
||||
|
||||
for (String s : str) {
|
||||
switch (s) {
|
||||
case "fen":
|
||||
map.put(s + String.valueOf(level), String.valueOf(number%10));
|
||||
map.put(s + level, String.valueOf(ret%10));
|
||||
if (ret < 10) {
|
||||
flag = true;
|
||||
}
|
||||
break;
|
||||
case "jiao":
|
||||
map.put(s + String.valueOf(level), String.valueOf((number/10)%10));
|
||||
if (flag){
|
||||
if (ret < 100) {
|
||||
map.put(s + level, "¥");
|
||||
flag = true;
|
||||
} else {
|
||||
map.put(s + level, " ");
|
||||
}
|
||||
} else {
|
||||
map.put(s + level, String.valueOf((ret/10)%10));
|
||||
}
|
||||
break;
|
||||
case "yuan":
|
||||
map.put(s + String.valueOf(level), String.valueOf((number/100)%10));
|
||||
if (flag) {
|
||||
if (ret < 1000) {
|
||||
map.put(s + level, "¥");
|
||||
flag = true;
|
||||
} else {
|
||||
map.put(s + level, " ");
|
||||
}
|
||||
} else {
|
||||
map.put(s + level, String.valueOf((ret/100)%10));
|
||||
}
|
||||
break;
|
||||
case "shi":
|
||||
map.put(s + String.valueOf(level), String.valueOf((number/1000)%10));
|
||||
if (flag) {
|
||||
if (ret < 10000) {
|
||||
map.put(s + level, "¥");
|
||||
flag = true;
|
||||
} else {
|
||||
map.put(s + level, " ");
|
||||
flag = true;
|
||||
}
|
||||
} else {
|
||||
map.put(s + level, String.valueOf((ret/1000)%10));
|
||||
}
|
||||
break;
|
||||
case "bai":
|
||||
map.put(s + String.valueOf(level), String.valueOf((number/10000)%10));
|
||||
if (flag) {
|
||||
if (ret < 100000) {
|
||||
map.put(s + level, "¥");
|
||||
flag = true;
|
||||
} else {
|
||||
map.put(s + level, " ");
|
||||
}
|
||||
} else {
|
||||
map.put(s + level, String.valueOf((ret/10000)%10));
|
||||
}
|
||||
break;
|
||||
case "qian":
|
||||
map.put(s + String.valueOf(level), String.valueOf((number/100000)%10));
|
||||
if (flag) {
|
||||
if (ret < 1000000) {
|
||||
map.put(s + level, "¥");
|
||||
flag = true;
|
||||
} else {
|
||||
map.put(s + level, " ");
|
||||
}
|
||||
} else {
|
||||
map.put(s + level, String.valueOf((ret/100000)%10));
|
||||
}
|
||||
break;
|
||||
case "wan":
|
||||
map.put(s + String.valueOf(level), String.valueOf((number/1000000)%10));
|
||||
if (flag) {
|
||||
if (ret < 10000000) {
|
||||
map.put(s + level, "¥");
|
||||
flag = true;
|
||||
} else {
|
||||
map.put(s + level, " ");
|
||||
}
|
||||
} else {
|
||||
map.put(s +level, String.valueOf((ret/1000000)%10));
|
||||
}
|
||||
break;
|
||||
case "shiwan":
|
||||
map.put(s + String.valueOf(level), String.valueOf((number/10000000)%10));
|
||||
if (flag) {
|
||||
if (ret < 100000000) {
|
||||
map.put(s + level, "¥");
|
||||
flag = true;
|
||||
} else {
|
||||
map.put(s + level, " ");
|
||||
}
|
||||
} else {
|
||||
map.put(s + level, String.valueOf((ret/10000000)%10));
|
||||
}
|
||||
break;
|
||||
case "baiwan":
|
||||
map.put(s + String.valueOf(level), String.valueOf((number/100000000)%10));
|
||||
if (flag) {
|
||||
if (ret < 1000000000) {
|
||||
map.put(s + level, "¥");
|
||||
flag = true;
|
||||
} else {
|
||||
map.put(s + level, " ");
|
||||
}
|
||||
} else {
|
||||
map.put(s + level, String.valueOf((ret/100000000)%10));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// flag = false;
|
||||
// for (String s : str) {
|
||||
// if (flag == false) {
|
||||
// if (Objects.equals(map.get(s + level), " ")) {
|
||||
// flag = true;
|
||||
// map.put(s + level, "¥");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,29 +5,23 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.ruoyi.common.annotation.Log;
|
||||
import com.ruoyi.common.config.RuoYiConfig;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.enums.BusinessType;
|
||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.system.fantang.common.NumberToList;
|
||||
import com.ruoyi.system.fantang.domain.FtPrepaymentDao;
|
||||
import com.ruoyi.system.fantang.domain.FtPrepaymentVo;
|
||||
import com.ruoyi.system.fantang.service.IFtPrepaymentDaoService;
|
||||
import com.ruoyi.system.fantang.utils.PdfUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 收费管理Controller
|
||||
@ -186,32 +180,29 @@ public class FtPrepaymentDaoController extends BaseController {
|
||||
return toAjax(iFtPrepaymentDaoService.removeByIds(Arrays.asList(prepaymentIds)) ? 1 : 0);
|
||||
}
|
||||
|
||||
@GetMapping("/generateReceiptPdf")
|
||||
public void generateReceiptPdf(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
@PostMapping("/generateReceiptPdf")
|
||||
public AjaxResult generateReceiptPdf(@RequestBody JSONObject params) {
|
||||
Map<String, String> convert = NumberToList.convertTo(123.12F, "1");
|
||||
System.out.println(convert);
|
||||
|
||||
// 模板路径
|
||||
String templatePath = "F:\\pdfTemplate\\饭堂票据模板.pdf";
|
||||
|
||||
System.out.println(RuoYiConfig.getUploadPath());
|
||||
|
||||
// 生成的新文件路径
|
||||
String outputPath = RuoYiConfig.getUploadPath() + "\\饭堂票据.pdf";
|
||||
|
||||
// 数据
|
||||
Map<String, Object> values = new HashMap<>();
|
||||
|
||||
// 填入模板
|
||||
PdfUtils.fillTemplate(templatePath, outputPath, values);
|
||||
|
||||
// 下载地址
|
||||
String downloadPath = "profile/upload/饭堂票据.pdf";
|
||||
|
||||
return AjaxResult.success(downloadPath);
|
||||
|
||||
//根据文件信息中文件名字 和 文件存储路径获取文件输入流
|
||||
String realpath = "E:\\Dev\\jmfx_1\\fantang\\ruoyi-ui\\src\\assets\\images\\login-background.jpg";
|
||||
//PDF文件地址
|
||||
File file = new File(realpath);
|
||||
if (file.exists()) {
|
||||
byte[] data = null;
|
||||
FileInputStream input=null;
|
||||
try {
|
||||
input= new FileInputStream(file);
|
||||
data = new byte[input.available()];
|
||||
input.read(data);
|
||||
response.getOutputStream().write(data);
|
||||
} catch (Exception e) {
|
||||
System.out.println("pdf文件处理异常:" + e);
|
||||
}finally{
|
||||
try {
|
||||
if(input!=null){
|
||||
input.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,73 @@
|
||||
package com.ruoyi.system.fantang.utils;
|
||||
|
||||
import com.itextpdf.text.Document;
|
||||
import com.itextpdf.text.DocumentException;
|
||||
import com.itextpdf.text.pdf.*;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class PdfUtils {
|
||||
|
||||
public static void fillTemplate(String templatePath, String outputPath, Map<String, Object> values) {
|
||||
|
||||
PdfReader reader;
|
||||
FileOutputStream out;
|
||||
ByteArrayOutputStream bos;
|
||||
PdfStamper stamper;
|
||||
try {
|
||||
out = new FileOutputStream(outputPath);
|
||||
reader = new PdfReader(templatePath);
|
||||
bos = new ByteArrayOutputStream();
|
||||
stamper = new PdfStamper(reader, bos);
|
||||
AcroFields form = stamper.getAcroFields();
|
||||
|
||||
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
|
||||
form.addSubstitutionFont(baseFont);
|
||||
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("departName", "杜阮颐养院");
|
||||
map.put("payUnit", "苏镜泉");
|
||||
map.put("hospitalId", "R20081001");
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
|
||||
map.put("invoiceDate", sdf.format(date));
|
||||
map.put("projName1", "预付款 2020.10.06 至 2021.02.01");
|
||||
map.put("oneQian1", "1");
|
||||
map.put("oneBai1", "1");
|
||||
map.put("oneShi1", "1");
|
||||
map.put("oneWan", "1");
|
||||
map.put("oneQian2", "1");
|
||||
map.put("oneBai2", "1");
|
||||
map.put("oneShi2", "1");
|
||||
map.put("oneYuan", "1");
|
||||
map.put("oneJiao", "1");
|
||||
map.put("oneFen", "1");
|
||||
map.put("priceLite", "¥11111111.11");
|
||||
map.put("priceBig", "壹仟壹佰壹拾壹万壹仟壹佰壹拾壹元壹角壹分");
|
||||
map.put("remarks1", "测试收据样式");
|
||||
map.put("payee", "江门市第三人民医院");
|
||||
|
||||
|
||||
for (String name : form.getFields().keySet()) {
|
||||
form.setField(name, map.get(name));
|
||||
}
|
||||
//true代表生成的PDF文件不可编辑
|
||||
stamper.setFormFlattening(false);
|
||||
stamper.close();
|
||||
Document doc = new Document();
|
||||
PdfCopy copy = new PdfCopy(doc, out);
|
||||
doc.open();
|
||||
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
|
||||
copy.addPage(importPage);
|
||||
doc.close();
|
||||
} catch (IOException | DocumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -105,7 +105,7 @@ export function exportPrepayment(query) {
|
||||
export function generateReceiptPdf(data) {
|
||||
return request({
|
||||
url: '/fantang/prepayment/generateReceiptPdf',
|
||||
method: 'get',
|
||||
method: 'post',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
@ -252,11 +252,10 @@ export default {
|
||||
|
||||
},
|
||||
|
||||
// 生产收据 pdf
|
||||
// 生成收据 pdf
|
||||
handleGenerateReceiptPdf(row) {
|
||||
console.log(row);
|
||||
generateReceiptPdf(row).then(response => {
|
||||
// window.open("http://47.106.12.11:8080/prod-api/" + response.msg)
|
||||
window.open("http://192.168.3.93:9001/dev-api/" + response.msg)
|
||||
})
|
||||
console.log(process.env);
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user