• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近工作中遇到不少问题。总结一下。这段代码主要功能是将一个生成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输出到页面的实现方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
JavaScript实现链表插入排序和链表归并排序发布时间:2022-02-05
下一篇:
JSP MySQL插入数据时出现中文乱码问题的解决方法发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap