总结CURD系列之将pdf显示为图片缩略图

面试造航母,工作CURD,又是一天。

今天遇到了一个小需求,页面上上传一个pdf,要求上传成功之后,显示出该pdf的图片缩略图,至于需求,不想说什么,来,看代码:

Controller的方法:

public ApiResponse<String> displayBack(@RequestParam("src") String path) {
        String src = path.trim();
        HttpURLConnection urlConnection = null;
        InputStream inputStream = null;
        PDDocument doc = null;
        ByteArrayOutputStream out = null;
        try {
            URL url = new URL(src);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            inputStream = urlConnection.getInputStream();
            doc = PDDocument.load(inputStream);
            PDFRenderer renderer = new PDFRenderer(doc);
            BufferedImage image = renderer.renderImage(0);
            int height = image.getHeight();
            int width = image.getWidth();
            BufferedImage newBImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            newBImage.getGraphics().drawImage(image, 0, 0, width, height, null);
            out = new ByteArrayOutputStream();
            ImageIO.write(newBImage, "jpg", out);
            byte[] smallContent = out.toByteArray();
            //进行base64加密
            String data = Base64Utils.encodeToString(smallContent);
            return ApiResponse.success("data:image/jpg;base64,".concat(data));
        } catch (IOException e) {
            LOGGER.error("打开失败,文件路径为:" + src, e);
            e.printStackTrace();
        } finally {
            try {
                doc.close();
                inputStream.close();
                urlConnection.disconnect();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

重点是找对jar包,此处用的是

org.apache.pdfbox.pdmodel.PDDocument
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.11</version>
</dependency>

和它的一些方法,比如convertToImage(),之后就没什么了,获取BufferedImage,然后画图,输出为字节数组,然后转换为base64串,传给页面的img标签显示,当然在src前要加: data:image/jpg;base64,这样才能正确显示出为jpg格式的图片。

今天领导说,显示pdf、图片这种事情,由前端来做,后台是提供服务的,如果提供文件流,接口调得多了就死了。。又长见识了。

所以,就有了pdf.js,火狐的,用来做pdf阅读器的,功能很强大~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Avue 中使用 `avue-dialog` 和 `avue-curd` 组件可以实现将 Dialog 和 CRUD 绑定起来。具体步骤如下: 1. 安装 `avue` 和 `avue-cli`: ```bash npm install avue avue-cli ``` 2. 在 `main.js` 中引入 `avue` 和 `avue-cli`: ```javascript import Vue from 'vue' import App from './App.vue' import Avue from 'avue' import AvueCli from 'avue-cli' Vue.use(Avue) Vue.use(AvueCli) new Vue({ el: '#app', render: h => h(App) }) ``` 3. 在 `App.vue` 中使用 `avue-dialog` 和 `avue-curd` 组件: ```vue <template> <div> <avue-dialog ref="dialog" :visible="dialogVisible" :title="dialogTitle" @confirm="handleConfirm"> <avue-curd ref="curd" :crud="crud" @search="handleSearch" @change="handleChange" @delete="handleDelete" /> </avue-dialog> <button @click="openDialog">打开 Dialog</button> </div> </template> <script> export default { data () { return { dialogVisible: false, dialogTitle: '编辑', crud: { api: '/api/user', columns: [ { label: 'ID', field: 'id' }, { label: '姓名', field: 'name', type: 'input', rules: [{ required: true, message: '请输入姓名' }] }, { label: '年龄', field: 'age', type: 'input', rules: [{ required: true, message: '请输入年龄' }] }, { label: '性别', field: 'gender', type: 'radio', options: [{ label: '男', value: '男' }, { label: '女', value: '女' }] } ] } } }, methods: { openDialog () { this.dialogVisible = true this.$refs.curd.search() }, handleSearch () { // 处理搜索逻辑 }, handleChange (data) { // 处理数据变化逻辑 }, handleDelete (data) { // 处理删除逻辑 }, handleConfirm () { const data = this.$refs.curd.getData() // 处理确认逻辑 } } } </script> ``` 在上述代码中,`avue-dialog` 组件用于显示 Dialog,`avue-curd` 组件用于显示 CRUD 表格。`crud` 对象包含了 CRUD 表格的配置信息,包括 API 地址、列信息等。`handleSearch`、`handleChange` 和 `handleDelete` 方法分别处理搜索、数据变化和删除事件。`handleConfirm` 方法处理确认事件,将数据保存到数据库中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值