1

Using Groovy 1.8.6 and Grails 2.1.0

Using embedded API, after user signs document, browser is redirected back to my app. Using "Get Envelope Documents and Certificate" API to download document to server. URL format:

"${baseUrl}/envelopes/${envelopeId}/documents/combined"

Code snippet (with minor details removed):

private void getDocument(requestUrl) {
    def connection = urlConnect(requestUrl, null, "GET")
    if (connection.responseCode == 200) {
        savePDF(envelopeId, connection.inputStream)
    }
}
private void savePDF(envelopeId, inputStream) {
    String filePath = getSavedPDFPath(envelopeId)
    def pdfWriter = new File(filePath).newWriter()
    pdfWriter << inputStream
    pdfWriter.close()
}

What happens is that the resulting file is not 100% correct, Adobe Reader complains that "at least one signature is invalid". Reader at least knows that the file was signed by DocuSign, Inc., and can show details about the certificate.

NorthCat
  • 9,643
  • 16
  • 47
  • 50
tkmilbaugh
  • 25
  • 4
  • What happens if you login (manually) to the DocuSign web console and attempt to open/view the Envelope (documents) there? And -- are you consistently seeing this issue (for all Envelopes), is it a sporatic issue (only for some Envelopes), or is it a one-time issue (for one specific Envelope only)? – Kim Brandl Jun 17 '14 at 14:53
  • If I login to the console, I can view the document. If I download the document from the console, it opens fine in Adobe Reader. If I do a binary comparison of the manually downloaded file with the application downloaded file, there are thousands of mis-compares. This happens for all files that are downloaded with the application. – tkmilbaugh Jun 17 '14 at 15:02
  • FWIW, I've never experienced the issue you describe -- i.e., files I've downloaded via API have always opened without any issues. So, I doubt it's an issue with the content of the DocuSign API Response -- more likely it's an issue with the way you're accessing/saving the byte stream during your download process. Have you tried simply writing the contents of the byte stream (from the API response) directly to the browser? If that works successfully, then it'd narrow your issue down to something that's going wrong during your "save file" process. – Kim Brandl Jun 17 '14 at 15:34
  • I replaced the savePDF method with this (un-groovy) version, all is working well now: private void savePDF(envelopeId, connection) { FileOutputStream fop = null; File file; String filePath = getSavedPDFPath(envelopeId) try { file = new File(filePath); fop = new FileOutputStream(file); byte[] buffer = new byte[1024]; int numRead; while((numRead = connection.getInputStream().read(buffer)) > 0) { fop.write(buffer, 0, numRead); } fop.flush(); fop.close(); } catch (Exception e) { throw new RuntimeException(e); } } – tkmilbaugh Jun 17 '14 at 16:27
  • Glad to hear that. I'm going to add the code from your comment as an "Answer" on this thread, so that others may benefit from this information in the future. If you don't mind, please mark it as an "accepted" Answer so that others will know the answer is correct/valid....thanks! – Kim Brandl Jun 17 '14 at 17:05

1 Answers1

1

Per the Question's comment thread, the issue was being caused by the way the file was being saved. Using this code instead, the file saves / opens correctly:

private void savePDF(envelopeId, connection) 
{ 
    FileOutputStream fop = null; 
    File file; 
    String filePath = getSavedPDFPath(envelopeId); 
    try { 
        file = new File(filePath); 
        fop = new FileOutputStream(file); 
        byte[] buffer = new byte[1024]; 
        int numRead; 
        while((numRead = connection.getInputStream().read(buffer)) > 0) 
        { 
            fop.write(buffer, 0, numRead); 
        } 
        fop.flush(); 
        fop.close(); 
    } 
    catch (Exception e) 
    { 
        throw new RuntimeException(e); 
    } 
}
Kim Brandl
  • 13,125
  • 2
  • 16
  • 21