我正在开发 Struts 1 项目,该项目之前已经实现了 PDF.js 用于预览文档。由于 PDF.js 库不支持水印选项,我决定转向 PDF.js Express。
下面的代码片段是之前的实现
HTML 正文:
<iframe id="content" style='height: 600px; width: 98%; min-width: 900px;' scrolling="no" src=""></iframe>
脚本:
<script type="text/javascript">
$(document).ready(function() {
var base='web/viewer.html?file=';
var subUrl=encodeURIComponent('${pageContext.request.contextPath}/getImage.do?imageDocID='+document.getElementById('imageDocID').value);
var url=base+subUrl+'#page=1';
$('#content').attr('src',url);
});
</script>
getImage.do
将会进入GetImageAction.java
页面并生成并获取PDF文件,
struts-config.xml
:
<action
path="/getImage"
type="web.ead.webapp.ssd.common.action.GetImageAction"
scope="request">
</action>
GetImageAction.java
public class GetImageAction extends GenericAction {
static Object imageServerLock = new Object();
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException, Exception {
UserProfile userProfile = (UserProfile) request.getSession().getAttribute("UserProfile");
ActionForward actionForward = null;
String imageDocID = request.getParameter("imageDocID");
if (actionForward == null)
{
byte[] appletData = ImageManager.getMod2TifImage(imageDocID);
ObjectOutputStream outputToApplet = new ObjectOutputStream(response.getOutputStream());
outputToApplet.writeObject(appletData);
outputToApplet.flush();
outputToApplet.close();
actionForward = null;
}
return actionForward;
ActionForward actionForward = null;
try{
actionForward = super.execute(mapping, form, request, response);
String imageDocID = request.getParameter("imageDocID");
System.out.println(new Timestamp(System.currentTimeMillis()).toString() + "\trequest.getRemoteAddr() "
+ request.getRemoteAddr()
+ " Utility.getClientIp(request) : "
+ Utility.getClientIp(request) + " - " + imageDocID
+ " UserProfileID: " + userProfile.getUserProfileID());
byte[] image = null;
synchronized (imageServerLock) {
if(request.getSession().getAttribute("IMAGE-"+imageDocID) != null)
image = (byte[]) request.getSession().getAttribute("IMAGE-"+imageDocID);
if (actionForward == null) {
byte[] dest = addWatemark(image);
try{
PdfReader reader = new PdfReader(dest);
int pages = reader.getNumberOfPages();
}catch(Exception ex){
ex.printStackTrace();
image = ImageManager.optimizePDF(image);
dest = addWatemark(image);
}
response.setContentType("application/pdf");
OutputStream outs = response.getOutputStream();
outs.write(dest);
outs.close();
actionForward = null;
if(request.getSession().getAttribute("IMAGE-"+imageDocID) != null)
request.getSession().removeAttribute("IMAGE-"+imageDocID);
}
}
}catch(Exception e){
e.printStackTrace();
}
LoggerUtils.info(this.getClass().getName() + " - execute method --> Exit");
return actionForward;
}
当我添加新的 PDF.js Express 时,它说使用 this 格式。
因为我没有生成的 PDF 文件的直接路径,所以无法使用
initialDoc
属性。
您能给我建议如何使用 PDF.js Express 的任何解决方案吗?
您可以使用图像操作的直接路径。
initialDoc = '${pageContext.request.contextPath}/getImage.do?imageDocID='+document.getElementById('imageDocID').value;