itext5.5.13 PDF预览权限问题

PdfUtils.htFile.createNewFile()

createNewFile 创建文件错误

bash 复制代码
ht = getResourceBasePath() + "\\templates\\ht.pdf";
htFile = new File(ht);

代码含义是创建源文件路径下创建ht.pdf模板,但是创建模板时就出错误信息,提示:系统找不到路径

错误原因

查看资料说是itext5版本 有对文件权限问题,itext7没有此问题,也可以直接用7解决

如果用5的话方式有两种

方式一

自己先创建好指定路径,输入固定位置即可

需要手动维护,不采取

方式二

查询此路径是否存在,如果存在不需要操作,如果不存在需要用File代码生成文件,可采取

实例代码-生成PDF表格数据

可避免出现 权限问题,找不到路径信息

bash 复制代码
			<dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itextpdf</artifactId>
                <version>5.5.13</version>
            </dependency>
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itext-asian</artifactId>
                <version>5.2.0</version>
            </dependency>
            <dependency>
                <groupId>com.itextpdf.tool</groupId>
                <artifactId>xmlworker</artifactId>
                <version>5.5.11</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.11.0</version>
            </dependency>
bash 复制代码
@GetMapping(value = "/exportFile")
    public void create(WoMaterialBalanceVo woMaterialBalanceVo,
                       HttpServletResponse response) throws IOException {
        File pdfFile = null;
        //导出方法
        iWoMaterialBalanceVoService.createContractInfo(woMaterialBalanceVo);
        response.setContentType("application/pdf");
        //file 文件指定在file.pdf中写入新数据
        FileInputStream in = new FileInputStream("C:/path/file.pdf");
        OutputStream out = response.getOutputStream();
        byte[] b = new byte[1024 * 5];
        int n;
        while ((n = in.read(b)) != -1) {
            out.write(b, 0, n);
        }
        out.flush();
        in.close();
        out.close();
}
bash 复制代码
public File createContractInfo(WoMaterialBalanceVo woMaterialBalanceVo) {
        try {
            // 1.新建document对象 建立一个Document对象
            Document document = new Document(PageSize.A4);

            //PdfUtils.htFile.createNewFile(); 此工具不用,自己创建模板路径

            // 指定C盘路径"C:/path/file.pdf"; 
            String path = "C:\\path\\file.pdf";
            // 创建File对象
            File file = new File(path);

            // 检查路径是否存在
            if (!file.getParentFile().exists()) {
                // 如果不存在,创建目录
                file.getParentFile().mkdirs();
            }
            try {
                // 检查文件是否存在
                if (!file.exists()) {
                    // 如果不存在,创建文件
                    file.createNewFile();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
            // 3.打开文档
            document.open();
            // 标题
            document.addTitle("测试");
            // 4.向文档中添加内容
            generatePDF(document,woMaterialBalanceVo);
            // 5.关闭文档
            document.close();
            writer.close();

            return PdfUtils.htFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return PdfUtils.htFile;
    }
bash 复制代码
// 生成PDF文件
    public void generatePDF(Document document,WoMaterialBalanceVo woMaterialBalanceVo) throws Exception {
        PdfPTable billTable = PdfUtils.createTitleTable(5,PdfUtils.tableCellFont,"1列","2列","3列","4列","5列");
            PdfUtils.addCell(billTable,x1,1,PdfUtils.tableCellFont);
            PdfUtils.addCell(billTable,x2,1,PdfUtils.tableCellFont);
            PdfUtils.addCell(billTable,x3,1,PdfUtils.tableCellFont);
            PdfUtils.addCell(billTable,x4,1,PdfUtils.tableCellFont);
            PdfUtils.addCell(billTable,x5,1,PdfUtils.tableCellFont);
        }

        document.add(billTable);
    }
相关推荐
姓蔡小朋友8 分钟前
Java 定时器
java·开发语言
crossaspeed8 分钟前
Java-SpringBoot的启动流程(八股)
java·spring boot·spring
这儿有个昵称28 分钟前
互联网大厂Java面试场景:从Spring框架到微服务架构的提问解析
java·spring boot·微服务·kafka·grafana·prometheus·数据库优化
2401_8823515232 分钟前
Flutter for OpenHarmony 商城App实战 - 地址编辑实现
android·java·flutter
爬山算法42 分钟前
Hibernate(47)Hibernate的会话范围(Scope)如何控制?
java·后端·hibernate
雨中飘荡的记忆44 分钟前
Caffeine入门到实战
java
砚边数影44 分钟前
AI开发依赖引入:DL4J / Java-ML 框架 Maven 坐标配置
java·数据库·人工智能·深度学习·机器学习·ai·maven
一路向北North1 小时前
nacos更改配置值后,应用提示Refresh keys changed 但是注入的值没有发生变化
java
黎雁·泠崖1 小时前
Java面向对象:this关键字+构造方法+标准JavaBean
java·开发语言·python
sheji34162 小时前
【开题答辩全过程】以 基于Java的智慧环卫垃圾收运管理系统设计与实现为例,包含答辩的问题和答案
java·开发语言