最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)圖片轉(zhuǎn)換PDF文件的示例代碼

 更新時間:2020年09月02日 10:29:20   作者:看地闊天高云深處  
這篇文章主要介紹了Java實現(xiàn)圖片轉(zhuǎn)換PDF文件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

最近因為一些事情,需要將一張簡單的圖片轉(zhuǎn)換為PDF的文件格式,在網(wǎng)上找了一些工具,但是這些工具不是需要注冊賬號,就是需要下載軟件。

而對于只是轉(zhuǎn)換一張圖片的情況下,這些操作顯然是非常繁瑣的,所以作者就直接使用Java寫了一個圖片轉(zhuǎn)換PDF的系統(tǒng),現(xiàn)在將該系統(tǒng)分享在這里。

引入依賴

<!--該項目以SpringBoot為基礎(chǔ)搭建-->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.4.RELEASE</version>
  <relativePath/>
</parent>

<dependencies>
	<!--SpringMVC的依賴,方便我們可以獲取前端傳遞過來的文件信息-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--ITextPdf,操作PDF文件的工具類-->
  <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.4.2</version>
  </dependency>
</dependencies>

前端頁面

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>圖片轉(zhuǎn)換Pdf</title>
  <style>
    .submitButton {
      margin-top: 20px;
      margin-left: 150px;
      background-color: #e37e10;
      border-radius: 10px;
      border: 1px solid #ff8300;
    }
  </style>
</head>
<body>
  <div style="text-align: center">
    <h1>圖片轉(zhuǎn)換pdf工具</h1>
    <form action="/pdf/image/to" enctype="multipart/form-data" method="post" onsubmit="return allowFileType()">
      <input type="file" id="file" name="file" placeholder="請選擇圖片" onchange="allowFileType()" style="border: 1px solid black;"><br>
      <input type="submit" value="一鍵轉(zhuǎn)換pdf文件" class="submitButton">
    </form>
  </div>
</body>
<script>
  function allowFileType() {
    let file = document.getElementById("file").files[0];
    let fileName = file.name;
    console.log(fileName)
    let fileSize = file.size;
    console.log(fileSize)
    let suffix = fileName.substring(fileName.lastIndexOf("."),fileName.length);
    if('.jpg' != suffix && '.png' != suffix) {
      alert("目前只允許傳入.jpg或者.png格式的圖片!");
      return false;
    }
    if(fileSize > 2*1024*1024) {
      alert("上傳圖片不允許超過2MB!");
      return false;
    }
    return true;
  }
</script>
</html>

控制層接口

package com.hrp.controller;

import com.hrp.util.PdfUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @description: 用于處理Pdf相關(guān)的請求
 */
@Controller
@RequestMapping("pdf")
public class PdfController {

  @PostMapping("image/to")
  public void imageToPdf(@RequestParam("file") MultipartFile file,HttpServletResponse response) throws Exception{
    PdfUtils.imageToPdf(file,response);
  }

}

PDF工具類

package com.hrp.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;


/**
 * @description: pdf相關(guān)的工具類
 */
@Component
public class PdfUtils {

  /**
   * 圖片轉(zhuǎn)換PDF的公共接口
   *
   * @param file   SpringMVC獲取的圖片文件
   * @param response HttpServletResponse
   * @throws IOException    IO異常
   * @throws DocumentException PDF文檔異常
   */
  public static void imageToPdf(MultipartFile file, HttpServletResponse response) throws IOException, DocumentException {
    File pdfFile = generatePdfFile(file);
    downloadPdfFile(pdfFile, response);
  }

  /**
   * 將圖片轉(zhuǎn)換為PDF文件
   *
   * @param file SpringMVC獲取的圖片文件
   * @return PDF文件
   * @throws IOException    IO異常
   * @throws DocumentException PDF文檔異常
   */
  private static File generatePdfFile(MultipartFile file) throws IOException, DocumentException {
    String fileName = file.getOriginalFilename();
    String pdfFileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf";
    Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
    PdfWriter.getInstance(doc, new FileOutputStream(pdfFileName));
    doc.open();
    doc.newPage();
    Image image = Image.getInstance(file.getBytes());
    float height = image.getHeight();
    float width = image.getWidth();
    int percent = getPercent(height, width);
    image.setAlignment(Image.MIDDLE);
    image.scalePercent(percent);
    doc.add(image);
    doc.close();
    File pdfFile = new File(pdfFileName);
    return pdfFile;
  }

