Skip to content

Gotenberg developer guide

The Gotenberg package sends Portable Document Format (PDF) conversion to an external service. In application code, keep that service boundary explicit: validate input, build a payload, send the request, parse the result, and handle service failure.

Use this guide when you build office-to-PDF conversion workflows, upload endpoints, service health checks, or transport policy around nextpdf/gotenberg.

LayerOwned byResponsibilityDo not put here
Upload or document sourceApplicationAuthorize the caller, validate the source, and choose conversion policy.Service endpoint or transport pinning decisions.
Format detectionnextpdf/gotenbergMap safe extensions to OfficeFormat.Trust in declared media types without application validation.
Bridgenextpdf/gotenbergBuild the multipart request, call Gotenberg, and parse the PDF response.Domain queries or storage policy.
Gotenberg serviceDeploymentConvert the office document to PDF.Hypertext Preprocessor (PHP) application authorization.
Core enginenextpdf/nextpdfImport or combine converted PDF data when needed.Office conversion.
StageBehaviorDeveloper action
Config creationApplication programming interface (API) endpoint, timeout, max size, API key, and pins load from configuration.Keep the service endpoint and token out of source code.
Input validationFile path, file size, filename, extension, and Uniform Resource Locator (URL) safety are checked.Reject unsupported input before you dispatch it.
Payload constructionGotenbergConvertPayload builds multipart form data.Preserve the original filename only when it is safe.
Service requestThe bridge sends the request to /forms/libreoffice/convert.Configure the timeout and transport policy.
Result parsingGotenbergResponseParser::parse() returns PDF bytes and metadata.Treat non-PDF responses and error responses as conversion failures.
PathPurpose
app/Pdf/Conversion/*Application wrapper around GotenbergBridge.
app/Pdf/Uploads/*Upload validation, storage, and filename policy.
app/Pdf/ConversionPolicy/*Size, format, and service selection policy.
tests/Pdf/Conversion/*Format, file, service, and transport tests.

Keep file validation separate from conversion. Your conversion service should receive already-authorized input, then still rely on package validation as defense in depth.

<?php
use NextPDF\Gotenberg\GotenbergBridge;
final readonly class OfficeConversionService
{
public function __construct(
private GotenbergBridge $bridge,
) {}
public function convertUploadedFile(string $safePath): string
{
$result = $this->bridge->convertFile($safePath);
if (!$result->isValid()) {
throw new RuntimeException('The conversion service did not return a valid PDF.');
}
return $result->pdfData;
}
}
PatternAPIUse whenConstraint
Convert file pathGotenbergBridge::convertFile()The document is already stored on disk.The path must be readable and policy-approved.
Convert in-memory bytesGotenbergBridge::convertString()Your application already has bytes from an upload or object store.The filename still controls format detection.
Build payload directlyGotenbergConvertPayloadYou need multipart bytes for tests or custom transport code.Keep boundary generation outside user input.
Parse response directlyGotenbergResponseParser::parse()Custom Hypertext Transfer Protocol (HTTP) calls still need package parsing.You must pass the expected OfficeFormat.

GotenbergBridge::isAvailable() is a readiness signal, not your only runtime guard. A service can pass readiness and still fail the next conversion.

CheckPurposeOperational note
Config validityDetect a missing API endpoint.Run this during app boot or deployment checks.
/health availabilityDetect whether the service is reachable.Use this for readiness dashboards.
Small fixture conversionDetect broken LibreOffice behavior or service regression.Run this in scheduled smoke tests, not on every request.
Timeout monitoringDetect slow service behavior.Track this by format and file size.
Extension pointUse it forConstraint
PHP Standard Recommendation (PSR)-18 ClientInterfaceCustom HTTP client behavior.Must follow PSR-18 response and exception semantics.
PSR-17 factoriesRequest and stream creation.Required for bridge construction.
HtmlSecurityPolicyInterfaceParse-layer policy for Hypertext Markup Language (HTML) inputs.Complements Gotenberg security policy.
ResponseFactoryInterfacePinned cURL transport response construction.Required only when you use the package transport path.
GotenbergSecurityPolicyURL, file size, filename, and pin validation.Keep application authorization outside this layer.
  1. Start with convertString() in tests to keep fixtures explicit.
  2. Add convertFile() only after filesystem paths are controlled.
  3. Keep the max file size below service and infrastructure limits.
  4. Use isAvailable() for readiness signals, not as a replacement for error handling.
  5. Log filename, format, size, status, and duration. Do not log document bytes.
  6. Add timeout and failure-mode tests before you expose upload endpoints.
FailureWhere it should be handledRecommended response
Unsupported extensionFormat detection.Reject before you dispatch to the service.
Unsafe filenameSecurity policy.Reject it and normalize the upload naming policy.
Oversize fileUpload policy and package validation.Reject before you read or send large payloads.
Gotenberg unavailableBridge boundary.Return a retryable application error when appropriate.
Non-PDF responseResponse parser.Treat it as a conversion failure and capture service status.
Pin or URL validation failureTransport policy.Fail closed and alert operators.
ConcernDefaultWhen to override
Timeout30 seconds.Increase only after you measure real service latency.
Max file size52,428,800 bytes.Lower for public or multi-tenant endpoints.
API keyEmpty.Set for private Gotenberg deployments.
Supported formatsdocx, xlsx, pptx, odt, ods, odp.Add formats only when OfficeFormat and the parser support them.
Pin setsEmpty.Add pins with backup pins and a rotation procedure.
  • Format tests cover supported and unsupported extensions.
  • File tests cover missing files, unreadable files, oversize files, and unsafe filenames.
  • Service tests cover non-2xx responses, invalid response bodies, and transport exceptions.
  • Security tests cover private or invalid service URLs when the policy forbids them.
  • Timeout tests assert that the configured timeout is passed to the transport.
  • Fixture tests keep sample documents small and non-sensitive.