Introduction:
Environment:
Vaadin 6.8 + AppletIntegration 1.2.9 (Add-on)
Spring-Security 3.1.2
Spring 3.1.0
EmbedPDF.jar (Version: 0.18)
Condition:
Due to business requirement, it only allows users to print PDF. No save function will be provided at all. (That means: No PDF file will be created at the Local directory)
I tired to find the solution on the internet but no suitable methods for me. Therefore, I used the EmbedPDF Applet instead of Adobe Reader to view PDF.
There are several steps that will be gone through in details:
- Add Vaadin addon AppletIntegration (not describe in details)
- Add the EmbedPDF Applet into your Vaadin Web Application
- Configure your Spring-Security file
Add the EmbedPDF Applet into your Vaadin Web Application
- Create a directory named "applet" under your WebContent/VAADIN/ It is because files under /WEB-INF directory are protected. It is not allowed to be directly accessed by users.
- Place your EmbedPDF.jar and a PDF file into "applet" directory
- In your source code, please pay attention to
- AppletClass - it always be "EmbedPDF.class"
- Codebase - it is the directory your EmbedPDF.jar placed into
- AppletParams - they are the parameters being passed into the EmbedPDF applet
- Please kindly refer to the EmbedPDF Specification Readme file. There are some parameters must be set.
VerticalLayout x = new VerticalLayout();
AppletIntegration applet2 = new AppletIntegration(){
private static final long serialVersionUID = 7124868233557401171L;
@Override
public void attach(){
setAppletArchives(Arrays.asList(new String[]{"EmbedPDF.jar"}));
setCodebase("/ProjectName/VAADIN/applet/");
setAppletClass("EmbedPDF.class");
setAppletParams("id", "embedId");
setAppletParams("pdf", "/ProjectName/VAADIN/applet/example.pdf");
setAppletParams("enableOpenWindow", "false");
setWidth("500px");
setHeight("500px");
}
};
x.addComponent(applet2);
getApplication().getMainWindow().addWindow(x);
|
The file structure |
Configure your Spring-Security file
- It is required to make the /VAADIN/applet/ directory to be Anonymously. It is because a new session will be created when the EmbedPDF applet is called. So, the spring security go through the authentication. It is required to pass the username and password to the spring security for the authentication process. There is an exception thrown in the app. server since spring security cannot get the username and password. So, it will block the EmbedPDF.jar and EmbedPDF.class to run.
- There lists some exceptions will be found if the spring security setting is not correct
- java.lang.ClassFormatError: Truncated class file
- java.lang.ClassFormatError: Incompatible magic value 537528892 in class file
<http auto-config="false" entry-point-ref="authenticationEntryPoint"
access-denied-page="/jsp/login_error" use-expressions="true">
<intercept-url pattern="/jsp/login*" access="permitAll" />
<intercept-url pattern="/jsp/login_error*" access="permitAll" />
<intercept-url pattern="/VAADIN/applet/**" access="permitAll" />
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<logout invalidate-session="true"/>
<custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER"/>
</http>
Addition Information: Delete the dynamic generated PDF file
- The following code describe how to get the absolute path in java for server
- It is better to use a helper to create a PDF file and return a new AppletIntegration
- When user removes the Applet from the layout, the remove will be deleted (see the "detach" function)
//TODO: create your own file
VerticalLayout x = new VerticalLayout();
final String filename = "example.pdf";
final String absReportzPath = String absProjPath = ((WebApplicationContext)ProjectApplication.getInstance().getContext())
.getHttpSession().getServletContext().getRealPath("/VAADIN/reportz/reportz/");
AppletIntegration applet2 = new AppletIntegration(){
private static final long serialVersionUID = 7124868233557401171L;
@Override
public void attach(){
setAppletArchives(Arrays.asList(new String[]{"EmbedPDF.jar"}));
setCodebase("/ProjectName/VAADIN/applet/");
setAppletClass("EmbedPDF.class");
setAppletParams("id", "embedId");
setAppletParams("pdf", "/ProjectName/VAADIN/applet/reportz/"+filename);
setAppletParams("enableOpenWindow", "false");
setWidth("500px");
setHeight("500px");
}
@Override
public void detach(){ //To delete file if the applet is removed.
String absPath = getAbsoluteReportzDirectory();
File delFile = new File(absReportzPath+"/"+fileName);
if(delFile.exists()){
delFile.delete();
}
}
};
x.addComponent(applet2);
getApplication().getMainWindow().addWindow(x);