Java: Get The Generated Letter with GetPDF()

Download
Download our Java samples from this page.
Find this sample in directory /test/com/postalmethods/client
package com.postalmethods.client;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
 
import org.junit.Before;
import org.junit.Test;
 
import com.postalmethods.client.api.GetPdfResponse;
import com.postalmethods.client.api.SendLetterResponse;
 
/**
 * Integration test that calls the GetPDF web method.
 * <p>
 * For details about PostalMethods.GetPDF see
 * <a href="http://www.postalmethods.com/method/2009-02-26/GetPDF">the PostalMethods.GetPDF reference</a>
 */
public class GetPdfTest {
 
    private PostalMethodsClient client;
 
    private int letterId;
 
    private String outFilename;
 
    @Before
    public void setUp() {
        client = new PostalMethodsClientFactory().build();
 
        // Send a sample letter so we have a letterId to check status on.
        SendLetterResponse sendResponse = client.sendLetter("Junit test letter", TestConstants.SAMPLE_LETTER_FILENAME);
        int code = sendResponse.getResult().getCode();
        assertTrue(code > 0);
        this.letterId = code;
        System.out.println("Letter id is: " + this.letterId);
 
        String tmpDir = System.getProperty("java.io.tmpdir");
        String pathSeparator = System.getProperty("file.separator");
        outFilename = tmpDir + pathSeparator + new String(letterId + ".pdf");
 
        // This is a bit of a hack, but it does seem like we need to give the
        // PostalMethods API some time to process our test report before we
        // turn around and ask it for a rendered PDF.
        try {
            System.out.println("Waiting a bit to give PostalMethods a chance to process our test letter.");
            Thread.sleep(20000);
            System.out.println("OK, should be ready now.");
        } catch (Exception e) {
        }
    }
 
    @Test
    public void testGetPdf() {
 
        System.out.println("Making call to GetPDF...");
        GetPdfResponse response = client.getPdf(letterId, outFilename);
        System.out.println("Service call completed:\n" + response.getResult().toString());
 
        int code = response.getResult().getCode();
        assertEquals(-3000, code);
 
        System.out.println("PDF saved to: " + outFilename);
    }
}