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

java根據(jù)富文本生成pdf文件過程解析

 更新時間:2019年10月07日 10:45:10   作者:﹏岸與海的距離╮  
這篇文章主要介紹了java根據(jù)富文本生成pdf文件過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了java根據(jù)富文本生成pdf文件過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

public class PdfUtil {

  /*
   * 生成pdf工具類
   * wmy 12:40 2019/8/9
   * @Param [guideBook, pdfPath]
   * @return java.lang.Boolean
   **/
  public static Boolean htmlToPdf(GuideBook guideBook, String pdfPath) {
    try {
      // 1.新建document
      Document document = new Document();
      // 2.建立一個書寫器(Writer)與document對象關(guān)聯(lián),通過書寫器(Writer)可以將文檔寫入到磁盤中。
      //創(chuàng)建 PdfWriter 對象 第一個參數(shù)是對文檔對象的引用,第二個參數(shù)是文件的實際名稱,在該名稱中還會給出其輸出路徑。
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
      // 3.打開文檔
      document.open();
      //要解析的html
      //html轉(zhuǎn)換成普通文字,方法如下:
      org.jsoup.nodes.Document contentDoc = Jsoup.parseBodyFragment(getHtml(guideBook.getTitle())+guideBook.getContent());
      org.jsoup.nodes.Document.OutputSettings outputSettings = new org.jsoup.nodes.Document.OutputSettings();
      outputSettings.syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
      contentDoc.outputSettings(outputSettings);
      String parsedHtml = contentDoc.outerHtml();
      //這兒的font-family不支持漢字,{font-family:仿宋} 是不可以的。
      InputStream cssIs = new ByteArrayInputStream("* {font-family: PingFang-SC-Medium.otf;}".getBytes("UTF-8"));
      //第四個參數(shù)是html中的css文件的輸入流
      //第五個參數(shù)是字體提供者,使用系統(tǒng)默認支持的字體時,可以不傳。
      XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(parsedHtml.getBytes()), cssIs);
      // 5.關(guān)閉文檔
      document.close();
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  /*
   * 下載文件
   * wmy 9:54 2019/8/12
   * @Param [request, response, inputStream, fileName]
   * @return void
   **/
  public static void download(HttpServletRequest request, HttpServletResponse response, InputStream inputStream, String fileName){
    BufferedOutputStream bos = null;
    try {
      // 定義輸出緩沖 10k
      byte[] buffer = new byte[10240];
      //文件名稱的處理
      // http://127.0.0.1:5002/guide-book/pdf?id=124
      fileName = fileName.replaceAll("[\\pP\\p{Punct}]", "-").replace(" ", "-").replaceAll("[-]+", "-")+".pdf";
      String userAgent = request.getHeader("user-agent").toLowerCase();
      if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
        fileName = URLEncoder.encode(fileName, "UTF-8");
      } else {
        fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
      }
      response.setCharacterEncoding("utf-8");
      response.setContentType("application/msword");
      response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
      bos = new BufferedOutputStream(response.getOutputStream());
      int bytesRead = 0;
      while ((bytesRead = inputStream.read(buffer)) != -1) {
        bos.write(buffer, 0, bytesRead);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (bos != null) {
        try {
          bos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  /*
   * 獲取html
   * wmy 10:39 2019/8/12
   * @Param [title]
   * @return java.lang.String
   **/
  public static String getHtml(String title){
    return "<h1 align=\"center\">"+title+"</h1>";
  }

}

測試結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

  • Java中StringUtils與CollectionUtils和ObjectUtil概念講解

    Java中StringUtils與CollectionUtils和ObjectUtil概念講解

    這篇文章主要介紹了Java中StringUtils與CollectionUtils和ObjectUtil概念,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-12-12
  • 關(guān)于@SpringBootApplication與@SpringBootTest的區(qū)別及用法

    關(guān)于@SpringBootApplication與@SpringBootTest的區(qū)別及用法

    這篇文章主要介紹了關(guān)于@SpringBootApplication與@SpringBootTest的區(qū)別及用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 詳解Java分布式緩存系統(tǒng)中必須解決的四大問題

    詳解Java分布式緩存系統(tǒng)中必須解決的四大問題

    分布式緩存系統(tǒng)是三高架構(gòu)中不可或缺的部分,極大地提高了整個項目的并發(fā)量、響應(yīng)速度,但它也帶來了新的需要解決的問題,分別是: 緩存穿透、緩存擊穿、緩存雪崩和緩存一致性問題。本文將詳細講解一下這四大問題,需要的可以參考一下
    2022-04-04
  • springboot?集成easy-captcha實現(xiàn)圖像驗證碼顯示和登錄

    springboot?集成easy-captcha實現(xiàn)圖像驗證碼顯示和登錄

    本文主要介紹了springboot?集成easy-captcha實現(xiàn)圖像驗證碼顯示和登錄,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Springboot整合freemarker 404問題解決方案

    Springboot整合freemarker 404問題解決方案

    這篇文章主要介紹了Springboot整合freemarker 404問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • Java虛擬機底層原理詳細分析

    Java虛擬機底層原理詳細分析

    這篇文章主要介紹了Java虛擬機底層原理詳細分析,運行時數(shù)據(jù)區(qū)就是俗稱的虛擬機內(nèi)存,主要包括我們熟悉的堆、棧、本地方法棧、方法區(qū)(元空間)、程序計數(shù)器,虛擬機調(diào)優(yōu)主要針對的是運行時數(shù)據(jù)區(qū),也就是虛擬機內(nèi)存,需要的朋友可以參考下
    2024-01-01
  • idea hibernate jpa 生成實體類的實現(xiàn)

    idea hibernate jpa 生成實體類的實現(xiàn)

    這篇文章主要介紹了idea hibernate jpa 生成實體類的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 關(guān)于maven打包時的報錯: Return code is: 501 , ReasonPhrase:HTTPS Required

    關(guān)于maven打包時的報錯: Return code is: 501 , ReasonPhrase:HTTPS Requ

    這篇文章主要介紹了關(guān)于maven打包時的報錯: Return code is: 501 , ReasonPhrase:HTTPS Required,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 最新評論