溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java如何根據富文本生成pdf文件

發布時間:2021-09-26 17:08:57 來源:億速云 閱讀:399 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關java如何根據富文本生成pdf文件,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

示例代碼:

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對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中。      //創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));      // 3.打開文檔      document.open();      //要解析的html      //html轉換成普通文字,方法如下:      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"));      //第四個參數是html中的css文件的輸入流      //第五個參數是字體提供者,使用系統默認支持的字體時,可以不傳。      XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(parsedHtml.getBytes()), cssIs);      // 5.關閉文檔      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 "<h2 align=\"center\">"+title+"</h2>";  }}

關于“java如何根據富文本生成pdf文件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女