Implementation of converting dynamic jsp pages to PDF pages to pages

Source: Internet
Author: User
I recently encountered many pitfalls. Today, I will take some time to introduce the implementation of converting dynamic jsp pages to PDF pages. If you are interested, let's take a look at some problems encountered in my recent work. To sum up. The main function of this Code is to forward a generated JSP page to PDF and output it to the page.

Use ITEXT

String html = ServletUtils. forward (request, response, "/WEB-INF/jsp/depot/print/jhd. jsp "); // forward the request to jsp, and return the parsed content instead of output to the browser // System. out. println (html); byte [] pdf = PDFUtils.html 2pdf (html); response. setContentType ("application/pdf"); response. setHeader ("Content-Length", String. valueOf (pdf. length); response. setHeader ("Connection", "keep-alive"); response. setHeader ("Accept-Ranges", "none"); response. setHeader ("X-F Rame-Options "," DENY "); OutputStream out = response. getOutputStream (); out. write (pdf); out. flush (); public class ServletUtils {/*** after the forward method is executed, the content is not output to the browser, but to the byte stream, finally, return * @ param request * @ param response * @ param src * @ return */public static String forward (HttpServletRequest request, HttpServletResponse response, String src) as a String) {try {/* encode streams into bytes reconstructs response, modifies the output stream object in response, and outputs it to the byte array into bytes */final Byt EArrayOutputStream streams = new byteArrayOutputStream (); final ServletOutputStream servletOuputStream = new ServletOutputStream () {@ Overridepublic void write (int B) throws IOException {ByteArrayOutputStream. write (B) ;}@ Overridepublic boolean isReady () {return false ;}@ Overridepublic void setWriteListener (WriteListener writeListener) {}}; final PrintWriter printWriter = new PrintWriter (n Ew OutputStreamWriter (byteArrayOutputStream, "UTF-8"); response = new HttpServletResponseWrapper (response) {public ServletOutputStream getOutputStream () {return servletOuputStream;} public PrintWriter getWriter () {return printWriter ;}};/* rewrite response when response has failed to reconstruct response and modify the output stream object in response, make it output to the byte array bytes have been written into bytes * // execute the forward operation request. getRequestDispatcher (src ). forward (request, response); // convert the content in the byte stream too much into a String return new String (ByteArrayOutputStream. toByteArray (), "UTF-8");} catch (Exception e) {throw new RuntimeException (e) ;}} import com. itextpdf. text. *; import com.itext;.text=. using writer; import com. itextpdf. tool. xml. pipeline; import com. itextpdf. tool. xml. XMLWorker; import com. itextpdf. tool. xml. XMLWorkerFontProvider; import com. itextpdf. tool. xml. XMLWorkerHelper; import com. itextpdf. tool. xml. exceptions. cssResolverException; I Mport com.itext0000.tool.xml.html. cssAppliers; import com.itext;.tool.xml.html. cssAppliersImpl; import com.itext;.tool.xml.html. tags; import com. itextpdf. tool. xml. parser. XMLParser; import com.itext;.tool.xml.pipeline.css. CSSResolver; import com.itext;.tool.xml.pipeline.css. cssResolverPipeline; import com. itextpdf. tool. xml. pipeline. end. using writerpipeline; import com.itext;.tool.xml.pipeline.html. H TmlPipeline; import com.itext;.tool.xml.pipeline.html. htmlPipelineContext; import java. io. *;/*** pdf tool class */public class PDFUtils {/*** converts html to pdf, returns a pdf file * @ param html * @ return pdf byte array * @ throws IOException * @ throws define entexception * @ throws CssResolverException */public static byte [] html2pdf (String html) throws IOException, disable entexception, CssResolverException {Document document = new Doc Ument (PageSize. a4); ByteArrayOutputStream OS = new ByteArrayOutputStream (); bytes writer = bytes writer. getInstance (document, OS); document. open (); XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider () {@ Overridepublic Font getFont (String fontname, String encoding, float size, int style) {return super. getFont (fontname = null? "": Fontname, encoding, size, style) ;}}; fontProvider. addFontSubstitute ("lowagie", "garamond"); fontProvider. setUseUnicode (true); // use our font provider and set it to the unicode font style CssAppliers cssAppliers = new CssAppliersImpl (fontProvider); HtmlPipelineContext htmlContext = new HtmlPipelineContext (cssAppliers ); htmlContext. setTagFactory (Tags. getHtmlTagProcessorFactory (); CSSResolver cssResolver = XMLWorkerHelper. getInstance (). getDefaultCssResolver (true); Pipeline
 Pipeline = new CssResolverPipeline (cssResolver, new HtmlPipeline (htmlContext, new javaswriterpipeline (document, writer); XMLWorker worker = new XMLWorker (pipeline, true ); XMLParser p = new XMLParser (worker); p. parse (new InputStreamReader (new ByteArrayInputStream (html. getBytes ("gbk"); document. close (); return OS. toByteArray ();}}

The above section describes how to convert a dynamic jsp page to a PDF page. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in time!

For more information about how to convert dynamic jsp pages to PDF pages and how to output them to pages, see the PHP Chinese website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.