欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot各種格式轉(zhuǎn)pdf的實(shí)例代碼

 更新時(shí)間:2021年01月06日 10:51:38   作者:兔老大的胡蘿卜  
這篇文章主要介紹了springboot各種格式轉(zhuǎn)pdf的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

添加依賴

<!--轉(zhuǎn)pdf-->
    <dependency>
      <groupId>com.documents4j</groupId>
      <artifactId>documents4j-local</artifactId>
      <version>1.0.3</version>
    </dependency>
    <dependency>
      <groupId>com.documents4j</groupId>
      <artifactId>documents4j-transformer-msoffice-word</artifactId>
      <version>1.0.3</version>
    </dependency>

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

測(cè)試方法

package com.ruoyi.mlogin.util;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.*;
import java.net.MalformedURLException;

/**
 * @author cai
 * @version 1.0
 * @date 2021/1/4 14:58
 */
public class Topdf {


  /**
   * 轉(zhuǎn)pdf doc docx xls xlsx
   * @param path
   */
  public void docTopdf(String path) {

    File inputWord = new File("C:\\Users\\29934\\Documents\\Tencent Files\\2993481541\\FileRecv\\1111.docx");
    File outputFile = new File("C:\\Users\\29934\\Documents\\Tencent Files\\2993481541\\FileRecv\\1111.pdf");
    try {
      InputStream docxInputStream = new FileInputStream(inputWord);
      OutputStream outputStream = new FileOutputStream(outputFile);
      IConverter converter = LocalConverter.builder().build();
      String fileTyle=path.substring(path.lastIndexOf("."),path.length());//獲取文件類型
      if(".docx".equals(fileTyle)){
        converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
      }else if(".doc".equals(fileTyle)){
        converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
      }else if(".xls".equals(fileTyle)){
        converter.convert(docxInputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
      }else if(".xlsx".equals(fileTyle)){
        converter.convert(docxInputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
      }
      outputStream.close();
      System.out.println("pdf轉(zhuǎn)換成功");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  /**
   *
   *      生成pdf文件
   *      需要轉(zhuǎn)換的圖片路徑的數(shù)組
   */
  public static void main(String[] args) {
    try {
      String imagesPath = "C:\\Users\\29934\\Documents\\Tencent Files\\2993481541\\FileRecv\\1111.jpg";
      File file = new File("C:\\Users\\29934\\Documents\\Tencent Files\\2993481541\\FileRecv\\1111.pdf");
      // 第一步:創(chuàng)建一個(gè)document對(duì)象。
      Document document = new Document();
      document.setMargins(0, 0, 0, 0);
      // 第二步:
      // 創(chuàng)建一個(gè)PdfWriter實(shí)例,
      PdfWriter.getInstance(document, new FileOutputStream(file));
      // 第三步:打開(kāi)文檔。
      document.open();
      // 第四步:在文檔中增加圖片。
      if (true) {
        Image img = Image.getInstance(imagesPath);
        img.setAlignment(Image.ALIGN_CENTER);
        // 根據(jù)圖片大小設(shè)置頁(yè)面,一定要先設(shè)置頁(yè)面,再newPage(),否則無(wú)效
        document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
        document.newPage();
        document.add(img);
        //下面是對(duì)應(yīng)一個(gè)文件夾的圖片
//      File files = new File(imagesPath);
//      String[] images = files.list();
//      int len = images.length;
//
//      for (int i = 0; i < len; i++)
//      {
//        if (images[i].toLowerCase().endsWith(".bmp")
//            || images[i].toLowerCase().endsWith(".jpg")
//            || images[i].toLowerCase().endsWith(".jpeg")
//            || images[i].toLowerCase().endsWith(".gif")
//            || images[i].toLowerCase().endsWith(".png")) {
//          String temp = imagesPath + "\\" + images[i];
//          Image img = Image.getInstance(temp);
//          img.setAlignment(Image.ALIGN_CENTER);
//          // 根據(jù)圖片大小設(shè)置頁(yè)面,一定要先設(shè)置頁(yè)面,再newPage(),否則無(wú)效
//          document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
//          document.newPage();
//          document.add(img);
//        }
//      }
        // 第五步:關(guān)閉文檔。
        document.close();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (BadElementException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }

}

補(bǔ)充:下面看下springboot:擴(kuò)展類型轉(zhuǎn)換器

需求:提交一個(gè)字符串到后端的java.sql.Time類型,就報(bào)錯(cuò)了:

Failed to convert property value of type [java.lang.String] to required type [java.sql.Time]

正常提交到j(luò)ava.util.Date類型是沒(méi)有問(wèn)題的。

所以這里就需要擴(kuò)展內(nèi)置的springmvc的轉(zhuǎn)換器

代碼如下:

WebConfig : 添加新的類型轉(zhuǎn)換器

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import com.csget.web.converter.StringToTimeConverter;

@Configuration
public class WebConfig {

 @Autowired
 private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

 @PostConstruct
 public void addConversionConfig() {
  ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter
    .getWebBindingInitializer();
  if (initializer.getConversionService() != null) {
   GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
   genericConversionService.addConverter(new StringToTimeConverter());
  }
 }
}

StringToTimeConverter :類型轉(zhuǎn)換器的具體實(shí)現(xiàn)

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;

public class StringToTimeConverter implements Converter<String, Time> {
 public Time convert(String value) {
  Time time = null;
  if (StringUtils.isNotBlank(value)) {
   String strFormat = "HH:mm";
   int intMatches = StringUtils.countMatches(value, ":");
   if (intMatches == 2) {
    strFormat = "HH:mm:ss";
   }
   SimpleDateFormat format = new SimpleDateFormat(strFormat);
   Date date = null;
   try {
    date = format.parse(value);
   } catch (Exception e) {
    e.printStackTrace();
   }
   time = new Time(date.getTime());
  }
  return time;
 }

}

到此這篇關(guān)于springboot各種格式轉(zhuǎn)pdf的文章就介紹到這了,更多相關(guān)springboot格式轉(zhuǎn)pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn)

    SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn)

    Spring項(xiàng)目中經(jīng)常需要配置日期時(shí)間格式格式,本文主要介紹了SpringBoot Jackson日期格式化統(tǒng)一配置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • java多線程之鐵路售票系統(tǒng)

    java多線程之鐵路售票系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java多線程之鐵路售票系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • springboot數(shù)據(jù)庫(kù)操作圖文教程

    springboot數(shù)據(jù)庫(kù)操作圖文教程

    本文以圖文并茂的形式給大家介紹了springboot數(shù)據(jù)庫(kù)操作,感興趣的朋友一起看看吧
    2017-07-07
  • java對(duì)list<Object>進(jìn)行手動(dòng)分頁(yè)實(shí)現(xiàn)

    java對(duì)list<Object>進(jìn)行手動(dòng)分頁(yè)實(shí)現(xiàn)

    本文主要介紹了java對(duì)list<Object>進(jìn)行手動(dòng)分頁(yè)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Java HashTable的原理與實(shí)現(xiàn)

    Java HashTable的原理與實(shí)現(xiàn)

    Java中的HashTable是一種線程安全的哈希表實(shí)現(xiàn),它可以高效地存儲(chǔ)和快速查找數(shù)據(jù),本文將介紹Java中的HashTable的實(shí)現(xiàn)原理、常用方法和測(cè)試用例,需要的小伙伴可以參考一下
    2023-09-09
  • iBatis習(xí)慣用的16條SQL語(yǔ)句

    iBatis習(xí)慣用的16條SQL語(yǔ)句

    iBatis 是apache 的一個(gè)開(kāi)源項(xiàng)目,一個(gè)O/R Mapping 解決方案,iBatis 最大的特點(diǎn)就是小巧,上手很快.這篇文章主要介紹了iBatis習(xí)慣用的16條SQL語(yǔ)句的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • Java性能優(yōu)化之?dāng)?shù)據(jù)結(jié)構(gòu)實(shí)例代碼

    Java性能優(yōu)化之?dāng)?shù)據(jù)結(jié)構(gòu)實(shí)例代碼

