Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate pdf Files from Rich text by java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how java generates pdf files based on rich text. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Sample code:

Public class PdfUtil {/ * * generate pdf utility class * wmy 12:40 2019-8-9 * @ Param [guideBook, pdfPath] * @ return java.lang.Boolean * * / public static Boolean htmlToPdf (GuideBook guideBook, String pdfPath) {try {/ / 1. New document Document document = new Document (); / / 2. Establish a writer (Writer) associated with a document object, through which the document can be written to disk (Writer). / / to create a PdfWriter object, the first parameter is a reference to the document object, and the second parameter is the actual name of the file, in which the output path is also given. PdfWriter writer = PdfWriter.getInstance (document, new FileOutputStream (pdfPath)) / / 3. Open the document document.open (); / / convert the html / / html to be parsed into plain text as follows: 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 (); / / the font-family here does not support Chinese characters, {font-family: imitation of Song} is not allowed. InputStream cssIs = new ByteArrayInputStream ("* {font-family: PingFang-SC-Medium.otf;}" .getBytes ("UTF-8")); / / the fourth parameter is the input stream of the css file in html / / the fifth parameter is the font provider, which can be left unpassed when using fonts supported by the system by default. XMLWorkerHelper.getInstance (). ParseXHtml (writer, document, new ByteArrayInputStream (parsedHtml.getBytes ()), cssIs); / / 5. Close the document document.close ();} catch (Exception e) {e.printStackTrace (); return false;} return true;} / * * download file * 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 {/ / define output buffer 10k byte [] buffer = new byte [10240]; / / processing of file names / / 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 () Obtain html * wmy 10:39 2019-8-12 * @ Param [title] * @ return java.lang.String * * / public static String getHtml (String title) {return "" + title+ " }} this is the end of the article on "how java generates pdf files from rich text". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report