  /**
   *
   * 用于下載PDF文件
   *
   * @param pdfFile PDF文件
   * @param response HttpServletResponse
   * @throws IOException IO異常
   */
  private static void downloadPdfFile(File pdfFile, HttpServletResponse response) throws IOException {
    FileInputStream fis = new FileInputStream(pdfFile);
    byte[] bytes = new byte[fis.available()];
    fis.read(bytes);
    fis.close();

    response.reset();
    response.setHeader("Content-Type", "application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(pdfFile.getName(), "UTF-8"));
    OutputStream out = response.getOutputStream();
    out.write(bytes);
    out.flush();
    out.close();
  }


  /**
   * 等比壓縮,獲取壓縮百分比
   *
   * @param height 圖片的高度
   * @param weight 圖片的寬度
   * @return 壓縮百分比
   */
  private static int getPercent(float height, float weight) {
    float percent = 0.0F;
    if (height > weight) {
      percent = PageSize.A4.getHeight() / height * 100;
    } else {
      percent = PageSize.A4.getWidth() / weight * 100;
    }
    return Math.round(percent);
  }
}

頁面效果

這就是系統(tǒng)啟動之后的頁面效果,雖然頁面比較簡陋,但是功能卻沒有任何折扣,有興趣或者有需要的同學(xué)可以自己搭建一下,試一試圖片轉(zhuǎn)換PDF文件的效果。

注意:作者自己測試了一下,普通圖片基本是沒有問題的,但是遇到一些特殊的圖片可能會出現(xiàn)異常,畢竟只是一個比較簡單的圖片轉(zhuǎn)換PDF系統(tǒng),難以兼容所有圖片。

到此這篇關(guān)于Java實現(xiàn)圖片轉(zhuǎn)換PDF文件的示例代碼的文章就介紹到這了,更多相關(guān)Java 圖片轉(zhuǎn)換PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springmvc4+hibernate4分頁查詢功能實現(xiàn)

    springmvc4+hibernate4分頁查詢功能實現(xiàn)

    本篇文章主要介紹了springmvc4+hibernate4分頁查詢功能實現(xiàn),Springmvc+hibernate成為現(xiàn)在很多人用的框架整合,有興趣的可以了解一下。
    2017-01-01
  • java中redis增刪查以及清理緩存的案例

    java中redis增刪查以及清理緩存的案例

    這篇文章主要介紹了java中redis增刪查以及清理緩存的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 基于JavaSwing設(shè)計和實現(xiàn)的酒店管理系統(tǒng)

    基于JavaSwing設(shè)計和實現(xiàn)的酒店管理系統(tǒng)

    這篇文章主要介紹了基于JavaSwing+mysql的酒店管理系統(tǒng)設(shè)計和實現(xiàn),它可以實現(xiàn)酒店日常的管理功能包括開房、退房、房間信息、顧客信息管理等
    2021-08-08
  • 手把手帶你用java搞定青蛙跳臺階

    手把手帶你用java搞定青蛙跳臺階

    這篇文章主要給大家介紹了關(guān)于Java青蛙跳臺階問題的解決思路與代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • Spring Boot 深入分析AutoConfigurationImportFilter自動化條件配置源碼

    Spring Boot 深入分析AutoConfigurationImportFilter自動化條件

    這篇文章主要分析了Spring Boot AutoConfigurationImportFilter自動化條件配置源碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-07-07
  • 序列化版本號serialVersionUID的作用_動力節(jié)點Java學(xué)院整理

    序列化版本號serialVersionUID的作用_動力節(jié)點Java學(xué)院整理

    Java序列化是將一個對象編碼成一個字節(jié)流,反序列化將字節(jié)流編碼轉(zhuǎn)換成一個對象,這篇文章主要介紹了序列化版本號serialVersionUID的作用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 解決@RequestMapping和@FeignClient放在同一個接口上遇到的坑

    解決@RequestMapping和@FeignClient放在同一個接口上遇到的坑

    這篇文章主要介紹了解決@RequestMapping和@FeignClient放在同一個接口上遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Spring計時器StopWatch的具體使用

    Spring計時器StopWatch的具體使用

    本文主要介紹了Spring計時器StopWatch的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • springboot實現(xiàn)獲取當(dāng)前服務(wù)器IP及當(dāng)前項目使用的端口號Port

    springboot實現(xiàn)獲取當(dāng)前服務(wù)器IP及當(dāng)前項目使用的端口號Port

    這篇文章主要介紹了springboot實現(xiàn)獲取當(dāng)前服務(wù)器IP及當(dāng)前項目使用的端口號Port方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java拆裝箱深度剖析

    Java拆裝箱深度剖析

    這篇文章主要為大家深度剖析了Java拆箱裝箱的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評論

济源市| 神农架林区| 资兴市| 会东县| 堆龙德庆县| 竹溪县| 黄冈市| 高安市| 隆回县| 高台县| 吐鲁番市| 微博| 汾阳市| 门头沟区| 曲沃县| 沙田区| 柏乡县| 镇坪县| 永胜县| 颍上县| 锡林浩特市| 无为县| 治多县| 龙口市| 南投县| 勐海县| 曲阜市| 开化县| 鸡泽县| 崇左市| 额济纳旗| 三都| 旬阳县| 平陆县| 枣庄市| 大港区| 惠来县| 健康| 丹江口市| 奇台县| 大同市|