How to Convert PDF to PostScript with Java
This Java walkthrough uses an OkHttp multipart request from a JVM application. It converts a PDF into PostScript through /postscript with visible output controls for a production workflow. The complete sample is included below so Java developers can see the file handling, request construction, and response path together.
Why PDF to PostScript with Java?
JVM applications can support PostScript-based downstream systems while continuing to receive and manage PDFs as their primary application documents. The conversion endpoint supplies a separate output resource rather than mutating the source file.
An enterprise print coordinator might route selected pages of an approved PDF to a legacy renderer that expects Level 3 PostScript. The Java request carries the page range, scaling, rotation, binary-output, and annotation decisions alongside the uploaded input.
This keeps production settings in reviewable application code and allows response IDs to be recorded with the job for traceability or later retrieval.
Java Code Example for PDF to PostScript
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
public class Postscript {
// By default, we use the US-based API service. This is the primary endpoint for global use.
private static final String API_URL = "https://api.pdfrest.com";
// For GDPR compliance and enhanced performance for European users, replace the URL above
// with https://eu-api.pdfrest.com. For more information, visit
// https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
/* This sample converts PDF to PostScript through /postscript. Pairing this endpoint
* with /pdf creates a PDF -> PostScript -> PDF workflow commonly called PDF refrying.
* Some print and prepress workflows use it to rebuild, flatten, or normalize page
* content, but the lossy roundtrip can discard PDF-specific features. These settings
* request Level 3, text-safe output, all pages at original scale, shrink-to-fit without
* rotation, and printable annotations.
*/
public static void main(String[] args) throws IOException {
File inputFile = new File(args.length > 0 ? args[0] : "/path/to/sample.pdf");
Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
String apiKey = dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY);
MultipartBody.Builder form =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"file",
inputFile.getName(),
RequestBody.create(inputFile, MediaType.parse("application/pdf")))
.addFormDataPart("ps_level", "3")
.addFormDataPart("page_range", "all")
.addFormDataPart("binary_output", "false")
.addFormDataPart("scale", "1")
.addFormDataPart("rotate", "false")
.addFormDataPart("shrink_to_fit", "true")
.addFormDataPart("print_annotations", "true")
.addFormDataPart("output", "postscript_from_pdf");
Request request =
new Request.Builder()
.url(API_URL + "/postscript")
.header("Api-Key", apiKey)
.post(form.build())
.build();
send(request);
}
private static void send(Request request) throws IOException {
OkHttpClient client = new OkHttpClient.Builder().readTimeout(60, TimeUnit.SECONDS).build();
try (Response response = client.newCall(request).execute()) {
System.out.println("Result code " + response.code());
if (response.body() != null) System.out.println(response.body().string());
if (!response.isSuccessful()) System.exit(1);
}
}
}
Source: View the sample on GitHub
Breaking Down the Code
Configure the service and input
// By default, we use the US-based API service. This is the primary endpoint for global use. private static final String API_URL = "https://api.pdfrest.com"; // For GDPR compliance and enhanced performance for European users, replace the URL above // with https://eu-api.pdfrest.com. For more information, visit // https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work. private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
For this PDF to PostScript example, Java sends the multipart request to /postscript. The Java samples use OkHttp for multipart HTTP requests and dotenv support for repository configuration. The sample project supplies those dependencies; applications that copy the class elsewhere need equivalent dependencies and secure runtime configuration. The sample’s placeholder path and API key make the moving pieces visible; replace them with a real local path and protected runtime credential before using the request.
Select PostScript Output Settings
"file",
inputFile.getName(),
RequestBody.create(inputFile, MediaType.parse("application/pdf")))
.addFormDataPart("ps_level", "3")
.addFormDataPart("page_range", "all")
.addFormDataPart("binary_output", "false")
.addFormDataPart("scale", "1")
.addFormDataPart("rotate", "false")
.addFormDataPart("shrink_to_fit", "true")
.addFormDataPart("print_annotations", "true")
.addFormDataPart("output", "postscript_from_pdf");
The Java sample explicitly requests ps_level=3, all pages, binary output, no rotation, unit scale, shrink-to-fit, and printable annotations. Those choices describe the new PostScript result; they do not edit or replace the uploaded PDF.
Build the multipart request
Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
String apiKey = dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY);
MultipartBody.Builder form =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"file",
inputFile.getName(),
RequestBody.create(inputFile, MediaType.parse("application/pdf")))
.addFormDataPart("ps_level", "3")
.addFormDataPart("page_range", "all")
For the PDF to PostScript form, okHttp’s MultipartBody.Builder constructs the form and assigns its boundary. Each file part carries a filename and media type, while conversion options are ordinary text parts.
Read the output resource
private static void send(Request request) throws IOException {
OkHttpClient client = new OkHttpClient.Builder().readTimeout(60, TimeUnit.SECONDS).build();
try (Response response = client.newCall(request).execute()) {
System.out.println("Result code " + response.code());
if (response.body() != null) System.out.println(response.body().string());
if (!response.isSuccessful()) System.exit(1);
}
}
}
After the PDF to PostScript call, the response block checks the HTTP status before printing its body. On success, the JSON contains the output resource ID and URL; on failure, the printed body gives the API error detail needed for diagnosis.
Beyond the Tutorial
This Java example submitted a PDF to /postscript and selected the controls that shape the resulting PostScript file. Retain the returned resource ID whenever the output needs to be downloaded, monitored, or used by a following workflow step.
Try representative pdf to postscript inputs in API Lab. For the authoritative request fields, accepted values, and response contract, consult the pdfRest API Reference Guide. The same request pattern can then be adapted to your Java application’s error handling, retention policy, and delivery workflow.
For Java applications that already hold the source as a managed resource, the repository includes a JSON-payload PDF-to-PostScript example. It uploads the PDF separately and sends the returned ID with the PostScript options. View the JSON-payload sample on GitHub.