在springboot项目中实现将上传的jpg图片类型转为pdf并保存到本地

前言:前端使用uniapp中的uni.canvasToTempFilePath方法将画板中的内容保存为jpg上传至后端处理

uni.canvasToTempFilePath({
                canvasId: 'firstCanvas',
				sourceType: ['album'],
				fileType: "jpg",
                success: function (res1) {
					let signature_base64 = res1.tempFilePath;
					
					let signature_file = that.base64toFile(signature_base64);
					// 将签名存储到服务器
					uni.uploadFile({
						url: "convertJpgToPdf",
						name: "file",
						file: signature_file,
						formData: {
							busiId: 'ceshi12',
							token: token
						},
						success: function (res1) {
							console.log(res1);
						}
					})
                }
            });

// 将base64转为file
		base64toFile: function(base64String) {
		    // 从base64字符串中解析文件类型
			var mimeType = base64String.match(/^data:(.*);base64,/)[1];
			// 生成随机文件名
			var randomName = Math.random().toString(36).substring(7);
			var filename = randomName + '.' + mimeType.split('/')[1];
			
			let arr = base64String.split(",");
			let bstr = atob(arr[1]);
			let n = bstr.length;
			let u8arr = new Uint8Array(n);
			while (n--) {
				u8arr[n] = bstr.charCodeAt(n);
			}
			return new File([u8arr], filename, { type: mimeType });
		},

java代码:
首先在pom.xml中安装依赖

<dependency>
	<groupId>org.apache.pdfbox</groupId>
	<artifactId>pdfbox</artifactId>
	<version>2.0.8</version>
</dependency>

使用方法:

/**
     * 将jpg转为pdf文件
     * @param jpgFile
     * @return
     * @throws IOException
     */
    public MultipartFile convertJpgToPdf(MultipartFile jpgFile) throws IOException {
        // 保存MultipartFile到临时文件
        File tempFile = new File(System.getProperty("java.io.tmpdir"), "temp.jpg");
        jpgFile.transferTo(tempFile);
        // 创建PDF文档
        PDDocument pdfDoc = new PDDocument();
        PDPage pdfPage = new PDPage();
        pdfDoc.addPage(pdfPage);

        // 从临时文件创建PDImageXObject
        PDImageXObject pdImage = PDImageXObject.createFromFile(tempFile.getAbsolutePath(), pdfDoc);

        // 获取PDF页面的内容流以添加图像
        PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, pdfPage);

        // 获取PDF页面的大小
        PDRectangle pageSize = pdfPage.getMediaBox();
        float pageWidth = pageSize.getWidth();
        float pageHeight = pageSize.getHeight();

        // 在PDF页面上绘制图像
        float originalImageWidth = pdImage.getWidth();
        float originalImageHeight = pdImage.getHeight();

        // 初始化缩放比例
        float scale = 1.0f;

        // 判断是否需要缩放图片
        if (originalImageWidth > pageWidth || originalImageHeight > pageHeight) {
            // 计算宽度和高度的缩放比例,取较小值
            float widthScale = pageWidth / originalImageWidth;
            float heightScale = pageHeight / originalImageHeight;
            scale = Math.min(widthScale, heightScale);
        }
        // 计算缩放后的图片宽度和高度
        float scaledImageWidth = originalImageWidth * scale;
        float scaledImageHeight = originalImageHeight * scale;

        // 计算图片在页面中居中绘制的位置
        float x = (pageWidth - scaledImageWidth) / 2;
        float y = (pageHeight - scaledImageHeight) / 2;

        // 绘制缩放后的图片到PDF页面
        contentStream.drawImage(pdImage, x, y, scaledImageWidth, scaledImageHeight);
        // 关闭内容流
        contentStream.close();

        // 将PDF文档写入到字节数组中
        ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
        pdfDoc.save(pdfOutputStream);

        // 关闭PDF文档
        pdfDoc.close();

        // 删除临时文件
        tempFile.delete();

        // 创建代表PDF的MultipartFile
        MultipartFile pdfFile = new MockMultipartFile("converted12.pdf", "converted12.pdf", "application/pdf", pdfOutputStream.toByteArray());
        saveFileToLocalDisk(pdfFile, "D:/");
        return pdfFile;
    }

