0%

关于通过PDF.JS显示本地PDF文件和跨域获取文件

1.去官网下载pdf.js。
其中的view.html不要改,直接放在jsp里。
view.js是渲染pdf,下面的方法需要注意的是路径要对。之前文件流已获得,但是渲染不出来就是因为这里路径不对。

1
2
3
4
5
6
function configure(PDFJS) {
PDFJS.imageResourcesPath = path +’resources/ctrl/pdfjs/web/images/’;
PDFJS.workerSrc = path +’resources/ctrl/pdfjs/build/pdf.worker.js’;
PDFJS.cMapUrl = path +’resources/ctrl/pdfjs/web/web/cmaps/’;
PDFJS.cMapPacked = true;
}

2.修改viewer.js
var DEFAULT_URL = ‘compressed.tracemonkey-pldi-09.pdf‘ 里面是PDF的路径删除该变量定义;

3.ajax调用
写在view.js开头

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var path = $(“base”).attr(“href”);
var PDFData = “”;
$.ajax({
type: “GET”,
async: false, //
//dataType: “text”,
mimeType: ‘text/plain; charset=x-user-defined’,
url: path+”pdf”,//这里是读取的文件流
success: function(data) {
PDFData = data; //data就是byte[]数组,下面有介绍
}
});
var rawLength = PDFData.length;
//转换成pdf.js能直接解析的Uint8Array类型,见pdf.js-4068
var array = new Uint8Array(new ArrayBuffer(rawLength));
for (var i = 0;i < rawLength; i++) {
array[i] = PDFData.charCodeAt(i) & 0xff;
}
DEFAULT_URL = array;

方法一:
1.本地文件流后台处理,只用了web项目,没有任何框架

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void pdfStreamHandeler(HttpServletRequest request, HttpServletResponse response) {
filePath = “d://a.pdf”;
File file = new File(filePath);
byte[] data = null;
try {
FileInputStream input = new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.getOutputStream().write(data);
input.close();
} catch (Exception e) {
logger.error(“pdf文件处理异常:” + e.getMessage());
}
}

方法二:跨域(从云服务器下载)设计知识点(https://www.alibabacloud.com/help/zh/doc-detail/32016.htm?spm=a3c0i.o32014zh.b99.108.e7882928dW9FL)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@RequestMapping(value = “/pdf”, method = RequestMethod.GET)
@ResponseBody
public byte[] loadPdf1(Model model, HttpServletResponse response, HttpServletRequest request) {
final String keySuffixWithSlash = “a.pdf”;
// 创建OSSClient实例
OSSClient Server = new OSSClient(endpoint, accessKeyId, accessKeySecret);
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
GeneratePresignedUrlRequest request1 = new
GeneratePresignedUrlRequest(“aaa”, keySuffixWithSlash,
HttpMethod.GET);
// 设置过期时间
request1.setExpiration(expiration);
URL signedUrl = Server.generatePresignedUrl(request1);
String url = signedUrl.toString();
Server.shutdown();
System.out.println(“signed url for getObject: “ + signedUrl.toString());
HttpURLConnection httpConn = null;
URL urlObj;
try {
//将url转化成流,把他利用resonse读出,返回页面的是buffBytes
HttpURLConnHelper HttpURLConnHelper=new HttpURLConnHelper();
InputStream input=HttpURLConnHelper.loadFileFromURL(url);
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(“application/pdf;charset=UTF-8”);
ServletOutputStream out =null;
out = response.getOutputStream();
int read = 0;
byte buffBytes[] =new byte[1024];
while((read = input.read(buffBytes)) != -1) {
out.write(buffBytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}

其中HttpURLConnHelper是一个方法类,使用了别人的工具类