我试图通过仅添加一个带有多个标签的 ZPL 代码来生成多个标签,但是当我输入 ZPL 代码时,它只生成第一个标签。请帮助我
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter ZPL (type 'exit' to finish):");
String zpl = scanner.nextLine();
int labelIndex = 0; // Start with label index 0
while (!zpl.equalsIgnoreCase("exit")) {
// Adjust print density (8dpmm), label width (4 inches), label height (6 inches), and label index as necessary
URI uri = URI.create("https://api.labelary.com/v1/printers/8dpmm/labels/3.93701x5.90551/" + labelIndex + "/");
HttpRequest request = HttpRequest.newBuilder(uri)
.header("Accept", "application/pdf") // omit this line to get PNG images back
.POST(HttpRequest.BodyPublishers.ofString(zpl))
.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
byte[] body = response.body();
if (response.statusCode() == 200) {
File file = new File("label_" + labelIndex + ".pdf"); // Change file name for PNG images
Files.write(file.toPath(), body);
System.out.println("Label_" + labelIndex + " created successfully.");
} else {
String errorMessage = new String(body, StandardCharsets.UTF_8);
System.out.println("Failed to create label: " + errorMessage);
}
// Read the next ZPL input
System.out.println("Enter next ZPL (type 'exit' to finish):");
zpl = scanner.nextLine();
labelIndex++; // Increment label index for the next label
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
您正在将索引添加到 URL。通过测试 API,即使您传递了多个 ZPL,它也只会返回索引 0 的第一个 ZPL,索引 1 的第二个,依此类推。
如果您只能为所有标签生成 1 个文件,只需从 URL 中删除索引即可。
但是,如果您想为每个标签生成 1 个文件,则必须:
zpl
上的标签数量,并使用该数量多次调用此 API。不过,请注意速率限制。请参阅他们的 API 文档:https://labelary.com/service.html#limits