I am using icepdf
in my web project. It works fine when I run this project in eclipse and also run when I use this project war on tomcat version 8/9. But when I deploy this war in Linux instance all goes fine but pdf does not render in JFrame
. My java version and tomcat version also same as i locally use.
Here is my code
import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;
import java.io.InputStream;
import javax.swing.*;
public class PdfPreview {
public static void pdfPreview(InputStream stream) {
// build a component controller
SwingController controller = new SwingController();
SwingViewBuilder factory = new SwingViewBuilder(controller);
JPanel viewerComponentPanel = factory.buildViewerPanel();
// add interactive mouse link annotation support via callback
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
JFrame applicationFrame = new JFrame();
applicationFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
applicationFrame.getContentPane().add(viewerComponentPanel);
// Now that the GUI is all in place, we can try openning a PDF
controller.openDocument(stream, "Pdf Viewer", null);
// show the component
applicationFrame.pack();
applicationFrame.setVisible(true);
}}
Here stream
is coming from a source
The Swing based viewer is designed to run as a standalone application as a thick client. Your code is likely running OK on the server but the GUI will be loaded on the Server system and not on your system. There may also be a headless exception when the server tries to load the Swing subsystem.
If you want to launch the Viewer application via a web server you'll need to build and deploy a Java WebStart application (JWS). An example is here, http://anonsvn.icesoft.org/repo/icepdf/tags/icepdf-6.3.0/icepdf/examples/jws/ . You'll need to to a bit a your own research with regards to signing the jars.
You can also use the ICEpdf library to save a PDF document's page as an image and you can then use Tomcat to serve the image to the requesting client. There is some example code that uses JSF/ICEfaces http://anonsvn.icesoft.org/repo/icepdf/tags/icepdf-6.3.0/icepdf/examples/icefaces/ . The core capture is done by this Servlet class, http://anonsvn.icesoft.org/repo/icepdf/tags/icepdf-6.3.0/icepdf/examples/icefaces/src/main/java/org/icepdf/examples/jsf/viewer/servlet/PdfRenderer.java