/**
     * 将MultipartFile file文件保存到本地磁盘
     * @param file
     * @param directoryPath
     * @throws IOException
     */
    public void saveFileToLocalDisk(MultipartFile file, String directoryPath) throws IOException {
        // 获取文件名
        String fileName = file.getOriginalFilename();

        // 创建目标文件
        File targetFile = new File(directoryPath + File.separator + fileName);

        // 将文件内容写入目标文件
        try (FileOutputStream outputStream = new FileOutputStream(targetFile)) {
            outputStream.write(file.getBytes());
        } catch (IOException e) {
            // 处理异常
            e.printStackTrace();
            throw e;
        }
    }

MockMultipartFile类

package com.jiuzhu.server.common;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class MockMultipartFile implements MultipartFile {
    private final String name;

    private String originalFilename;

    private String contentType;

    private final byte[] content;

    /**
     * Create a new MultipartFileDto with the given content.
     *
     * @param name    the name of the file
     * @param content the content of the file
     */
    public MockMultipartFile(String name, byte[] content) {
        this(name, "", null, content);
    }

    /**
     * Create a new MultipartFileDto with the given content.
     *
     * @param name          the name of the file
     * @param contentStream the content of the file as stream
     * @throws IOException if reading from the stream failed
     */
    public MockMultipartFile(String name, InputStream contentStream) throws IOException {
        this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
    }

    /**
     * Create a new MultipartFileDto with the given content.
     *
     * @param name             the name of the file
     * @param originalFilename the original filename (as on the client's machine)
     * @param contentType      the content type (if known)
     * @param content          the content of the file
     */
    public MockMultipartFile(String name, String originalFilename, String contentType, byte[] content) {
        this.name = name;
        this.originalFilename = (originalFilename != null ? originalFilename : "");
        this.contentType = contentType;
        this.content = (content != null ? content : new byte[0]);
    }

    /**
     * Create a new MultipartFileDto with the given content.
     *
     * @param name             the name of the file
     * @param originalFilename the original filename (as on the client's machine)
     * @param contentType      the content type (if known)
     * @param contentStream    the content of the file as stream
     * @throws IOException if reading from the stream failed
     */
    public MockMultipartFile(String name, String originalFilename, String contentType, InputStream contentStream)
            throws IOException {

        this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getOriginalFilename() {
        return this.originalFilename;
    }

    @Override
    public String getContentType() {
        return this.contentType;
    }

    @Override
    public boolean isEmpty() {
        return (this.content.length == 0);
    }

    @Override
    public long getSize() {
        return this.content.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return this.content;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(this.content);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.content, dest);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现Springboot MybatisPlus实现PDF上传到服务器并保存路径到数据库: 1. 在Springboot项目添加依赖:spring-boot-starter-web、mybatis-plus-boot-starter、poi-ooxml、commons-io等。 2. 创建一个Controller类,编写一个上传PDF文件的接口,接收PDF文件并保存到服务器指定目录下。 3. 在接口使用POI库将PDF文件换为图片,然后将图片保存到服务器指定目录下。 4. 将图片的路径保存到数据库,可以使用MybatisPlus提供的Mapper接口进行操作。 以下是示例代码: @Controller public class PdfController { @Autowired private PdfMapper pdfMapper; @PostMapping("/uploadPdf") public String uploadPdf(@RequestParam("file") MultipartFile file) throws IOException { // 保存PDF文件到服务器指定目录下 String filePath = "D:/pdf/" + file.getOriginalFilename(); File dest = new File(filePath); FileUtils.copyInputStreamToFile(file.getInputStream(), dest); // 将PDF文件换为图片保存到服务器指定目录下 String imgPath = "D:/pdf/" + file.getOriginalFilename().replace(".pdf", ".jpg"); PdfToImageUtil.pdfToImage(filePath, imgPath); // 将图片路径保存到数据库 Pdf pdf = new Pdf(); pdf.setPdfPath(filePath); pdf.setImgPath(imgPath); pdfMapper.insert(pdf); return "success"; } } 其PdfMapper是MybatisPlus自动生成的Mapper接口,Pdf是对应的实体类。PdfToImageUtil是一个工具类,用于将PDF文件换为图片

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值