java-no.07 SPRINGMVC HTML content export PDF and download to local

Source: Internet
Author: User

1. Generate PDF file (including Chinese) by Itext Reference:http://my.oschina.net/sanji/blog/277704

1) Download the jar package:

CORE Pack:http://sourceforge.net/projects/itext/files/

XML Package:http://sourceforge.net/projects/xmlworker/files/

2) front Page

<input type= "button" style= "Position:absolute;top:55px;right:20px;background:url (resources/images/ 20150203052448221_easyicon_net_48.png) no-repeat;width:48px;height:48px;cursor:pointer;border:0px; "Onclick=" Download (' + ID + ') "/>

function download (ID) {window.open ("service.do?method=download&id=" + ID);}

3) Background Code

@RequestMapping (params =  "Method=download") Public void download (@RequestParam ("id")   Integer id, httpservletrequest req, httpservletresponse response)  { Serviceinfoentity serviceitem = configcategoryservice.getserviceitem (ID); string newscontent =  "<div id= ' Pano_camera '  class= ' servie_info1 ' >Package com.superscene.vhscene.base;import java.io.file;import java.io.fileinputstream;import  java.io.FileOutputStream;import java.io.IOException;import java.nio.charset.Charset;  import com.itextpdf.text.document;import com.itextpdf.text.documentexception;import  com.itextpdf.text.pdf.pdfwriter;import com.itextpdf.tool.xml.xmlworkerhelper;     public class parsehtmlasian {      /**      * Creates a PDF with the words  "Hello world"       *  @param  file     *  @throws  IOException      *  @throws  DocumentException     */     public static void createpdf (String path, string filename)  throws  IOException, DocumentException {        // step 1         document document = new document ();         / / step 2        pdfwriter writer =  Pdfwriter.getinstance (Document, new fileoutputstream (New file (path + file.separator  + fileName +  ". pdf"));        // step  3        document.open ();         // step 4        xmlworkerhelper.getinstance (). Parsexhtml (writer, document,                 new fileinputstream (path + file.separator + filename +   ". html"),  charset.forname ("UTF-8 "));        // step 5         document.close ();     }}


java-no.07 SPRINGMVC HTML content export PDF and download to local

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.