Flex端使用alivepdf直接导出PDF文件:测试中文有乱码
import org.alivepdf.colors.RGBColor;import org.alivepdf.data.Grid;import org.alivepdf.data.GridColumn;import org.alivepdf.display.Display;import org.alivepdf.drawing.Joint;import org.alivepdf.fonts.FontFamily;import org.alivepdf.fonts.Style;import org.alivepdf.layout.Align;import org.alivepdf.layout.Orientation;import org.alivepdf.layout.Size;import org.alivepdf.layout.Unit;import org.alivepdf.pages.Page;import org.alivepdf.pdf.PDF;import org.alivepdf.saving.Download;import org.alivepdf.saving.Method; protected var claimPDF:PDF; protected function exportPdf():void { claimPDF = new PDF(Orientation.LANDSCAPE, Unit.MM, Size.A4);//创建PDF实例 claimPDF.setDisplayMode (Display.FULL_WIDTH);//设置整个页面 claimPDF.setTitle("这是一个标题"); claimPDF.addPage();//添加一页(默认第一页) //添加图片流 claimPDF.textStyle(new RGBColor(0x0),1); // claimPDF.setFont(FontFamily.ARIAL , 13);//设置字体样式、大小 var idCol:GridColumn=new GridColumn("碳排放","percent",20,Align.CENTER,Align.CENTER); var nameCol:GridColumn=new GridColumn("NAME","name",50,Align.CENTER,Align.CENTER); var ageCol:GridColumn=new GridColumn("AGE","data",40,Align.CENTER,Align.CENTER); var columns:Array=new Array(idCol,nameCol,ageCol); var grid:Grid = new Grid(treeSource.children.toArray(), 300, 300, new RGBColor (0xCCCCCC), new RGBColor (0xCCCCCC), false, new RGBColor ( 0x0 ),1, Joint.MITER); grid.columns=columns; claimPDF.addGrid(grid,10,160); claimPDF.save(Method.REMOTE,"PDFServlet",Download.ATTACHMENT,"aaa.pdf"); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); int i = 0; int k = 0; int maxLength = request.getContentLength();// 获取请求的内容 byte[] bytes = new byte[maxLength];// 转换byte数组 String method = request.getParameter("method"); String name = request.getParameter("name");// 请求的文件名,aaa.pdf ServletInputStream si = request.getInputStream(); while (true) {// 循环读取文件流 k = si.read(bytes, i, maxLength); i += k; if (k <= 0) break; } if (bytes != null) { ServletOutputStream stream = response.getOutputStream();// 实例化 response.setContentType("application/pdf");// 内容类型 response.setCharacterEncoding("UTF-8");// 编码方式 response.setContentLength(bytes.length);// 设置内容长度 response.setHeader("Content-Disposition", method + ";filename=" + name); stream.write(bytes);// 写入 stream.flush(); stream.close();// 关闭流 } else { response.setContentType("text"); response.getWriter().write("bytes is null"); } }