-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAsync.java
More file actions
68 lines (60 loc) · 2.61 KB
/
Async.java
File metadata and controls
68 lines (60 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// This example demonstrates creating a PDF using common options and saving it
// to a place on the filesystem.
//
// It is created asynchronously, which means DocRaptor will render it for up to
// 10 minutes. This is useful when creating many documents in parallel, or very
// large documents with lots of assets.
//
// DocRaptor supports many CSS and API options for output customization. Visit
// https://docraptor.com/documentation/ for full details.
//
// You can run this example with: ./script/run_java_file examples/Async.java
import com.docraptor.*;
import java.io.*;
import java.net.*;
import java.nio.file.*;
public class DocRaptorExample {
public static void main(String[] args) throws Exception {
DocApi docraptor = new DocApi();
ApiClient client = docraptor.getApiClient();
client.setUsername("YOUR_API_KEY_HERE"); // this key works in test mode!
try {
Doc doc = new Doc();
doc.setTest(true); // test documents are free but watermarked
doc.setDocumentType(Doc.DocumentTypeEnum.PDF);
doc.setDocumentContent("<html><body>Hello World!</body></html>");
// doc.setDocumentUrl("https://docraptor.com/examples/invoice.html");
// doc.setJavascript(true);
// PrinceOptions princeOptions = new PrinceOptions();
// princeOptions.setMedia("print"); // @media 'screen' or 'print' CSS
// princeOptions.setBaseurl("https://yoursite.com"); // the base URL for any relative URLs
// doc.setPrinceOptions(princeOptions);
// different method than the synchronous documents
AsyncDoc response = docraptor.createAsyncDoc(doc);
while (true) {
DocStatus statusResponse = docraptor.getAsyncDocStatus(response.getStatusId());
System.out.println("status: " + statusResponse.getStatus());
if (statusResponse.getStatus().equals("completed")) {
// createAsyncDoc() returns a binary string
byte[] docResponse = docraptor.getAsyncDoc(statusResponse.getDownloadId());
FileOutputStream file = new FileOutputStream("github-async.pdf");
file.write(docResponse);
file.close();
System.out.println("Successfully created github-async.pdf!");
return;
} else if (statusResponse.getStatus().equals("failed")) {
System.err.println("FAILED");
System.err.println(statusResponse);
return;
} else {
Thread.sleep(1000);
}
}
} catch (ApiException error) {
System.err.println(error);
System.err.println(error.getCode());
System.err.println(error.getMessage());
System.err.println(error.getResponseBody());
}
}
}