原创

[PDF]利用腾讯云票据识别接口自动修改PDF文件名

前言

公司发票报销的时候,需要根据发票的编码+金额进行重新名,几个文件还好,几十个文件,就需要耗费大量的时间来整理,太过于繁琐,所以我利用腾讯云免费提供的票据识别接口,自动进行重命名,瞬间速度就上去了,腾讯云每个月免费提供1000次的调用接口,对于我来说完全够用。

逻辑

程序的逻辑很简单,主要为以下几个步骤:

  1. 取指定目录下的所有PDF文件
  2. 根据取到的PDF文件,转换为base64位编码后,调用腾讯云的接口
  3. 根据腾讯云返回的结果,对文件进行重命名

主要代码

主要代码就是取文件内容转换为base64字符串,如下:

  /** * 将pdf文件转换为Base64编码 * @param file pdf文件 * @return */
    public static String PdfToBase64(File file) {
        Base64.Encoder encoder = Base64.getEncoder();
        FileInputStream fin =null;
        BufferedInputStream bin =null;
        ByteArrayOutputStream baos = null;
        BufferedOutputStream bout =null;
        try {
            fin = new FileInputStream(file);
            bin = new BufferedInputStream(fin);
            baos = new ByteArrayOutputStream();
            bout = new BufferedOutputStream(baos);
            byte[] buffer = new byte[1024];
            int len = bin.read(buffer);
            while(len != -1){
                bout.write(buffer, 0, len);
                len = bin.read(buffer);
            }
            //刷新此输出流并强制写出所有缓冲的输出字节
            bout.flush();
            byte[] bytes = baos.toByteArray();
            return encoder.encodeToString(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fin.close();
                bin.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

调用腾讯云接口代码如下:


  /** * ocr票据识别,只需要传入一个pdf的base64位编码就行了 * @param pdfbase64 * @return */
    public static VatInvoiceOCRResponse getResp(ConfigModel config,String pdfbase64){
        try {
            // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取

            Credential cred = new Credential(config.getSecretId(), config.getSecretKey());

            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");

            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            // 实例化要请求产品的client对象,clientProfile是可选的
            OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象

            VatInvoiceOCRRequest req = new VatInvoiceOCRRequest();
            req.setIsPdf(true);
            req.setPdfPageNumber(1L);
            req.setImageBase64(pdfbase64);
            Thread.sleep(1000);
            return  client.VatInvoiceOCR(req);

        } catch (TencentCloudSDKException | InterruptedException e) {
            System.out.println(e.toString());
        }
        return null;
    }

仓库

目前已经在github上开源,自取:
点击这里跳转

本文来自:[PDF]利用腾讯云票据识别接口自动修改PDF文件名-小码农,转载请保留本条链接,感谢!

温馨提示:
本文最后更新于 2023年08月31日,已超过 200 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
正文到此结束
本文目录