欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

动态jsp页面转PDF输出到页面的实现方法

程序员文章站 2023-11-24 21:29:04
最近工作中遇到不少问题。总结一下。这段代码主要功能是将一个生成jsp页面转发成pdf输出到页面 需要利用itext string html = serv...

最近工作中遇到不少问题。总结一下。这段代码主要功能是将一个生成jsp页面转发成pdf输出到页面

动态jsp页面转PDF输出到页面的实现方法

需要利用itext

string html = servletutils.forward(request,response,"/web-inf/jsp/depot/print/jhd.jsp"); //转发请求到jsp,返回解析之后的内容而不是输出到浏览器
//system.out.println(html);
byte[] pdf = pdfutils.html2pdf(html);
response.setcontenttype("application/pdf");
response.setheader("content-length",string.valueof(pdf.length));
response.setheader("connection","keep-alive");
response.setheader("accept-ranges","none");
response.setheader("x-frame-options","deny");
outputstream out = response.getoutputstream();
out.write(pdf);
out.flush();
public class servletutils {
/**
* 此forward方法执行完毕之后不会输出内容到浏览器,而是把输出到字节流,最后以字符串的形式返回
* @param request
* @param response
* @param src
* @return
*/
public static string forward(httpservletrequest request, httpservletresponse response, string src) {
try{
/* ↓↓↓↓↓重新构造response,修改response中的输出流对象,使其输出到字节数组↓↓↓↓↓ */
final bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream();
final servletoutputstream servletouputstream = new servletoutputstream() {
@override
public void write(int b) throws ioexception {
bytearrayoutputstream.write(b);
}
@override
public boolean isready() {
return false;
}
@override
public void setwritelistener(writelistener writelistener) {
}
};
final printwriter printwriter = new printwriter(new outputstreamwriter(bytearrayoutputstream, "utf-8"));
response = new httpservletresponsewrapper(response) {
public servletoutputstream getoutputstream() {
return servletouputstream;
}
public printwriter getwriter() {
return printwriter;
}
};
/* ↑↑↑↑↑↑重新构造response,修改response中的输出流对象,使其输出到字节数组↑↑↑↑↑↑ */
//执行forward操作
request.getrequestdispatcher(src).forward(request,response);
//把字节流中的内容太转为字符串
return new string(bytearrayoutputstream.tobytearray(),"utf-8");
}
catch (exception e){
throw new runtimeexception(e);
}
}
}
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.pdfwriter;
import com.itextpdf.tool.xml.pipeline;
import com.itextpdf.tool.xml.xmlworker;
import com.itextpdf.tool.xml.xmlworkerfontprovider;
import com.itextpdf.tool.xml.xmlworkerhelper;
import com.itextpdf.tool.xml.exceptions.cssresolverexception;
import com.itextpdf.tool.xml.html.cssappliers;
import com.itextpdf.tool.xml.html.cssappliersimpl;
import com.itextpdf.tool.xml.html.tags;
import com.itextpdf.tool.xml.parser.xmlparser;
import com.itextpdf.tool.xml.pipeline.css.cssresolver;
import com.itextpdf.tool.xml.pipeline.css.cssresolverpipeline;
import com.itextpdf.tool.xml.pipeline.end.pdfwriterpipeline;
import com.itextpdf.tool.xml.pipeline.html.htmlpipeline;
import com.itextpdf.tool.xml.pipeline.html.htmlpipelinecontext;
import java.io.*;
/**
* pdf工具类
*/
public class pdfutils {
/**
* 把html转换成pdf,以字节数组的形式返回pdf文件
* @param html
* @return pdf字节数组
* @throws ioexception
* @throws documentexception
* @throws cssresolverexception
*/
public static byte[] html2pdf(string html) throws ioexception, documentexception,cssresolverexception {
document document = new document(pagesize.a4);
bytearrayoutputstream os = new bytearrayoutputstream();
pdfwriter writer = pdfwriter.getinstance(document,os);
document.open();
xmlworkerfontprovider fontprovider = new xmlworkerfontprovider(){
@override
public font getfont(string fontname, string encoding, float size, int style) {
return super.getfont(fontname == null ? "宋体" : fontname, encoding, size, style);
}
};
fontprovider.addfontsubstitute("lowagie", "garamond");
fontprovider.setuseunicode(true);
//使用我们的字体提供器,并将其设置为unicode字体样式
cssappliers cssappliers = new cssappliersimpl(fontprovider);
htmlpipelinecontext htmlcontext = new htmlpipelinecontext(cssappliers);
htmlcontext.settagfactory(tags.gethtmltagprocessorfactory());
cssresolver cssresolver = xmlworkerhelper.getinstance().getdefaultcssresolver(true);
pipeline<?> pipeline = new cssresolverpipeline(cssresolver,new htmlpipeline(htmlcontext, new pdfwriterpipeline(document,writer)));
xmlworker worker = new xmlworker(pipeline, true);
xmlparser p = new xmlparser(worker);
p.parse(new inputstreamreader(new bytearrayinputstream(html.getbytes("gbk"))));
document.close();
return os.tobytearray();
}
}

以上所述是小编给大家介绍的动态jsp页面转pdf输出到页面的实现方法,希望对大家有所帮助