flying saucer 将 xhtml ---> pdf
View Code
/** * 在servlet中 **/ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer; import com.lowagie.text.pdf.BaseFont; /** * * @author joshy * @version */ public class PDFServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // response.setContentType("application/pdf"); /** * 可注意的问题有: 1.html所有的标签都得是小写的,大写不识别。 2.所有的标签要成对出现:如:<div></div> 3.图片要先用renderer指定一个路径,代码中引用图片是要写与指定路径对应的相对路径 4.上面的html.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>"); 一行不能去掉,否则就不能执行成功了,不知道为什么。 目前我就总结了这么多,还不全,请大家一起努力,谢谢。 */ StringBuffer html = new StringBuffer(); // DOCTYPE 必需写否则类似于 这样的字符解析会出现错误 html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> "); html.append(" <html xmlns=\"http://www.w3.org/1999/xhtml\">"); html.append("<head>"); html.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>"); html.append(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>"); html.append("<title>test</title>"); html.append("</head>"); html.append("<body>"); html.append("<h1>"); html.append("名1字"); html.append("</h1>"); html.append("<h3>"); html.append("名字"); html.append("</h3>"); html.append("<div>"); html.append("<ul>"); html.append("<li><a href=\"http://service.qq.com/special/aq/info/45293.html\">我的密保手机不用了,怎么找回密码?</a></li>"); html.append("<li><a href=\"http://service.qq.com/special/aq/info/45273.html\" >申诉回执编号在哪里查询?</a></li>"); html.append("<li><a href=\"http://service.qq.com/special/aq/info/13895.html\">申诉处理时间是多久?</a></li>"); html.append("<li><a href=\"http://service.qq.com/special/aq/info/13953.html\" >如何避免qq帐号被盗?</a></li>"); html.append("</ul>"); html.append("</div>"); html.append("</body>"); html.append("</html>"); // parse our markup into an xml Document try { String outputFile = "E:/demo_3.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.setDocumentFromString(html.toString()); renderer.layout(); renderer.createPDF(os); os.close(); } catch (Exception ex) { ex.printStackTrace(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } // </editor-fold>}
在类中
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.BaseFont; public class FirstDoc { public static void main(String[] args) throws IOException, DocumentException { String inputFile = "samples/ddd.xhtml"; String url = new File(inputFile).toURI().toURL().toString(); String outputFile = "ccc.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(url); //解决中文支持问题 ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/Fonts/arialuni.ttf", 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(); } }
在类中
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.BaseFont; public class FirstDoc { public static void main(String[] args) throws IOException, DocumentException { String inputFile = "samples/ddd.xhtml"; String url = new File(inputFile).toURI().toURL().toString(); String outputFile = "ccc.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(url); //解决中文支持问题 ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont("C:/Windows/Fonts/arialuni.ttf", 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(); } }
我在IBM工作,可以为大家内部推荐IBM各种职位
IBM全球职位尽在以下链接(请在浏览器中打开,QQ/微信 会阻止):
http://ibmreferrals.com/
很乐意为感兴趣的小伙伴分享:我的面试经验^_^
如需咨询,请邮件发送以下邮箱,有问必回
1026096425@qq.com