The
1,pdf generated. The creation of a PDF takes advantage of Itext.
package Pdfoutput; Import Java.awt.Color; Import Java.io.FileOutputStream; Import Java.util.Date; Import Java.io.File; Import Java.text.SimpleDateFormat; Import java.util.ArrayList; Import java.util.List; The following is the class import Com.lowagie.text.Cell in the Itext package; Import com.lowagie.text.Document; Import com.lowagie.text.Element; Import Com.lowagie.text.Font; Import com.lowagie.text.PageSize; Import Com.lowagie.text.Paragraph; Import Com.lowagie.text.Phrase; Import Com.lowagie.text.Rectangle; Import com.lowagie.text.Table; Import Com.lowagie.text.pdf.BaseFont; Import Com.lowagie.text.pdf.PdfWriter; public class Repeatingtable {//generated PDF name, with time name private static String Pdfname = Getnowtime () + ". pdf";//Current project path private St Ring Pdf_path = This.getclass (). Getprotectiondomain (). Getcodesource (). GetLocation (). GetPath (); Get method, get the PDF name public String Getpdfname () {return pdfname;} public repeatingtable () {//First generate a Document object document Docum ent = new Document (PAGESIZE.A4); try {//I'm going to put the PDF file in the project folder Web-inf//resources/pdf/below if (Pdf_path.indexof ("Web-inf") > 0) {pdf_path = pdf_path.substring (0, Pdf_path.indexof ("/ web-inf/")) +"/resources/pdf/"; } System.out.println (Pdf_path + "_pdf_path"); File File = new file (Pdf_path); Use Itext's PDFWriter object to generate PDF files, file paths and names such as: Web-inf//resources/pdf/2010-06-28 10:10:10.pdf pdfwriter.getinstance ( Document, new FileOutputStream (file + "/" + Pdfname)); Some simple PDF file information, which is the right-click property of the generated PDF file to see some information Document.addauthor ("ZhouFly@osunda.com"); Document.addsubject ("Resource List"); Open PDF file, prepare to write Operation Document.open (); Generate a 4-column table int row = 4; Table datatable = new Table (row); Define no column width int headerwidths[] = {10, 20, 20, 50}; Datatable.setwidths (headerwidths); Datatable.setwidth (100); Edge width datatable.setpadding (3); Modify the encoding, because Itext does not support Asian languages, you need to add an Asian language pack Itextasian.jar can search on Google, and then import basefont Bfchinese = Basefont.createfont (" Stsong-light "," unigb-ucs2-h ", basefont.not_embedded); Font of a setting font Fontchinese = new Font (Bfchinese, font.bold); Generate a small unit this unit accounts for 4 columns in fact, the table header is generated, the table header is Endheaders ()Method End Cell cell = new cell (new Phrase ("Device Report", Fontchinese)); Cell.sethorizontalalignment (Element.align_center); Cell.setleading (30); Cell.setcolspan (row); Cell.setborder (Rectangle.no_border); Cell.setbackgroundcolor (New Color (0xC0, 0xC0, 0xC0)); Datatable.addcell (cell); Datatable.setborderwidth (2); Datatable.setalignment (1); Fontchinese = new Font (Bfchinese, +, font.bold); Because Asian languages are not supported, each word is written with the Fontchinese encoding Datatable.addcell (New Phrase ("Resource id", Fontchinese)); Datatable.addcell (New Phrase ("Resource description", Fontchinese)); Datatable.addcell (New Phrase ("Resource Name", Fontchinese)); Datatable.addcell (New Phrase ("Resource Content", Fontchinese)); Datatable.endheaders (); Datatable.setborderwidth (2); Reset font Format Fontchinese = new Font (Bfchinese, font.normal); Data list AR = Getallresource (); for (int i = 0; i < ar.size ()-1; i = i + 4) {datatable.setalignment (element.align_left); Datatable.addcell (New Phras E (ar.get (0 + i). ToString (), Fontchinese); Datatable.addcell (New Phrase (Ar.get (1 + i). ToString (), FONTCHinese)); Datatable.addcell (New Phrase (Ar.get (2 + i). ToString (), Fontchinese)); Datatable.addcell (New Phrase (Ar.get (3 + i). ToString (), Fontchinese)); } Fontchinese = new Font (Bfchinese, +, font.normal); Document.add (New Paragraph ("Report generation Time:" + getnowtime (), Fontchinese)); Document.add (DataTable); } catch (Exception e) {e.printstacktrace ();} document.close (); }//Global get current time public static String Getnowtime () {SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss") ; String now = Formatter.format (New Date ()); return now; }//Generate pseudo data test public static List Getallresource () {List L = new ArrayList (); int i = n, while (i>0) {l.add (i); i--;} ret Urn L; }}
Export of 2,pdf. Export of PDF I used a JSF framework called ICEfaces. You can also use JavaScript to write an export method.
1) on the JSP page, the code is as follows:
<ice:outputresource id= "Pdfbutton" Filename= "#{pdfresources.pdfname}" mimetype= "Application/pdf" resource= "#{ Pdfresources.pdfresourcedynfilename} "shared=" false "label=" Download "type=" button "/>
2) Java backend code:
Package pdfoutput; Import Com.icesoft.faces.context.Resource; Import Java.io.ByteArrayInputStream; Import Java.io.ByteArrayOutputStream; Import java.io.IOException; Import Java.io.InputStream; Import java.io.Serializable; Import Java.util.Date; Import javax.faces.FacesException; Import Javax.faces.context.ExternalContext; Import Javax.faces.context.FacesContext; public class pdfresources{Private Resource pdfresourcedynfilename;//This directory is the directory where we store PDF files public static final String Resou Rce_path = "/resources/pdf/"; public static String pdfname = "Download"; Public String Getpdfname () {return pdfname;} public static void Setpdfname (String pdfname) {pdfresources.pdfname = PDFN Ame } public Pdfresources () {///Here is the method of generating the PDF data table repeatingtable RT = new Repeatingtable ();//Then get the PDF file name Pdfname = Rt.getpdfna Me (); System.out.println ("pdfname_" + pdfname); try {facescontext FC = Facescontext.getcurrentinstance (); Externalcontext EC = Fc.getexternalcontext (); Pdfresourcedynfilename = new MyResource (EC,Pdfname); } catch (Exception e) {e.printstacktrace ();}} public static byte[] Tobytearray (InputStream input) throws IOException {bytearrayoutputstream output = new BYTEARRAYOUTPU TStream (); byte[] buf = new byte[4096]; int len = 0; while (len = Input.read (BUF)) >-1) {output.write (buf, 0, Len);} return Output.tobytearray (); } public Resource Getpdfresourcedynfilename () {return pdfresourcedynfilename;} public void Setpdfresourcedynfilename ( Resource pdfresourcedynfilename) {this.pdfresourcedynfilename = Pdfresourcedynfilename;}} Class MyResource implements Resource, Serializable {private String resourcename; private InputStream inputstream; private Final Date LastModified; Private Externalcontext Extcontext; Public MyResource (Externalcontext EC, String resourcename) {this.extcontext = EC; this.resourcename = resourcename; LastModified = new Date (); Public InputStream Open () throws IOException {if (InputStream = = null) {//pdfresources.resource_path + resourcename isIs the PDF file we generated InputStream stream = Extcontext.getresourceasstream (Pdfresources.resource_path + resourcename); byte[] ByteArray = Pdfresources.tobytearray (stream); InputStream = new Bytearrayinputstream (ByteArray); } return InputStream; Public String Calculatedigest () {return resourcename,} public Date lastmodified () {return lastmodified; Withoptions (Options arg0) throws IOException {}}