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);
    }
相关推荐
沉到海底去吧Go9 分钟前
【PDF识别改名】PDF指定区域OCR识别重命名工具使用教程和注意事项
python·pdf·ocr
空中湖10 分钟前
免费批量PDF转Word工具
pdf·word
mangge0811 分钟前
python读取SQLite表个并生成pdf文件
pdf
开开心心就好12 分钟前
免费PDF转图片软件
javascript·智能手机·pdf·flask·word·excel·scikit-learn
卑微的Coder4 小时前
Redis Set集合命令、内部编码及应用场景(详细)
java·数据库·redis
CrissChan4 小时前
Pycharm 函数注释
java·前端·pycharm
启航挨踢5 小时前
java学习电子书推荐
java
wgslucky5 小时前
Dubbo报错:module java.base does not “opens java.lang“ to unnamed module
java·开发语言·dubbo
DougLiang6 小时前
关于easyexcel动态下拉选问题处理
java·开发语言
mochensage6 小时前
C++信息学竞赛中常用函数的一般用法
java·c++·算法