1, first to write a PDF tool class, and related tools
2. PDF of the required jar package
Itext is a Java component that generates PDF reports
Freemarker is based on a template to generate text output
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
3. You need to use Adobe Acrobat Pro software to convert the template you want to create into PDF format
Open Adobe Acrobat Pro, open the template, select |--Prepare the form, it automatically detects and names the form field, and then saves it in PDF format
PDF Tool Class
public class Pdftemplet {private String Templatepdfpath; Private String Targetpdfpath; Private Serviceorder Order; Public Pdftemplet () {}public void Pdftemplet (File file,string basepath) thows exception{/* Template path */pdfreader reader = new Pdfreader (Templatepdfpath); Bytearrayoutputstream bos=new Bytearrayoutputstream ();/* Read */pdfstamper pdfstamper= new Pdfstamper (Reader,bos);/* Use Chinese font */basefont basefont=basefont. CreateFont (basepath+"Web-inf/static/simhei. TTF ", Basefont. Identity_h,basefont. EMBEDDED); Arraylist<basefont> fontlist=new arraylist<> (); fontlist. Add (Basefont); Acrofields S=pdfstamper. Getacrofields (); s. Setsubstitutionfonts (Fontlist);/* Note that the SetField name and the name of the form are consistent */s. SetField ("Enterprisename", order. Getenerprisename ()); S. SetField ("Incubatorname", order. Getincubatorname ()); S. SetField ("Recommend","");//Incubator recommended S. SetField ("Contacts", order. Getcontacts ()); S. SetField ("Phone", order. Getphone ()); S. SetField ("Email", order. Getemail ()); S. SetField ("Category","");//Service class S. SetField ("Demand", order. Getdemand ()); SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Createtime = Formatter. Format (Order. Getcreatetime ()); String updatetime = Formatter. Format (Order. Getupdatetime ()); s. SetField ("Createtime", Createtime); S.SetField ("UpdateTime", UpdateTime);p s. Setformflattenning (True);p s. Close (); FileOutputStream filesteam =new fileoutputstream (file); Fos.Write (Bos. Tobytearray); Fos. Close () ;}}
Calling methods
@RequestMapping (value ="Downloadpdf", method = Requestmethod. GET) public String downloadpdf (@PathVariable ("id") Integer id,httpservletrequest request) throws Exception {Serviceorder Serviceorder = Serviceorderservice. GetById (ID); Pdftemplet PDFTT = new Pdftemplet (); Pdftt. Setorder (Serviceorder); String BasePath = Request. GetSession (). Getservletcontext (). Getrealpath ("/"); String template = Request. GetSession (). Getservletcontext ().getrealpath ( "/") + WEB-INF /static/excel/confirmation.pdf ".settargetpdfpath ( "d:/Enterprise Service Confirmation sheet" pdf ") Pdftt "d:/Enterprise Service confirmation sheet. pdf") File.createnewfile () "/master/serviceorder/ OrderList "
Java generates PDF and downloads to local