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;
 
public class GetPdfTest {
 
    private PostalMethodsClient client;
    private int letterId;
    private String outFilename;
 
    @Before
    public void setUp() {
        client = new PostalMethodsClientFactory().build(TestConstants.USERNAME, TestConstants.PASSWORD);
 
        // 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;
 
        String tmpDir = System.getProperty("java.io.tmpdir");
        String pathSeparator = System.getProperty("file.separator");
        outFilename = tmpDir + pathSeparator + new String(letterId + ".pdf");
 
        // Give PostalMethods some time to render the document
        try {
            System.out.println("Waiting a bit to give PostalMethods a chance to process our test letter.");
            Thread.currentThread().sleep(20000);
            System.out.println("OK, should be ready now.");
        } catch(Exception e) {}
    }
 
    @Test
    public void testSendLetter() {
 
        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);
    }
}