How to Convert PDF to PostScript with PHP
This PHP walkthrough uses Guzzle to send the file and conversion options. It converts a PDF into PostScript through /postscript with visible output controls for a production workflow. The complete sample is included below so PHP developers can see the file handling, request construction, and response path together.
Why PDF to PostScript with PHP?
PHP can generate PostScript from an uploaded PDF when a legacy publishing or device workflow is the next destination. The endpoint makes that format transformation available from the same server-side code that handles the rest of the request lifecycle.
For example, a web order system could prepare a PDF proof for a print-production service, applying the chosen page range, scale, and annotation behavior before handing over the PostScript result. The conversion happens remotely instead of relying on a shared print server.
Guzzle returns a response containing the output resource details, allowing the application to tie the generated PostScript file back to its originating order or request.
PHP Code Example for PDF to PostScript
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 PDF to PostScript through multipart /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. */ $inputPath = '/path/to/sample.pdf'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'application/pdf']], ['name' => 'ps_level', 'contents' => '3'], ['name' => 'page_range', 'contents' => 'all'], ['name' => 'binary_output', 'contents' => 'false'], ['name' => 'scale', 'contents' => '1'], ['name' => 'rotate', 'contents' => 'false'], ['name' => 'shrink_to_fit', 'contents' => 'true'], ['name' => 'print_annotations', 'contents' => 'true'], ['name' => 'output', 'contents' => 'postscript_from_pdf'], ]; $response = (new Client())->post($apiUrl . '/postscript', ['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 PDF to PostScript example, PHP sends the multipart request to /postscript. 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.
Select PostScript Output Settings
$inputPath = '/path/to/sample.pdf'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'application/pdf']], ['name' => 'ps_level', 'contents' => '3'], ['name' => 'page_range', 'contents' => 'all'], ['name' => 'binary_output', 'contents' => 'false'], ['name' => 'scale', 'contents' => '1'], ['name' => 'rotate', 'contents' => 'false'], ['name' => 'shrink_to_fit', 'contents' => 'true'], ['name' => 'print_annotations', 'contents' => 'true'], ['name' => 'output', 'contents' => 'postscript_from_pdf'],
The PHP 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
*/ $inputPath = '/path/to/sample.pdf'; $multipart = [ ['name' => 'file', 'contents' => Utils::tryFopen($inputPath, 'r'), 'filename' => basename($inputPath), 'headers' => ['Content-Type' => 'application/pdf']], ['name' => 'ps_level', 'contents' => '3'], ['name' => 'page_range', 'contents' => 'all'], ['name' => 'binary_output', 'contents' => 'false'], ['name' => 'scale', 'contents' => '1'], ['name' => 'rotate', 'contents' => 'false'], ['name' => 'shrink_to_fit', 'contents' => 'true'], ['name' => 'print_annotations', 'contents' => 'true'],
For the PDF to PostScript 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' => 'print_annotations', 'contents' => 'true'], ['name' => 'output', 'contents' => 'postscript_from_pdf'], ]; $response = (new Client())->post($apiUrl . '/postscript', ['headers' => ['Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'Accept' => 'application/json'], 'multipart' => $multipart]); echo $response->getBody();
After the PDF to PostScript 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
This PHP 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 PHP application’s error handling, retention policy, and delivery workflow.
For PHP 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.