Saiku图表导出时中文显示问题的解决方法
Saiku图表导出时png,jpg,pdf三种格式的中文显示都有问题,目前找到一种不太完善的解决方法(中文可以显示但不清晰),需要修改Saiku项目下的ExporterResource.java文件,同时需要在项目Linux环境中安装中文字体,完成后重启项目即可。
后续找到更好的解决方案时会更新此文或者给出新文章地址。
Linux环境中安装中文字体请参考:http://blog.163.com/shexinyang@126/blog/static/13673931220147442149228/?newFollowBlog
ExporterResource.java文件修改如下:
/*
* Copyright 2012 OSBI Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.saiku.web.rest.resources; import org.saiku.olap.query2.ThinQuery;
import org.saiku.web.rest.objects.resultset.QueryResult;
import org.saiku.web.rest.util.ServletUtil;
import org.saiku.web.svg.Converter; import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter; import org.apache.batik.svggen.SVGConverter;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.ImageTranscoder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID; import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; /**
* QueryServlet contains all the methods required when manipulating an OLAP Query.
* @author Paul Stoellberger
*
*/
@Component
@Path("/saiku/{username}/export")
@XmlAccessorType(XmlAccessType.NONE)
public class ExporterResource { private static final Logger log = LoggerFactory.getLogger(ExporterResource.class); private ISaikuRepository repository; private Query2Resource query2Resource; public void setQuery2Resource(Query2Resource qr){
this.query2Resource = qr;
} public void setRepository(ISaikuRepository repository){
this.repository = repository;
} /**
* Export query to excel file format.
* @summary Export to excel.
* @param file The file
* @param formatter The cellset formatter
* @param name The name
* @param servletRequest The servlet request.
* @return A response containing an excel file.
*/
@GET
@Produces({"application/json" })
@Path("/saiku/xls")
public Response exportExcel(@QueryParam("file") String file,
@QueryParam("formatter") String formatter,@QueryParam("name") String name,
@Context HttpServletRequest servletRequest)
{
try {
Response f = repository.getResource(file);
String fileContent = new String( (byte[]) f.getEntity());
String queryName = UUID.randomUUID().toString();
//fileContent = ServletUtil.replaceParameters(servletRequest, fileContent);
// queryResource.createQuery(queryName, null, null, null, fileContent, queryName, null);
// queryResource.execute(queryName, formatter, 0);
Map<String, String> parameters = ServletUtil.getParameters(servletRequest);
ThinQuery tq = query2Resource.createQuery(queryName, fileContent, null, null);
if (parameters != null) {
tq.getParameters().putAll(parameters);
}
if (StringUtils.isNotBlank(formatter)) {
HashMap<String, Object> p = new HashMap<String, Object>();
p.put("saiku.olap.result.formatter", formatter);
if (tq.getProperties() == null) {
tq.setProperties(p);
} else {
tq.getProperties().putAll(p);
}
}
query2Resource.execute(tq);
return query2Resource.getQueryExcelExport(queryName, formatter, name);
} catch (Exception e) {
log.error("Error exporting XLS for file: " + file, e);
return Response.serverError().entity(e.getMessage()).status(Status.INTERNAL_SERVER_ERROR).build();
}
} /**
* Export the query to a CSV file format.
* @summary Export to CSV.
* @param file The file
* @param formatter The cellset formatter
* @param servletRequest The servlet request
* @return A response containing a CSV file.
*/
@GET
@Produces({"application/json" })
@Path("/saiku/csv")
public Response exportCsv(@QueryParam("file") String file,
@QueryParam("formatter") String formatter,
@Context HttpServletRequest servletRequest)
{
try {
Response f = repository.getResource(file);
String fileContent = new String( (byte[]) f.getEntity());
//fileContent = ServletUtil.replaceParameters(servletRequest, fileContent);
String queryName = UUID.randomUUID().toString();
// query2Resource.createQuery(null, null, null, null, fileContent, queryName, null);
// query2Resource.execute(queryName,formatter, 0);
Map<String, String> parameters = ServletUtil.getParameters(servletRequest);
ThinQuery tq = query2Resource.createQuery(queryName, fileContent, null, null);
if (parameters != null) {
tq.getParameters().putAll(parameters);
} if (StringUtils.isNotBlank(formatter)) {
HashMap<String, Object> p = new HashMap<String, Object>();
p.put("saiku.olap.result.formatter", formatter);
if (tq.getProperties() == null) {
tq.setProperties(p);
} else {
tq.getProperties().putAll(p);
}
}
query2Resource.execute(tq);
return query2Resource.getQueryCsvExport(queryName);
} catch (Exception e) {
log.error("Error exporting CSV for file: " + file, e);
return Response.serverError().entity(e.getMessage()).status(Status.INTERNAL_SERVER_ERROR).build();
}
} /**
* Export the query response to JSON.
* @summary Export to JSON
* @param file The file
* @param formatter The cellset formatter
* @param servletRequest The servlet request
* @return A response containing a JSON query response.
*/
@GET
@Produces({"application/json" })
@Path("/saiku/json")
public Response exportJson(@QueryParam("file") String file,
@QueryParam("formatter") String formatter,
@Context HttpServletRequest servletRequest)
{
try {
Response f = repository.getResource(file);
String fileContent = new String( (byte[]) f.getEntity());
fileContent = ServletUtil.replaceParameters(servletRequest, fileContent);
String queryName = UUID.randomUUID().toString();
// query2Resource.createQuery(null, null, null, null, fileContent, queryName, null);
// QueryResult qr = query2Resource.execute(queryName, formatter, 0);
Map<String, String> parameters = ServletUtil.getParameters(servletRequest);
ThinQuery tq = query2Resource.createQuery(queryName, fileContent, null, null);
if (parameters != null) {
tq.getParameters().putAll(parameters);
}
if (StringUtils.isNotBlank(formatter)) {
HashMap<String, Object> p = new HashMap<String, Object>();
p.put("saiku.olap.result.formatter", formatter);
if (tq.getProperties() == null) {
tq.setProperties(p);
} else {
tq.getProperties().putAll(p);
}
}
QueryResult qr = query2Resource.execute(tq);
return Response.ok().entity(qr).build();
} catch (Exception e) {
log.error("Error exporting JSON for file: " + file, e);
return Response.serverError().entity(e.getMessage()).status(Status.INTERNAL_SERVER_ERROR).build();
}
} /**
* Export the current resultset to an HTML file.
* @summary Export to HTML
* @param file The file
* @param formatter The formatter
* @param css The css
* @param tableonly Table only, or include chart
* @param wrapcontent Wrap content
* @param servletRequest The servlet reaquest.
* @return A reponse containing the HTML export.
*/
@GET
@Produces({"text/html" })
@Path("/saiku/html")
public Response exportHtml(@QueryParam("file") String file,
@QueryParam("formatter") String formatter,
@QueryParam("css") @DefaultValue("false") Boolean css,
@QueryParam("tableonly") @DefaultValue("false") Boolean tableonly,
@QueryParam("wrapcontent") @DefaultValue("true") Boolean wrapcontent,
@Context HttpServletRequest servletRequest)
{
try {
Response f = repository.getResource(file);
String fileContent = new String( (byte[]) f.getEntity());
fileContent = ServletUtil.replaceParameters(servletRequest, fileContent);
String queryName = UUID.randomUUID().toString();
query2Resource.createQuery(queryName, fileContent, null, null);
return query2Resource.exportHtml(queryName, formatter, css, tableonly, wrapcontent);
} catch (Exception e) {
log.error("Error exporting JSON for file: " + file, e);
return Response.serverError().entity(e.getMessage()).status(Status.INTERNAL_SERVER_ERROR).build();
}
} /**
* Export chart to a file.
* @summary Export Chart.
* @param type The export type (png, svg, jpeg)
* @param svg The SVG
* @param size The size
* @param name The name
* @return A reponse containing the chart export.
*/
@POST
@Produces({"image/*" })
@Path("/saiku/chart")
public Response exportChart(
@FormParam("type") @DefaultValue("png") String type,
@FormParam("svg") String svg,
@FormParam("size") Integer size,
@FormParam("name") String name)
{
try {
// final String imageType = type.toUpperCase();
Converter converter = Converter.byType("PDF");
if (converter == null)
{
throw new Exception("Image convert is null");
} // resp.setContentType(converter.getContentType());
// resp.setHeader("Content-disposition", "attachment; filename=chart." + converter.getExtension());
// final Integer size = req.getParameter("size") != null? Integer.parseInt(req.getParameter("size")) : null;
// final String svgDocument = req.getParameter("svg");
// if (svgDocument == null)
// {
// resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing 'svg' parameter");
// return;
// }
if (StringUtils.isBlank(svg)) {
throw new Exception("Missing 'svg' parameter");
}
// final InputStream in = new ByteArrayInputStream(svg.getBytes("UTF-8"));
// final ByteArrayOutputStream out = new ByteArrayOutputStream();
// converter.convert(in, out, size);
// out.flush();
// byte[] doc = out.toByteArray();
// byte[] b = null;
/*if(getVersion()!=null && !getVersion().contains("EE")) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfReader reader = new PdfReader(doc);
PdfStamper pdfStamper = new PdfStamper(reader,
baos); URL dir_url = ExporterResource.class.getResource("/org/saiku/web/svg/watermark.png");
Image image = Image.getInstance(dir_url); for (int i = 1; i <= reader.getNumberOfPages(); i++) { PdfContentByte content = pdfStamper.getOverContent(i); image.setAbsolutePosition(450f, 280f);
image.setAbsolutePosition(reader.getPageSize(1).getWidth() - image.getScaledWidth(), reader.getPageSize
(1).getHeight() - image.getScaledHeight());
//image.setAlignment(Image.MIDDLE);
//content.addImage(image);
}
pdfStamper.close();
b = baos.toByteArray();
}
else{
b = doc;
}
b=doc;*/
svg ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + svg;
TranscoderInput localTranscoderInput = new TranscoderInput(new StringReader(svg));
final BufferedImage[] imagePointer = new BufferedImage[1];
ImageTranscoder t = new ImageTranscoder() { @Override
public BufferedImage createImage(int w, int h) {
return new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
} @Override
public void writeImage(BufferedImage image, TranscoderOutput out)
throws TranscoderException {
imagePointer[0] = image;
}
};
t.transcode(localTranscoderInput, null);
BufferedImage o = imagePointer[0];
ByteArrayOutputStream imgb = new ByteArrayOutputStream(); if(!type.equals("pdf")) {
/*PDDocument document = PDDocument.load(new ByteArrayInputStream(b));
PDFTextStripper stripper=new PDFTextStripper();
String s=stripper.getText(document);
System.out.println(s);
PDPageTree pdPages = document.getDocumentCatalog().getPages();
PDPage page = pdPages.get(0);
BufferedImage o = new PDFRenderer(document).renderImage(0);
ByteArrayOutputStream imgb = new ByteArrayOutputStream();*/ String ct = "";
String ext = "";
if(type.equals("png")){
ct = "image/png";
ext = "png";
}
else if(type.equals("jpg")){
ct = "image/jpg";
ext = "jpg";
}
ImageIO.write(o, "png", imgb);
byte[] outfile = imgb.toByteArray();
if(name == null || name.equals("")){
name = "chart";
}
return Response.ok(outfile).type(ct).header(
"content-disposition",
"attachment; filename = "+name+"." + ext).header(
"content-length", outfile.length).build();
}
else{
// PdfReader reader = new PdfReader(doc);
// PdfStamper pdfStamper = new PdfStamper(reader,
// baos);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Image image = Image.getInstance(o, null); Document doc=new Document();
doc.setPageSize(new Rectangle(image.getWidth(),image.getHeight()));
PdfWriter writer=PdfWriter.getInstance(doc,baos);
doc.open(); // PdfContentByte content = pdfStamper.getOverContent(i);
PdfContentByte content = writer.getDirectContent(); image.setAbsolutePosition(0, 0);
image.setAlignment(Image.MIDDLE);
content.addImage(image); doc.close();
// pdfStamper.close();
byte[] outfile = baos.toByteArray();
if(name == null || name.equals("")){
name = "chart";
}
return Response.ok(outfile).type(converter.getContentType()).header(
"content-disposition",
"attachment; filename = "+name+"." + converter.getExtension()).header(
"content-length", outfile.length).build();
}
} catch (Exception e) {
log.error("Error exporting Chart to " + type, e);
return Response.serverError().entity(e.getMessage()).status(Status.INTERNAL_SERVER_ERROR).build();
}
} /**
* Get the version.
* @summary Get the Saiku version.
* @return A String containing the current version.
*/
public static String getVersion() {
Properties prop = new Properties();
InputStream input = null;
String version = "";
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("org/saiku/web/rest/resources/version.properties");
try { //input = new FileInputStream("version.properties"); // load a properties file
prop.load(is); // get the property value and print it out
System.out.println(prop.getProperty("VERSION"));
version = prop.getProperty("VERSION");
} catch (IOException e) {
e.printStackTrace();
}
return version;
}
}
Saiku图表导出时中文显示问题的解决方法的更多相关文章
- JDBC插入数据时中文变为问号的解决方法
JDBC插入数据时中文变为问号的解决方法 制作人:全心全意 出现中文变问号的代码: String url = "jdbc:mysql://localhost:3306/test"; ...
- SecureCRT中文显示乱码的解决方法
注:本文出自:http://riching.iteye.com/blog/349754 最近开始用SecureCRT登陆linux系统,由于是新手,很多问题不清楚,碰到显示中文乱码的问题,困扰了好几天 ...
- PLSQL Developer 中文显示乱码的解决方法
PLSQL Developer 中文显示乱码是因为 Oracle 数据库所用的编码和 PLSQL Developer 所用的编码不同所导致的. 解决方法: 1. 先查询 Oracle 所用的编码 se ...
- URL传参时中文参数乱码的解决方法
URL传参时,中文参数乱码的解决: 今天在工作中遇到了这样的一个问题,在页面之间跳转时,我将中文的参数放入到url中,使用location进行跳转传参,但是发现接收到的参数值是乱码.我的代码是这样写的 ...
- Python 3下Matplotlib画图中文显示乱码的解决方法
解决办法: 因为乱码是Matplotlib缺少中文配置所导致的,所以我们只需要在程序中说明使用中文字体即可. 先选一个字体.在计算机中找到字体,选择一种中文字体,比如我这里用的是楷体 右键可以查看其属 ...
- NestedScrollView嵌套ListView时只显示一行的解决方法
在使用CoordinatorLayout和AppBarLayout实现嵌套滑动的时候,出现listview没有嵌套滑动: 如果要实现嵌套滑动,则需要添加NestedScrollView,但是结果发现l ...
- 如何解决 Matlab 画图时中文显示乱码的问题?
使用的是win10系统,从前几个月某一天,我的matlab的figure里的中文都变成了口口.很是郁闷,还以为是动到了什么配置引起的. 前几天更新了matlab 2018b,发现还有这个问题.就觉得不 ...
- get/post时中文乱码问题的解决办法
1.文章1 最近遇到一个问题:用get方法传递中文有问题,用post没有问题. 问题简单的描述是这样的: <a href="userGroup.jsp?userGroupName=&l ...
- linux中文显示乱码的解决办法
linux中文显示乱码的解决办法 linux中文显示乱码是一件让人很头疼的事情. linux中文显示乱码的解决办法:[root@kk]#vi /etc/sysconfig/i18n将文件中的内容修改为 ...
随机推荐
- Linq to sql 操作
1.往数据库添加数据 NorthwindDataContext abc = new NorthwindDataContext(); abc.Log = Console.Out; User a = ne ...
- HibernateDaoSupport的getSession()与HibernateTemplate的区别
在 Spring+Hibernate的集成环境里,如果DAO直接使用HibernateDaoSupport的getSession()方法获取 session进行数据操作而没有显式地关闭该session ...
- 新浪IP归属地API
之前用过腾讯的AIP,但是官方暂停这个服务了,新浪的API时间很久了,稳定性也很好,但愿能一劳永逸. ''' '''
- poj1316
Self Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20864 Accepted: 11709 De ...
- 网易云课堂_C语言程序设计进阶_第二周:指针:取地址运算和指针、使用指针、指针与数组、指针与函数、指针与const、指针运算、动态内存分配_2信号报告
2 信号报告(5分) 题目内容: 无线电台的RS制信号报告是由三两个部分组成的: R(Readability) 信号可辨度即清晰度. S(Strength) 信号强度即大小. 其中R位于报告第一 ...
- 在mangento后台调用wysiwyg编辑器
在mangento后台调用操蛋的wysiwyg编辑器: 1.在头部加载TincyMCE protected function _prepareLayout() { parent::_prepa ...
- iOS 性能测试 - FBMemoryProfiler
FBMemoryProfiler 是Facebook开源的一款用于分析iOS内存使用和检测循环引用的工具库. 脑补:http://www.cocoachina.com/ios/20160421/159 ...
- 用C#实现生成PDF文档
using System; using System.IO; using System.Text; using System.Collections; namespace PDFGenerator { ...
- Linux Kernel系列一:开篇和Kernel启动概要
前言 近期几个月将Linux Kernel的大概研究了一下,以下须要进行深入具体的分析.主要将以S3C2440的一块开发板为硬件实体.大概包含例如以下内容: 1 bootloader分析,以uboot ...
- sctf pwn300
拿到程序后,拉入IDA,大概看了一番后,尝试运行,进一步了解程序的功能. 发现NX enabled,No PIE. 一号是一个猜数字的游戏,二号是一个留言本,三号是打印出留言的内容,四号是退出. 观察 ...