主頁(yè) > 知識(shí)庫(kù) > 動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法

動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法

熱門標(biāo)簽:聊城智能電銷機(jī)器人外呼 企業(yè)怎么在聯(lián)通申請(qǐng)400電話 南京新思維電話機(jī)器人 地圖標(biāo)注市場(chǎng)怎么樣 泰州泰興400電話 怎么申請(qǐng) 好操作的電話機(jī)器人廠家 如何用中國(guó)地圖標(biāo)注數(shù)字點(diǎn) 百度地圖添加標(biāo)注圖標(biāo)樣式 南昌市地圖標(biāo)注app

最近工作中遇到不少問(wèn)題??偨Y(jié)一下。這段代碼主要功能是將一個(gè)生成JSP頁(yè)面轉(zhuǎn)發(fā)成PDF輸出到頁(yè)面

需要利用ITEXT

String html = ServletUtils.forward(request,response,"/WEB-INF/jsp/depot/print/jhd.jsp"); //轉(zhuǎn)發(fā)請(qǐng)求到j(luò)sp,返回解析之后的內(nèi)容而不是輸出到瀏覽器
//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方法執(zhí)行完畢之后不會(huì)輸出內(nèi)容到瀏覽器,而是把輸出到字節(jié)流,最后以字符串的形式返回
* @param request
* @param response
* @param src
* @return
*/
public static String forward(HttpServletRequest request, HttpServletResponse response, String src) {
try{
/* ↓↓↓↓↓重新構(gòu)造response,修改response中的輸出流對(duì)象,使其輸出到字節(jié)數(shù)組↓↓↓↓↓ */
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;
}
};
/* ↑↑↑↑↑↑重新構(gòu)造response,修改response中的輸出流對(duì)象,使其輸出到字節(jié)數(shù)組↑↑↑↑↑↑ */
//執(zhí)行forward操作
request.getRequestDispatcher(src).forward(request,response);
//把字節(jié)流中的內(nèi)容太轉(zhuǎn)為字符串
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轉(zhuǎn)換成pdf,以字節(jié)數(shù)組的形式返回pdf文件
* @param html
* @return pdf字節(jié)數(shù)組
* @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);
//使用我們的字體提供器,并將其設(shè)置為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();
}
}

以上所述是小編給大家介紹的動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

您可能感興趣的文章:
  • Spring boot 跳轉(zhuǎn)到j(luò)sp頁(yè)面的實(shí)現(xiàn)方法
  • ajax 提交數(shù)據(jù)到后臺(tái)jsp頁(yè)面及頁(yè)面跳轉(zhuǎn)問(wèn)題
  • jsp頁(yè)面顯示數(shù)據(jù)庫(kù)的數(shù)據(jù)信息表
  • JSP頁(yè)面跳轉(zhuǎn)方法小結(jié)
  • bootstrap制作jsp頁(yè)面(根據(jù)值讓table顯示選中)
  • 攔截JSP頁(yè)面,校驗(yàn)是否已登錄詳解及實(shí)現(xiàn)代碼
  • 在JSP頁(yè)面中獲取當(dāng)前日期時(shí)間的方法
  • 詳解直接訪問(wèn)WEB-INF目錄下的JSP頁(yè)面的方法

標(biāo)簽:烏蘭察布 臨汾 山南 銅川 開(kāi)封 吉林 自貢 白銀

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  動(dòng)態(tài),jsp,頁(yè)面,轉(zhuǎn),PDF,輸出,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章