Java generates a PDF and downloads it to a local

Source: Internet
Author: User

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

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.