    這篇文章主要介紹了Java性能優(yōu)化之?dāng)?shù)據(jù)結(jié)構(gòu)實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • 深入探究Java?@MapperScan實(shí)現(xiàn)原理

    深入探究Java?@MapperScan實(shí)現(xiàn)原理

    之前是直接在Mapper類上面添加注解@Mapper,這種方式要求每一個(gè)mapper類都需要添加此注解,麻煩。通過(guò)使用@MapperScan可以指定要掃描的Mapper類的包的路徑,這篇文章深入探究Java?@MapperScan的實(shí)現(xiàn)原理
    2023-01-01
  • Mybatis實(shí)現(xiàn)分表插件

    Mybatis實(shí)現(xiàn)分表插件

    隨著系統(tǒng)的發(fā)展,數(shù)據(jù)量也會(huì)越來(lái)越大,分庫(kù)分表可以有效的緩解數(shù)據(jù)庫(kù)的壓力,本文主要介紹了Mybatis實(shí)現(xiàn)分表插件,感興趣的可以了解一下
    2021-05-05
  • 簡(jiǎn)單了解Java編程中對(duì)異常處理的運(yùn)用

    簡(jiǎn)單了解Java編程中對(duì)異常處理的運(yùn)用

    這篇文章主要簡(jiǎn)單介紹了Java編程中對(duì)異常處理的運(yùn)用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09

最新評(píng)論