freemark与flying saucer生成PDF

freemark与flying saucer生成PDF

1.导入flying saucer与freemark的包。

2.利用freemark生成html文件。

3.利用flying saucer进行pdf文件的生成。

具体方案:

参考javaeye中的大牛文章:

http://www.iteye.com/topic/509417

 

参考方法:

public String generateHtml() throws IOException, Exception {
		String meetingId = getRequest().getParameter("meetingId");
		// TODO 生成HTML
		// 得到配置对象
		Configuration configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		configuration.setOutputEncoding("utf-8");
		// 设置生成模板加载方式(由servletcontext生成)
		configuration.setServletContextForTemplateLoading(
				ServletActionContext.getServletContext(), "WEB-INF/ftl");
		// 生成数据模型
		Map<Object, Object> root = new HashMap<Object, Object>();
		// 把信息带回
		getNeedMeetingInfo(root, meetingId);
		// 得到模板
		Template template = configuration.getTemplate("meeting.ftl");
		// 创建文件夹
		String uploadFoldPathStr = fileSystemService.getDocumentRoot()
				+ "download" + File.separator + "pdf" + File.separator
				+ this.HTML_FOLD_SUFIX + meetingId;
		File uploadFoldPath = new File(uploadFoldPathStr);
		if (!uploadFoldPath.exists()) {
			uploadFoldPath.mkdirs();
		}
		// 文件保存名
		Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileSaveName = sdf.format(dt) + ".html";
		File afile = new File(uploadFoldPathStr + File.separator + fileSaveName);
		Writer out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(afile), "utf-8"));

		try {
			// 输出模板
			// template.process(root, this.getResponse().getWriter());
			template.process(root, out);
		} catch (TemplateException e) {
			e.printStackTrace();
		}

		// TODO生成PDF
		String inputFile = uploadFoldPathStr + File.separator + fileSaveName;
		String url = new File(inputFile).toURI().toURL().toString();

		// 生成PDF路径
		String generatorFilePath = fileSystemService.getDocumentRoot()
				+ "download" + File.separator + "pdf" + File.separator
				+ this.PDF_FOLD_SUFIX + meetingId;
		File generatorFoldPath = new File(generatorFilePath);
		if (!generatorFoldPath.exists()) {
			generatorFoldPath.mkdirs();
		}
		
		fileSaveName= sdf.format(dt)+ ".pdf";
		String outputFile = generatorFilePath + File.separator +fileSaveName;
		
		OutputStream os = new FileOutputStream(outputFile);
		ITextRenderer renderer = new ITextRenderer();
		renderer.setDocument(url);
		// 解决中文支持问题
		ITextFontResolver fontResolver = renderer.getFontResolver();
		fontResolver.addFont("C:/Windows/Fonts/simsun.ttc",
				BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

		// 解决图片的相对路径问题
		renderer.getSharedContext()
				.setBaseURL(
						"file:/D:/Work/Demo2do/Yoda/branch/Yoda%20-%20All/conf/template/");
		renderer.layout();
		renderer.createPDF(os);
		os.close();
		
		
		// 现在开始进行下载操作
		MeetingFiles meetingFiles = new MeetingFiles();
		meetingFiles.setFilePath("download" + File.separator + "pdf" + File.separator
				+ this.PDF_FOLD_SUFIX+ meetingId);
		meetingFiles.setFileName(fileSaveName);
		meetingFiles.setFileSaveName(fileSaveName);
		this.getRequest().setAttribute("meetingFiles", meetingFiles);
		
		String from = "WEB";
		String documentRoot = fileSystemService.getDocumentRoot();
		getRequest().setAttribute("documentRoot", documentRoot);
		getRequest().setAttribute("from", from);
		

		return SUCCESS;
	}


 

你可能感兴趣的:(exception,String,object,File,download,fold)