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