Java: Get The Generated Letter with GetPDF()

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="">the PostalMethods.GetPDF reference</a>
public class GetPdfTest {
    private PostalMethodsClient client;
    private int letterId;
    private String outFilename;
    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("");
        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.");
            System.out.println("OK, should be ready now.");
        } catch (Exception e) {
    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);