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);
    }
相关推荐
qq_12498707538 小时前
基于SSM的动物保护系统的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·毕业设计·ssm·计算机毕业设计
Coder_Boy_8 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
Mr_sun.8 小时前
Day06——权限认证-项目集成
java
瑶山8 小时前
Spring Cloud微服务搭建四、集成RocketMQ消息队列
java·spring cloud·微服务·rocketmq·dashboard
abluckyboy8 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
2301_818732068 小时前
前端调用控制层接口,进不去,报错415,类型不匹配
java·spring boot·spring·tomcat·intellij-idea
2501_941982058 小时前
深度对比:Java、Go、Python 实现企微外部群推送,哪个效率更高?
java·golang·企业微信
新缸中之脑8 小时前
用MonkeyOCR解析复杂PDF
pdf
马猴烧酒.9 小时前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
sino爱学习9 小时前
高性能线程池实践:Dubbo EagerThreadPool 设计与应用
java·后端