How to Convert PostScript to PDF with PHP
This PHP walkthrough uses Guzzle to send the file and conversion options. 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 PHP developers can see the file handling, request construction, and response path together.
Why PostScript to PDF with PHP?
PHP applications can provide a simple API boundary between legacy PostScript generation and modern web delivery. A converted PDF is easier to expose in a customer portal or attach to an internal workflow than a raw .ps file.
A content-management service might receive a PostScript report from a back-office system, send its production .joboptions profile alongside the report, and make the returned PDF available to an approver. The profile can be maintained as a file rather than reimplemented in PHP.
If the application does not need specialized conversion settings, it can omit the optional profile. That leaves the rest of the multipart request unchanged and uses the service defaults.
PHP Code Example for PostScript to PDF
require 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Utils; // By default, we use the US-based API service. This is the primary endpoint for global use. $apiUrl = "https://api.pdfrest.com"; /* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work */ //$apiUrl = "https://eu-api.pdfrest.com"; /* This sample converts a PostScript (.ps) file 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. * Pairing this /pdf call with /postscript creates a PDF -> PostScript -> PDF workflow * commonly called PDF refrying. Some print and prepress workflows use it to rebuild or * normalize page content, but the lossy roundtrip can discard PDF-specific features. */ $inputPath = '/path/to/sample.ps'; $jobOptionsPath = '/path/to/custom.joboptions'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'application/postscript']], ['name' => 'job_options', 'contents' => Utils::tryFopen($jobOptionsPath, 'r'), 'filename' => basename($jobOptionsPath), 'headers' => ['Content-Type' => 'application/octet-stream']], ['name' => 'output', 'contents' => 'pdf_from_postscript'], ]; $response = (new Client())->post($apiUrl . '/pdf', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
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. $apiUrl = "https://api.pdfrest.com"; /* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work */ //$apiUrl = "https://eu-api.pdfrest.com";
For this PostScript to PDF example, PHP sends the multipart request to /pdf. The PHP samples use Guzzle for HTTP and PSR-7 stream utilities for uploads. Install Guzzle with Composer when moving the code outside the repository, and provide the API key through protected 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
$jobOptionsPath = '/path/to/custom.joboptions'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'application/postscript']], ['name' => 'job_options', 'contents' => Utils::tryFopen($jobOptionsPath, 'r'), 'filename' => basename($jobOptionsPath), 'headers' => ['Content-Type' => 'application/octet-stream']], ['name' => 'output', 'contents' => 'pdf_from_postscript'], ]; $response = (new Client())->post($apiUrl . '/pdf', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
Here, PHP 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
$inputPath = '/path/to/sample.ps'; $jobOptionsPath = '/path/to/custom.joboptions'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'application/postscript']], ['name' => 'job_options', 'contents' => Utils::tryFopen($jobOptionsPath, 'r'), 'filename' => basename($jobOptionsPath), 'headers' => ['Content-Type' => 'application/octet-stream']], ['name' => 'output', 'contents' => 'pdf_from_postscript'], ]; $response = (new Client())->post($apiUrl . '/pdf', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
For the PostScript to PDF form, guzzle accepts multipart fields as an array. Each entry names a field and supplies either stream content for a file or a simple string for an option, then Guzzle produces the correct multipart boundary.
Read the output resource
['name' => 'job_options', 'contents' => Utils::tryFopen($jobOptionsPath, 'r'), 'filename' => basename($jobOptionsPath), 'headers' => ['Content-Type' => 'application/octet-stream']], ['name' => 'output', 'contents' => 'pdf_from_postscript'], ]; $response = (new Client())->post($apiUrl . '/pdf', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
After the PostScript to PDF call, the example prints Guzzle’s response body. A production caller can decode that JSON, retain outputId, and branch on an unsuccessful response instead of assuming every submitted conversion completed.
Beyond the Tutorial
You have now used PHP 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 PHP application’s error handling, retention policy, and delivery workflow.
A JSON-payload PostScript-to-PDF example is available for PHP 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.