How to Convert PostScript to PDF with Java
This Java walkthrough uses an OkHttp multipart request from a JVM application. It converts a PostScript (.ps) file into PDF through the pdfRest Convert to PDF API Tool and includes a custom .joboptions profile. The complete sample is included below so Java developers can see the file handling, request construction, and response path together.
Why PostScript to PDF with Java?
Java is often the orchestration layer around established composition, publishing, or enterprise print systems that still emit PostScript. This conversion creates a PDF result that a portal, archive, or later document service can consume.
A publishing application might receive a .ps proof from its composition engine and apply a profile approved by the production team before exposing the converted PDF to editors. The profile file travels as an ordinary multipart input rather than as hard-coded Java settings.
The API also works without that profile, using default settings. Keeping the optional file handling visible makes the decision clear to maintainers who need a reproducible conversion configuration.
Java Code Example for PostScript to PDF
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
public class PdfFromPostscript {
// 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 PostScript to PDF with a custom .joboptions profile.
* A .joboptions file contains Adobe Distiller-compatible conversion settings. The
* profile is optional; omit job_options to use default settings. pdfRest applies a
* supplied profile with Datalogics PDF Converter SDK. Datalogics maintains the SDK
* in partnership with Adobe, using the same Adobe technology that powers Distiller.
* Pair with /postscript for PDF
* refrying: PDF -> PostScript -> PDF. Some print and prepress workflows use this lossy
* roundtrip to rebuild or normalize page content.
*/
public static void main(String[] args) throws IOException {
File inputFile = new File(args.length > 0 ? args[0] : "/path/to/sample.ps");
File jobOptionsFile = new File(args.length > 1 ? args[1] : "/path/to/custom.joboptions");
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/postscript")))
.addFormDataPart(
"job_options",
jobOptionsFile.getName(),
RequestBody.create(jobOptionsFile, MediaType.parse("application/octet-stream")))
.addFormDataPart("output", "pdf_from_postscript");
Request request =
new Request.Builder()
.url(API_URL + "/pdf")
.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 PostScript to PDF example, Java sends the multipart request to /pdf. 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.
Apply the PostScript Conversion Profile
inputFile.getName(),
RequestBody.create(inputFile, MediaType.parse("application/postscript")))
.addFormDataPart(
"job_options",
jobOptionsFile.getName(),
RequestBody.create(jobOptionsFile, MediaType.parse("application/octet-stream")))
.addFormDataPart("output", "pdf_from_postscript");
Request request =
new Request.Builder()
.url(API_URL + "/pdf")
.header("Api-Key", apiKey)
Here, Java sends the PostScript source and the optional profile as separate files. A .joboptions file contains Adobe Distiller-compatible conversion settings; pdfRest applies a supplied profile with Datalogics PDF Converter SDK, maintained in partnership with Adobe and using the same Adobe technology that powers Distiller. Omitting job_options uses default settings.
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/postscript")))
.addFormDataPart(
"job_options",
For the PostScript to PDF 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 PostScript to PDF 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
You have now used Java to turn a PostScript source into a managed PDF and make its conversion profile explicit. That gives the surrounding application a deliberate choice between a team-maintained .joboptions file and the service defaults.
Try representative postscript to pdf inputs in API Lab. For the authoritative request fields, accepted values, and response contract, consult the Convert to PDF API Tool documentation. The same request pattern can then be adapted to your Java application’s error handling, retention policy, and delivery workflow.
A JSON-payload PostScript-to-PDF example is available for Java as well. That form uploads the .ps and optional .joboptions files first, then passes their resource IDs to /pdf. View the JSON-payload sample on GitHub.