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);
    }
相关推荐
ceffans4 分钟前
PDF文档中文本解析
c++·windows·pdf
10km21 分钟前
java:Apache Commons Configuration2占位符解析异常的正确解法:${prefix:name:-default}
java·apache·configuration2·变量插值·interpolation
customer0821 分钟前
【开源免费】基于SpringBoot+Vue.JS个人博客系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
灰色人生qwer29 分钟前
SpringBoot 项目配置日志输出
java·spring boot·后端
2301_7930698239 分钟前
Spring Boot +SQL项目优化策略,GraphQL和SQL 区别,Spring JDBC 等原理辨析(万字长文+代码)
java·数据库·spring boot·sql·jdbc·orm
阿华的代码王国1 小时前
【从0做项目】Java搜索引擎(6)& 正则表达式鲨疯了&优化正文解析
java·后端·搜索引擎·正则表达式·java项目·从0到1做项目
服务端相声演员1 小时前
Oracle JDK、Open JDK zulu下载地址
java·开发语言
是姜姜啊!1 小时前
java连接redis
java·redis
hhw1991121 小时前
spring boot知识点5
java·数据库·spring boot
EQUINOX11 小时前
lab4 CSAPP:Cachelab
java·后端·spring