1. Import maven Dependencies
<flyingSaucer.version>9.1.0</flyingSaucer.version>
<!--Flying-saucer--
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>${flyingSaucer.version}</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>${flyingSaucer.version}</version>
</dependency>
2. Use HTML to export a simple PDF file
@RequestMapping ("/pdfload") public void Pdfload (@RequestParam ("Pdfhtmldata") String Pdfhtmldata, @RequestParam (" FileName ") string filename, @RequestParam (" Borswerinfo ") string borswerinfo, HttpServletRequest request, HttpServletResponse response) {Log.info ("Pdfhtmldata:" +pdfhtmldata+ "; FileName:" +filename+ "; Borswerinfo:" + Borswerinfo); InputStream In=null; OutputStream Out=null; try {String Temppdffile=fileuploadrestcontroller.class.getresource ("/"). GetPath (); Temppdffile=temppdffile+ "/public/upload/" +system.currenttimemillis () + ". pdf"; File Dest=new file (temppdffile); Detects if there is a directory if (!dest.getparentfile (). exists ()) {Dest.getparentfile (). Mkdirs (); } fileoutputstream tempout=new FileOutputStream (temppdffile);//Build an HTML page (foreground control delivery via JS)//Build a document based on HTML page Documentbuilder builder = documentbuilderfactory.newinstance (). Newdocumentbuilder ();D ocument doc = builder.parse (new Bytearrayinputstream (Pdfhtmldata.getbytes ("UTF-8"));//RootAccording to document to fill Itextrendereritextrenderer itextrenderer=new itextrenderer (); Itextrenderer.setdocument (doc, null); I Textfontresolver fontresolver = Itextrenderer.getfontresolver (); String Pdffontpath=fileuploadrestcontroller.class.getresource ("/plugins/pdf-font/simsun.ttf"). GetPath ();// Request.getsession (). Getservletcontext (). Getrealpath ("Upload/pdf/font"); Fontresolver.addfont (PdfFontPath, basefont.identity_h,basefont.not_embedded); Itextrenderer.layout ();// Use Itextrenderer to generate PDF files itextrenderer.createpdf (tempout); Tempout.flush (); Tempout.close ();//File download files DownloadFile = New File (Temppdffile); FileName jsonobject borswerinfoobj=jsonobject.parseobject (borswerinfo), if (null!=borswerinfoobj&& "ie". equals (Borswerinfoobj.get ("browser"))) {Filename=java.net.urlencoder.encode (FileName, "UTF-8"); Filename=filename.replace ("+", "%20");// The file name with a space under IE will be converted to a plus sign Response.setheader ("CharSet", "Charset=utf-8");} else {filename=new String (filename.getbytes (), Charset.forname ("iso8859-1")), Response.setheader ("CharseT "," charset=iso8859-1 ");} Output Settings Response.setheader ("Content-type", "Application/octet-stream"); Response.setcontenttype ("Application/octet-stream"); Response.setheader ("Content-disposition", "attachment;filename=" +filename); Response.setcontentlengthlong (Downloadfile.length ()); In=new FileInputStream (DownloadFile); Out=response.getoutputstream (); Ioutils.copy (in,out); Response.flushbuffer (); }catch (Exception e) {e.printstacktrace ();} Finally {ioutils.closequietly (in); ioutils.closequietly (out); } }
3, the Pdfhtmldata parameter is
Export PDF file $ (' body '). Delegate (". Pdfload", "click", Function () {webindex.queryparam.limit=10000; Logger.info (Webindex.queryparam); $.ajax ({type: ' post ', url:webiface.rooturl+ '/filemanager/list/', DataType: ' JSON ') , Data:WebIndex.queryParam}). Done (function (data) {logger.info (data), if (data.code==1) {var ifile=$ (' body '). Find (' # Load-pdf '); var filename= "File Upload and download list. pdf"; Construct PDF to print HTML data var pdfhtmldata= '; pdfhtmldata+= '
4. Preview the PDF effect.
Use Flying-saucer to generate PDF files using HTML (Pei Donghui)