maven依赖

<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.4.3</version>
</dependency>

前端代码

<template><div class="pr" ref="father" ><div class="bg"><div class="theme-upload-body"><div class="theme-upload-drag"><Uploadmultiple="true"type="drag"action="":before-upload="handleBeforeUpload"><div style="padding: 45px 0"><Icon type="ios-folder-outline" size="52" style="color: #3399ff"></Icon><p>{{$t('page.verification.dragthefilehere')}}</p><p style="color: #acacac;font-size: 12px;text-align: center;">{{$t('page.verification.supportedpdf')}}</p></div></Upload></div><div class="theme-upload-data"><Table :columns="columns" :data="fileData" height="180" size="small"style="overflow-x: auto"></Table></div></div><Button type="primary" style="margin-left: 20px;margin-top:20px;"@click="pdfMerge()">合并文件</Button></div><loading :loading="loading"/></div>
</template><script>
import loading from '@/components/review/loading';
export default {name: 'pdfMerge',components: {loading},data () {return {pdfFile: [],//上传的pdf文件,pdfFileList:[],columns: [{title: $t('page.reviewApply.FileName'),key: 'fileName',align: 'center',width: '340',},{title: $t('page.reviewApprove.operate'),key: 'Operation',watch: '140',render: (h, params) => {return h('div', [h('Button', {props: {type: 'error',size: 'small'},style: {marginRight: '5px'},on: {click: () => {this.fileData.splice(params.index, 1);this.pdfFileList.splice(params.index, 1);}}}, $t('page.reviewApply.removeFile')),])}},],fileData: [],}},methods: {handleBeforeUpload (file) {let lastNum = file.name.lastIndexOf('.');let extensionName = file.name.substring(lastNum + 1, file.name.length).trim();if (extensionName === 'pdf') {this.pdfFileList.push(file)this.fileData.push({fileName: file.name,});}else {this.$Message.error({content: $t('page.common.pleaseuploadpdf'),duration: 10,closable: true})}},pdfMerge(){debuggerif(this.pdfFileList.length == 0){this.$Message.error("请上传pdf文件");return;}let formData = new FormData();//多个文件上传for (let i = 0; i < this.pdfFileList.length; i++) {formData.append("files", this.pdfFileList[i]); // 文件对象}this.abHttpUtil.postExcel('/pdm/drawingreview/mergePdf', formData).then((res) => {if(res.size > 0){let blob = new Blob([res],{ type: 'application/pdf' })let downloadElement = document.createElement('a');let href = window.URL.createObjectURL(blob); // 创建下载的链接downloadElement.href = href;downloadElement.download = "合并文件.pdf"; // 下载后文件名document.body.appendChild(downloadElement);downloadElement.click(); // 点击下载document.body.removeChild(downloadElement); // 下载完成移除元素window.URL.revokeObjectURL(href); // 释放掉blob对象this.$Message.success("下载成功!");}else{this.$Message.error($t('下载失败!'))}})}},created: function () {},
}
</script>
<style lang="less" scoped>
.bg {width: 100%;height: 550px;background-color: white;float: left;.theme-upload-body {display: flex;align-items: flex-start;margin-top: 10px;width: 100%;}.theme-upload-drag {margin-left:20px;text-align: center;width: 42%;}.theme-upload-data {width: 40%;margin-left: 20px;}table {width: 100% !important;table-layout: auto;}
}
.td-text {text-align: right;
}
.bunt {margin: 0 auto;width: 300px;
}
</style>

后端代码

    @RequestMapping("/mergePdf")@ApiOperation(value = "合并pdf", httpMethod = "POST")public ResultMsg mergePdf(@RequestParam("files") MultipartFile[] files, HttpServletResponse response, HttpServletRequest request) throws Exception {InputStream inputStream = null;String filePath = "C:\\BusinessFile\\合并文件.pdf";File file = new File(filePath);try {MergePDF.downloadMergePdfFiles(files,filePath);inputStream = new FileInputStream(file);response.setContentType("application/pdf");response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");response.addHeader("charset", "utf-8");response.addHeader("Pragma", "no-cache");response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("合并文件.pdf", "utf-8"));// 循环取出流中的数据byte[] b = new byte[1024];int len;try {while ((len = inputStream.read(b)) > 0) {response.getOutputStream().write(b, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {inputStream.close();file.delete();}}catch (Exception e){e.printStackTrace();}return getSuccessResult();}
package com.nssol_sh.bizbooster.tmec.pdm.utils;import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;import java.io.*;public class MergePDF {public static void main(String[] args) {String[] files = {"H:\\pdf\\part2.pdf", "H:\\pdf\\part3.pdf"};String savePath = "H:\\pdf\\final.pdf";mergePdfFiles(files,savePath);}public static boolean mergePdfFiles(String[] files, String newFile) {if(files.length == 1) {File file = new File(files[0]);File fileOut = new File(newFile);try {FileUtils.copyFile(file,fileOut);com.nssol_sh.bizbooster.tmec.pdm.utils.FileUtils.removeFile(files[0]);} catch (IOException e) {e.printStackTrace();}} else {boolean retValue = false;Document document = null;FileOutputStream fileOutputStream = null;PdfReader pdfReader = null;try {pdfReader = new PdfReader(files[0]);document = new Document(pdfReader.getPageSize(1));fileOutputStream = new FileOutputStream(newFile);PdfCopy copy = new PdfCopy(document,fileOutputStream);document.open();for (int i = 0; i < files.length; i++) {PdfReader reader = new PdfReader(files[i]);int n = reader.getNumberOfPages();for (int j = 1; j <= n; j++) {document.newPage();PdfImportedPage page = copy.getImportedPage(reader, j);copy.addPage(page);}reader.close();}retValue = true;} catch (Exception e) {System.out.println(e);try {pdfReader.close();document.close();fileOutputStream.close();} catch (IOException ioException) {ioException.printStackTrace();}} finally {System.out.println("执行结束");document.close();pdfReader.close();}for (String file : files) {File file1 = new File(file);if (file1.exists()){boolean delete = file1.delete();System.out.println(delete);}}}return true;}/*** 合并pdf文件* @param files 获取上传的文件* @param newFile 下载的文件路径*/public static void downloadMergePdfFiles(MultipartFile[] files,String newFile) {Document document = null;FileOutputStream fileOutputStream = null;PdfReader pdfReader = null;try {pdfReader = new PdfReader(files[0].getInputStream());document = new Document(pdfReader.getPageSize(1));fileOutputStream = new FileOutputStream(newFile);PdfCopy copy = new PdfCopy(document,fileOutputStream);document.open();for (MultipartFile file : files) {// 读取源文件PdfReader  reader = new PdfReader(file.getInputStream());int n = reader.getNumberOfPages();for (int i = 1; i <= n; i++) {document.newPage();PdfImportedPage page = copy.getImportedPage(reader, i);copy.addPage(page);}reader.close();}} catch (Exception e) {try {pdfReader.close();document.close();fileOutputStream.close();} catch (IOException ioException) {ioException.printStackTrace();}} finally {System.out.println("执行结束");document.close();pdfReader.close();}}}

效果图

vue ivew + spring boot合并pdf文件相关推荐

  1. 使用Vue和Spring Boot实现文件下载

    推荐:Vue学习汇总 使用Vue和Spring Boot实现文件下载 推荐 使用Vue和Spring Boot实现文件上传. 前端 这里介绍三种方式来实现文件下载,跨域问题在后端会进行处理,这个例子是 ...

  2. Spring Boot 提取pdf中的文字

    Spring Boot 提取pdf中的文字 提取pdf中的文字,由于字体不同,可能会提取出来乱码.(友情提示:建议先pdf文件转成图片,然后调用百度api提取文字,准确率高.跳转链接:https:// ...

  3. Spring Boot 实现万能文件在线预览-开源学习一

    Spring Boot 实现万能文件在线预览-开源学习一 1. 项目特性 支持word excel ppt,pdf等办公文档 支持txt,java,php,py,md,js,css等所有纯文本 支持z ...

  4. vue分页+spring boot +分页插件pagehelper

    vue分页+spring boot +分页插件pagehelper https://blog.csdn.net/baidu_38603246/article/details/98854013

  5. python合并pdf 加书签_使用Python批量合并PDF文件(带书签功能)

    1 #!/usr/bin/env python3 2 #-*- coding: utf-8 -*- 3 ''' 4 #文件名:pdfmerge.py5 本脚本用来合并pdf文件,输出的pdf文件按输入 ...

  6. mac怎么合并两个容器_PDF怎样合并?在Mac上合并PDF文件的最佳方法

    在保存编辑内容的同时合并PDF文件可能是一个大问题.合并大文件时,"预览"不涵盖展平,并且会使系统变慢.这些和其他问题可能会迫使您寻找更好的选择.让我们探索在Mac上组合PDF以涵 ...

  7. Spring Boot 上传文件(spring boot upload file)

    本篇文章将说明在Spring Boot web程序中如何上传文件. 开发环境: 1. eclipse Oxygen Release (4.7.0) 2. Spring Boot 1.4.3 RELEA ...

  8. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  9. Java合并pdf文件

    Java合并pdf文件 今天帮老师整理资料需要合并pdf文件,下了许多软件发现都需要VIP才行,所以写了个程序来帮助合并,直接在主程序中修改文件路径即可,如下图: 主要代码如下: package co ...

最新文章

  1. infer构建项目失败
  2. python模拟鼠标点击和键盘输入的操作_Python模拟鼠标点击及键盘输入(PyUserInput)...
  3. VTK:Filtering之TransformPolyData
  4. 张亚勤:PC之外的争夺战
  5. 十家全国学会就IEEE“审稿门”事件发表联合声明
  6. (转)Windows Form Application 读取并修改App.config文件
  7. 2014全新增强版迅捷PDF转换器介绍
  8. 如何打印被加密的PDF文件
  9. 过程FMEA(PFMEA)步骤一:策划与准备
  10. Eclipse插件开发
  11. win7 计算器 android,win7计算器
  12. 怎么做国外的问卷调查
  13. 1、AUTOSAR简介
  14. 软件测试人员的一般职业规划是如何的?
  15. 微信小程序期末大作业-天使童装商城
  16. OpenGl法向量计算
  17. Arcgis 10.2 中sde用oralc 做地理数据库。
  18. ppp项目跟踪审计为谁服务器,PPP项目跟踪审计研究——以J项目为例
  19. 27飞机大战_发射子弹
  20. java入门到成神之路的宝藏资源

热门文章

  1. Kafka穿过网闸(物理层)进行消费
  2. Hive 面试系列:时间交叉问题
  3. 河南省驻马店市谷歌高清卫星地图下载
  4. 我的世界服务器聊天显示坐标,我的世界端游怎么显示坐标
  5. matlab 微分方程组参数拟合,matlab拟合微分方程组中的参数
  6. java如何让程序暂停一会_Java如何暂停线程一段时间?
  7. 如何将磁盘从GPT格式转换成MBR
  8. 热部署工具jrebel-and-xrebel-for-intellij使用方法
  9. python弹窗_python弹窗运用
  10. 以数字孪生为基础,构建机房智慧化管理新环境