惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - mabiao008

CQRS 错误码code是int类型好还是String类型好 idea2025版本破解 pgsql切换当前会话的模式 java程序中增加mongodb的联合索引 文档启动脚本报错:-bash: ./app.sh: /bin/bash^M: bad interpreter: No such file or directory Milvus索引 pandoc使用 idea把unicode转为中文 idea 插件分享 java项目处理OFD文件 java项目无法读取resources目录下的文件 三种数据对象的区别 Linux环境aspose插件word转pdf中文乱码解决方案 java List报错Method threw ‘java.lang.UnsupportedOperationException‘ exception. 解决 java字段值为null,转json后不存在该字段对应的key 格式化json大文件 springboot项目启动报错Command line is too long. Shorten the command line via JAR manifest or via a classpath file and rerun. com.alibaba.excel.exception.ExcelGenerateException: Can not close IO linux的TCP端口问题 关于linux端口号 linux下生成pdf文件名遇到问题
把页面查询到的数据导出PDF文件(html中的在线表单下载为pdf文件)
mabiao008 · 2022-10-09 · via 博客园 - mabiao008

收到需求后,做了下调研。

在网上找了一下,发现大家都是在用itext。 iText是著名的开放项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

itextpdf.com/

具体使用开源参考:https://juejin.cn/post/6844903952933191687

但是我使用了后,发现问题不少。首先需要使用DC编辑模板。试用期30天,不好找到免费的版本。

 其次完成功能后,几个典型问题:

1、字体不展示全,比如三个字只显示1个字

点进去才能看到是3个字:“公开级”,而不是我们看到的“公”

 

2、上述可以看到字体不一样,字体大小也不一样

所以我就放弃了使用itext,使用了别的方案

客户给的是word模板,我考虑了word转pdf,和excel转pdf,比较后,觉得excel转pdf很简单。于是把word模板内容换成了excel。模板的值都是变量。

1、先依靠模板model.xlsx,生成一个带数据的id.xlsx

2、再把id.xlsx转换为pdf

3、删除id.xlsx

上述是思路,具体使用的是第三方组件:spire.xls.free,我使用的是免费版的。正版的需要付费,不付费会带水印,水印也有办法可以破解

具体实现如下:

先制作模板文件:客户模板1.xlsx:

0

pom依赖:

<repository>
    <id>com.e-iceblue</id>
    <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>spire.xls.free</artifactId>
    <version>2.2.0</version>
</dependency>

高级版本带有水印,免费版本无水印

map替换模板中的变量即可

Map map = new HashMap();
map.put("name", "测试方案");
map.put("no", "888888");
map.put("scheme", "生产方案001");
map.put("createdByName", "马飚");
map.put("auditTime", "2022-09-30");
replaceSheetsModel(map, "D:\\data\\客户模板1.xlsx", "D:\\data\\客户模板2.xlsx");
ExcelToPdf("D:\\data\\客户模板2.xlsx", "D:\\data\\客户模板44.pdf");

excel模板文件填充信息,生成excel详细文件:客户模板2.xlsx

public static void replaceSheetsModel(Map map,String intPath,String outPath) {
    try {
        FileInputStream fs = new FileInputStream(intPath);
        XSSFWorkbook workbook = new XSSFWorkbook(fs);
        XSSFWorkbook wb = (XSSFWorkbook) workbook;
        XSSFSheet sheet;
        for (int j = 0; j < wb.getNumberOfSheets(); j++) {
            sheet = workbook.getSheetAt(j);
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                XSSFRow row = (XSSFRow) rows.next();
                if (row != null) {
                    int num = row.getLastCellNum();
                    for (int i = 0; i < num; i++) {
                        XSSFCell cell = row.getCell(i);
                        if (cell != null) {
                            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                        }
                        if (cell == null || cell.getStringCellValue() == null) {
                            continue;
                        }
                        String value = cell.getStringCellValue();
                        if (!"".equals(value)) {
                            Set<String> keySet = map.keySet();
                            Iterator<String> it = keySet.iterator();
                            while (it.hasNext()) {
                                String text = it.next();
                                if (value.equalsIgnoreCase(text)) {
                                    cell.setCellValue((String) map.get(text));
                                    break;
                                }
                            }
                        } else {
                            cell.setCellValue("");
                        }
                    }
                }
            }
        }
        FileOutputStream fileOut = new FileOutputStream(outPath);
        wb.write(fileOut);
        fileOut.close();
    } catch (Exception e) {

        e.printStackTrace();
    }
}

excel转PDF方法:

public static void ExcelToPdf(String fileName, String outfileName) throws Exception {

    Workbook wb = new Workbook();

    wb.loadFromFile(fileName);

    wb.saveToFile(outfileName, FileFormat.PDF);
}

完整代码块:

先准备模板,用map替换模板中的变量

Map map = new HashMap();
map.put("name", "测试方案");
map.put("no", "888888");
map.put("scheme", "生产方案001");
map.put("createdByName", "马飚");
map.put("auditTime", "2022-09-30");
replaceSheetsModel(map, "D:\\data\\客户模板1.xlsx", "D:\\data\\客户模板2.xlsx");  // 替换模板文件中的变量,生成新的excel-客户模板2.xlsx
ExcelToPdf("D:\\data\\客户模板2.xlsx", "D:\\data\\客户模板44.pdf");               //客户模板2.xlsx转PDF文件
File file = new File(outPath);
file.delete();                                                                 // 删除客户模板2.xlsx

到此就完成了模板文件转化为pdf文件。