Java generates PDF files through Freemarker and seals __java

Source: Internet
Author: User
Java code generates a PDF receipt file, directly on the code:
Package com.pdf;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Java.io.Writer;

Import Java.util.HashMap;

Import Org.xhtmlrenderer.pdf.ITextRenderer;
Import com.lowagie.text.DocumentException;

Import Com.lowagie.text.pdf.BaseFont;
Import freemarker.template.Configuration;

Import Freemarker.template.Template; public class Pdfhandler {private static final String CONTRACT = "resource/contract/";//Contract file storage path private static final S Tring TEMPLATE = "resource/templates/";//template store path private static final String Pdfname = "Pdfdemo";//pdf filename Private St atic final String htmlname = "Pdfdemo";//html filename public static void Contracthandler (String templatename, HashMap p
		Arammap) throws exception{//Get local template storage path, contract file storage path String TemplatePath = TEMPLATE;
		String Contractpath = CONTRACT; Full-path URL for assembling HTML and PDF contracts String Localhtmlurl = Contractpath+ HTMLName + ". html";
		String Localpdfpath = Contractpath + "/";
		To determine if a local path exists if it does not exist, create the file LocalFile = new file (Localpdfpath);
		if (!localfile.exists ()) {localfile.mkdirs ();
		String Localpdfurl = LocalFile + "/" + Pdfname + ". pdf";
		Templatename=templatename+ ". FTL"; Htmhandler (TemplatePath, templatename, Localhtmlurl, parammap);/Generate HTML Contract pdfhandler (Localhtmlurl, LOCALPDFURL);
	Generate PDF Contract DeleteFile (LOCALHTMLURL) based on HTML contract;//delete HTML format contract System.out.println ("PDF build Success"); /** * Generate HTML format contract/private static void Htmhandler (String templatepath, String templatename, String htmurl, H
		Ashmap Parammap) throws Exception {Configuration cfg = new Configuration ();
		Cfg.setdefaultencoding ("UTF-8");

		Cfg.setdirectoryfortemplateloading (New File (TemplatePath));

		Template Template = cfg.gettemplate (templatename);

		File Outhtmfile = new file (Htmurl);
		Writer out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (Outhtmfile))); Template.pRocess (Parammap, out);
	Out.close (); /** * generates a PDF format contract/private static void Pdfhandler (String htmurl, String pdfurl) throws Documentexception, IOE
		xception {File Htmfile = new file (Htmurl);

		File Pdffile = new file (Pdfurl);

		String URL = Htmfile.touri (). Tourl (). toString ();

		OutputStream OS = new FileOutputStream (pdffile);
		Org.xhtmlrenderer.pdf.ITextRenderer renderer = new Itextrenderer ();

		Renderer.setdocument (URL);
		Org.xhtmlrenderer.pdf.ITextFontResolver fontresolver = renderer. Getfontresolver ();

		Solve the Chinese support problem Fontresolver.addfont (template+ "SIMSUN.TTC", Basefont.identity_h, basefont.not_embedded);
		Renderer.layout ();
		Renderer.createpdf (OS);
	Os.close ();
		/** * Delete file */private static void DeleteFile (String fileUrl) {File File = new file (FILEURL);
	File.delete ();
		public static void Main (string[] args) throws Exception {String templatename = "201";
		HashMap Parammap = new hashmap<> (); Parammap.put ("Zjhkzh", "271003********279975 ");
		Parammap.put ("Kyye", "79244.95");
		Parammap.put ("Lxfs", "Configure LXFS properties in Web.xml, such as (Zhangxiaofan, 123,4567,8909)");
		Parammap.put ("Khwd", "2****1");
		Parammap.put ("Csksrq", "October 31, 2016 00:00");
		Parammap.put ("Ksrq", "2017-03-14");
		Parammap.put ("YE", "94444.95");
		Parammap.put ("Khzh", "271**********07279975");
		Parammap.put ("AH", "(2015) * * * * 0***0 number");
		Parammap.put ("Ckh", "(2017) Law yh****9 control character");
		Parammap.put ("Ydjah", "(2015) * * * number No. 00020");
		Parammap.put ("Kzcs", "01");
		Parammap.put ("XM", "Zhang San");
		Parammap.put ("FYMC", "The People's Court");
		Parammap.put ("Jsrq", "2017-06-14");
		Parammap.put ("Kzzt", "1");
		Parammap.put ("SE", "100");
		Parammap.put ("Lczh", "987234234");
		Parammap.put ("DATE", "March 24, 2017 09:39");
		Parammap.put ("ckwh", "(2015) * * * * * letter 0**20-1**0");
		Parammap.put ("Skse", "100");
		
		Parammap.put ("Csjsrq", "October 31, 2016 00:00");
	Contracthandler (templatename, Parammap);


 }
}
Demo Project screenshot:

Demo Project Link: Click to open the project connection


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.