java playframework htmlToPdf

1 篇文章 0 订阅
1 篇文章 0 订阅

本来项目中想要使用playframework中自身的pdf模块实现html转换为pdf的功能,但是发现目前该模块对中文乱码和中文换行支持的不是很好,只能抛弃了附件中会提供本次开发中需要使用到的几个JAR包方便大家开发时使用。

 

public static String createPDF(String fileName,String content){

		fileName = StringUtil.trim(fileName);

		String fontPath=ContextUtil.getProperty("font.path");//在配置文件中指定字体的路径

		String filePath = ContextUtil.getProperty("pdf.path", "/public/download/");//生成的PDF文件存放的路径

		File f = Play.getFile(filePath+fileName+".pdf");

		try {

			OutputStream os  = new FileOutputStream(f);

			ITextRenderer tRend = new ITextRenderer();

			ITextFontResolver fontResolver = tRend.getFontResolver();

			fontResolver.addFont(fontPath, BaseFont.IDENTITY_H,BaseFont.EMBEDDED);

			tRend.setDocumentFromString(content);

			tRend.layout();

			tRend.createPDF(os);

	os.close();

		} catch (FileNotFoundException e) {

			ExceptionHandler.throwRuntimeException(e, "生成PDF文件时文件路径不存在", logger);

		} catch (DocumentException e) {

			ExceptionHandler.throwRuntimeException(e, "生成PDF文件时设置文件内容时出错", logger);

		} catch (IOException e) {

			ExceptionHandler.throwRuntimeException(e, "生成PDF文件时文件流关闭时出错", logger);

		}

			return f.getAbsolutePath();

		

	}

 

 

   在application.conf中增加以下内容:

   

font.path=file:/usr/share/fonts/simsun.ttc
pdf.path=/public/download/

 页面中增加的样式内容:

 

 

<style type="text/css">
 body {
	font-size: 12px;
	font-family: SimSun;
 }
 </style>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值