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);
    }
相关推荐
sheji34163 小时前
【开题答辩全过程】以 基于Springboot的体检中心信息管理系统设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
天河归来3 小时前
本地windows环境升级dify到1.11.1版本
java·spring boot·docker
超级种码3 小时前
Java:JavaAgent技术(java.instrument和java.attach)
java·开发语言·python
甜鲸鱼4 小时前
【Spring AOP】操作日志的完整实现与原理剖析
java·spring boot·spring
狗头大军之江苏分军4 小时前
年底科技大考:2025 中国前端工程师的 AI 辅助工具实战盘点
java·前端·后端
一 乐4 小时前
酒店客房预订|基于springboot + vue酒店客房预订系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
计算机毕设指导64 小时前
基于Spring Boot的防诈骗管理系统【源码文末联系】
java·spring boot·后端·spring·tomcat·maven·intellij-idea
a程序小傲4 小时前
饿了吗Java面试被问:Redis的持久化策略对比(RDBVS AOF)
java·redis·面试
我家领养了个白胖胖4 小时前
MCP模型上下文协议 Model Context Protocol & 百度地图MCP开发
java·后端·ai编程
Coder_Boy_4 小时前
基于DDD+Spring Boot 3.2+LangChain4j构建企业级智能客服系统
java·人工智能·spring boot·后端