AIDL调用生成PDF以及在安卓上pdf预览

场景:银行及政府自助签署授权书一体机上面,硬件交互以及和业务无关的功能单独在另个apk中实现,在业务apk中只处理业务 如下图:

1. 服务端apk生成自定义PDF

1.添加依赖

implementation 'com.itextpdf:itext7-core:7.1.13'

2.客户端链接服务及调用生成pdf接口

package com.zchd.sign.pdf.client

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.Environment
import android.os.IBinder
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.PermissionUtils
import com.zchd.sign.pdf.service.SignPdfAidlInterface
import com.zchd.sign.pdf.service.SignPdfCallback
import com.zchd.sign.pdf.service.SignPdfInfo

class MainActivity : AppCompatActivity() {
    private var pdfAidlInterface: SignPdfAidlInterface? = null
    private val serviceConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(componentName: ComponentName, iBinder: IBinder) {
            pdfAidlInterface = SignPdfAidlInterface.Stub.asInterface(iBinder)
            Toast.makeText(this@MainActivity, "链接成功", Toast.LENGTH_LONG).show()

        }

        override fun onServiceDisconnected(componentName: ComponentName) {
            Toast.makeText(this@MainActivity, "链接失败", Toast.LENGTH_LONG).show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (!PermissionUtils.isGranted(*PermissionActivity.permissions.toTypedArray())) {
            startActivity(
                Intent(this, PermissionActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            )
        }
        findViewById<Button>(R.id.btn_connect).setOnClickListener {
            val intent = Intent()
            intent.component = ComponentName("com.zchd.sign.pdf.service", "com.zchd.sign.pdf.service.SignPdfService"
            )
            bindService(intent, serviceConnection, BIND_AUTO_CREATE)
        }
        findViewById<Button>(R.id.btn_preview).setOnClickListener {
            val signPdfInfo = SignPdfInfo(
                userName = "王天一",
                userCardType = "身份证",
                userIdCard = "524000000000000",
                houseRelation = "/",
                repName = "测试",
                repCardType = "身份证",
                repIdCard = "123456789",
                authRelation = "/",
                signPicPath = null,
                fingerPicPath = null,
                repSignPicPath = null,
                repFingerPicPath = null

            )
            val sdCardPath = Environment.getExternalStorageDirectory().absolutePath
            val pdfPath = "${sdCardPath}/sign_pdf.pdf"
            pdfAidlInterface?.sign(signPdfInfo, pdfPath, object : SignPdfCallback.Stub() {
                override fun onSuccess(path: String?) {
                    Toast.makeText(this@MainActivity, "服务端生成pdf成功", Toast.LENGTH_LONG).show()
                    val intent = Intent(baseContext, WebViewActivity::class.java)
                    intent.putExtra("path", path)
                    startActivity(intent)
                }

                override fun onFail() {
                    Toast.makeText(this@MainActivity, "服务端生成pdf失败", Toast.LENGTH_LONG).show()
                }
            })
        }
    }
}

3.安卓上实现PDF预览 PDF.js 中文文档

pdfjs编译完后放在安卓assets 通过webView加载支持在线和本地pdf地址

webView.loadUrl("file:///android_asset/index.html?${path}")

项目地址icon-default.png?t=N7T8https://gitee.com/firefix-m/aidl-sign-pdf

 

  • 15
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GodCodeApps

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值