1. 概述
1.1 问题描述
使用导出 API 导出文件时,如果报表的文字内容中包含特殊的角标,例如下图所示的化学符号角标:
由于后台 API 不支持导出角标,导出后,角标的效果跟预览的效果不一致,如下图所示:
1.2 解决方案
可以利用导出 URL 接口将文件导出到 JVM 内存里面,然后从内存中利用字节输出流写出到操作系统指定盘符。
2. 示例代码
注:Web工程也可以调用性能插件的 PDF 打印方法显示角标,但是实现方式与文档中提供的代码不一样。代码不需要额外写 startModule,否则插件效果不会生效。
package com.fr.io;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class testUrl {
public static void main(String[] args){
String pdfPath = "D:"+File.separator+"TEST.pdf";
try {
URL url = new URL("http://localhost:8020/FR10/decision/view/report?viewlet=WorkBook48.cpt&format=pdf&aa=9");
URLConnection connection = url.openConnection();
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
FileOutputStream outputStream = new FileOutputStream(new File(pdfPath));
byte[] buff = new byte[1024];
int byteRead;
while (-1!=(byteRead=bis.read(buff,0,buff.length))){
outputStream.write(buff,0,byteRead);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}