Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,

Tool used : Java
Steps
1)Generate PDF (using jasperireport 3.6.1) java
2)Store On Server
3)Apply digital signature on stored server
4)Ready for download on page.
code below
Java
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
	String newfile = (new StringBuilder("/JCN/PDF/")).append(statementType).append("/").append(jpaccno).append(cnopdfnm).append(".pdf").toString();
        OutputStream os = new FileOutputStream(newfile);               
	net.sf.jasperreports.engine.JasperPrint jasperPrint = JasperFillManager.fillReport(CNote, jasperParameter, con);
        
	exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
        exporter.setParameter(JRPdfExporterParameter.PERMISSIONS, new Integer(2068));
        exporter.exportReport();
        os.close();

        String coordinateQry = (new StringBuilder("select dg_point from doc_con where rownum=1 and doc_typ='")).append(statementType).append("' ").toString();
        rs = st.executeQuery(coordinateQry);
        rs.next();
        String coordinate = rs.getString(1);

        onlineDigitalSign(newfile, outs, coordinate);

        outs.close();
}
 public void onlineDigitalSign(String s, OutputStream outputstream, String coordinate)
    {
        try
        {
            logger.info((new StringBuilder("New File for online DG.sign...")).append(s).toString());
            BaseFont basefont = BaseFont.createFont("/usr/local/fonts/ZurichBT.TTF", "Cp1252", true);
            FontFactory.register("/usr/local/fonts/ZurichBT.TTF");
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            
		keystore.load(new FileInputStream("/mypath/sg.ks"), "prd34".toCharArray());
            
	    String s1 = (String)keystore.aliases().nextElement();
            PrivateKey privatekey = (PrivateKey)keystore.getKey(s1, "prd34".toCharArray());
            java.security.cert.Certificate acertificate[] = keystore.getCertificateChain(s1);
            PdfReader pdfreader = new PdfReader(s);
            PdfStamper pdfstamper = PdfStamper.createSignature(pdfreader, outputstream, '\0');
            PdfSignatureAppearance pdfsignatureappearance = pdfstamper.getSignatureAppearance();
            pdfsignatureappearance.setCrypto(privatekey, acertificate, null, PdfSignatureAppearance.WINCER_SIGNED);
            pdfsignatureappearance.setReason("Digitally Signed");
            pdfsignatureappearance.setLocation("in");
            String DGP[] = new String[4];
            DGP = coordinate.split(",");
            Float DGP1 = Float.valueOf(0.0F);
            Float DGP2 = Float.valueOf(0.0F);
            Float DGP3 = Float.valueOf(0.0F);
            Float DGP4 = Float.valueOf(0.0F);
            DGP1 = Float.valueOf(Float.parseFloat(DGP[0].trim()));
            DGP2 = Float.valueOf(Float.parseFloat(DGP[1].trim()));
            DGP3 = Float.valueOf(Float.parseFloat(DGP[2].trim()));
            DGP4 = Float.valueOf(Float.parseFloat(DGP[3].trim()));
            pdfsignatureappearance.setVisibleSignature(new Rectangle(DGP1.floatValue(), DGP2.floatValue(), DGP3.floatValue(), DGP4.floatValue()), 1, null);
            int i = pdfreader.getNumberOfPages();
            int j = 0;
            do
            {
                if(j > i)
                    break;
                if(++j == i)
                {
                    PdfContentByte pdfcontentbyte = pdfstamper.getOverContent(j);
                    pdfcontentbyte.beginText();
                    pdfcontentbyte.setFontAndSize(basefont, 9F);
                    pdfcontentbyte.showTextAligned(6, "This is a digitally signed ", 28F, 14F, 0.0F);
                    pdfcontentbyte.endText();
                }
            } while(true);
            HashMap hashmap = pdfreader.getInfo();
            hashmap.put("Subject", "My Statement");
            hashmap.put("Title", "My Statement");
            hashmap.put("Creator", "my name");
            hashmap.put("Producer", "my product Name");
            pdfstamper.setMoreInfo(hashmap);
            pdfstamper.close();
            logger.info("Finished.1..");
        }
        catch(Exception e)
        {
            logger.info((new StringBuilder("Exception Occured in onlineDigitalSign.....")).append(e.getMessage()).toString());
            logger.error((new StringBuilder("Exception Occured in onlineDigitalSign.....")).append(e).toString());
        }
    }


What I have tried:

we have tried setContentType=force-download

response.setContentType("application/force-download");
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=CNotes.pdf");
Posted
Comments
Richard Deeming 2-Sep-16 11:36am    
application/force-download is not a valid MIME-type. You need to set the content type to application/pdf.
ShaktisinhRathod 6-Sep-16 7:11am    
Thanks for reply ,Earlier we have set application/pdf however it did not work so we have tried force-download. safari browser adds ".txt" extension in downloaded pdf.

Richard Deeming 6-Sep-16 7:49am    
Because you're not specifying a valid MIME type.

To prevent the file from opening in the browser, user the Content-Disposition header instead.
ShaktisinhRathod 6-Sep-16 8:55am    
Thanks again for reply,please forgive me if i am taking more time ...
I have set content type "application/pdf" still issue persist.
Still when we download the pdf it sets .txt i.e FileName.pdf.txt"
Thanks a ton for your help!
Richard Deeming 6-Sep-16 9:02am    
Try removing the response.reset(); line, or moving it before the setContentType line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900