Upgrade From PDF.js to PDF.js Express

120 Views Asked by At

I'm working on a Java JSP Struts project which has previously implemented PDF.js for previewing documents. Because of the the PDF.js library didn't support watermarks option I decided to move to PDF.js Express.

The below code snippet is the previous implementation

html body

<iframe id="content" style='height: 600px; width: 98%; min-width: 900px;' scrolling="no" src=""></iframe>

script

<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>

The getImage.do will go to the GetImageAction.java page and generate and get the PDF file,

struts-config

<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;
    }

When I added the new PDF.js Express it says to use the this format.

Because I don't have a direct path of the generated pdf file I cannot use the initialDoc property.

Can you suggest me any solution to use PDF.js Express

1

There are 1 best solutions below

4
Roman C On

You can use a direct path to the image action.

initialDoc = '${pageContext.request.contextPath}/getImage.do?imageDocID='+document.getElementById('imageDocID').value;