Most PDF automation starts with a verb: merge, split, compress, rotate.
The code for that verb may be a library call. The production work begins around it.
A dependable document step has to answer questions the PDF function cannot:
- Where may the input come from?
- How do we prove that a URL returned a PDF rather than an HTML error page?
- What does a page range mean, and what happens when it is invalid?
- Where is the result stored?
- How long does it remain there?
- Which failures are safe to retry?
This article builds a small operational contract for that step. The examples use hosted PDF tools, but the design applies equally to a local library, a containerized CLI or an internal service.
1. Treat retrieval as part of the operation
If an API accepts a URL, it is also a network client. That makes fetching part of the product surface.
At minimum, decide:
- whether only HTTPS is accepted;
- whether redirects are followed;
- which hosts or address ranges are allowed;
- the maximum download time and byte count;
- whether authenticated or private documents are supported;
- what happens when the response is not actually a PDF.
An unrestricted URL field can become a server-side request forgery risk. A private-document URL can leak through logs. A link that works in a browser may return an HTML viewer rather than the PDF bytes.
For sensitive workflows, a pre-signed object-store URL with a short lifetime is usually easier to reason about than a public permanent link. If the tool only supports public URLs, treat that as an architectural constraint—not a footnote.
2. Make one action explicit
Avoid an input that asks the processor to infer what should happen. The caller should choose a narrow action and provide action-specific options.
{
"action": "merge",
"files": [
"https://example.com/report.pdf",
"https://example.com/appendix.pdf"
],
"options": {}
}
That contract is useful because a workflow can validate it before it moves any bytes. It also gives an AI agent a bounded tool call instead of arbitrary code execution.
The same principle applies to page ranges. Pick one documented syntax, define whether pages are one-based or zero-based, reject overlapping or out-of-range input consistently, and return the normalized selection in the result if possible.
3. Define what “compress” means
Compression is not one operation.
Structural compression removes redundant objects and rewrites streams without deliberately reducing visual quality. Image recompression may lower JPEG quality or resolution. Font subsetting, object cleanup and linearization affect different aspects of the file.
A “high compression” setting is meaningless unless the documentation explains the trade-off. For image-heavy scans, lossless structural compression may barely change the size. For an already optimized PDF, any further reduction may require visible degradation.
Record both input and output sizes. If the tool changes image quality, say so. If it falls back to a different strategy when a dependency is unavailable, expose that fact or at least document it.
4. Return an inspectable result
A binary file is necessary, but it is not a useful workflow response on its own.
A structured result should include:
{
"action": "merge",
"inputCount": 2,
"outputUrls": ["https://storage.example/result.pdf"],
"sizeBefore": 842190,
"sizeAfter": 837421,
"ms": 913
}
Those fields let the next step check that an output exists, compare its size, record processing time and decide whether a retry or human review is appropriate.
Use stable error categories as well: invalid input, unreachable source, corrupt PDF, resource limit, processing failure and result too large are materially different states.
5. Decide where bytes live
“We do not keep your data” is not a storage model.
Document:
- whether processing is in memory or on disk;
- whether inputs or outputs are written to platform storage;
- the retention policy governing that storage;
- whether logs include URLs or filenames;
- who can retrieve the result;
- how a caller can delete it early.
For regulated or confidential documents, a managed API may be the wrong boundary regardless of convenience. A local library or service inside the protected network may be the better design.
6. Give agents decisions, not bytes
An agent can be useful at the orchestration layer:
- identify the approved source documents;
- choose merge, split or compression;
- construct a typed request;
- inspect the structured result;
- ask for review when the outcome is ambiguous.
The deterministic processor should manipulate the file. The agent should not improvise a binary transformation or receive broader filesystem access than the workflow needs.
Tool descriptions matter here. “Process a PDF” is a poor agent tool. “Split a PDF into these one-based page ranges; reject any range outside the document” is much easier to call safely.
7. A concrete comparison
Howth Technology Factory's PDF Toolkit provides a useful example because it has two related delivery forms.
The Apify Actor documents nine actions: merge, split, compress, rotate, delete pages, add a text watermark, render PDF pages as images, assemble images into a PDF, and render a URL as PDF. It accepts URL-based inputs and returns a dataset record plus files in the run's key-value store.
The MCPize package is not the same interface. It exposes five typed tools: merge, split, structural compression, PDF-to-text and images-to-PDF. Its listing states explicit per-input, per-request, result and text-extraction caps.
That difference is the lesson: select a document tool by its contract, limits and data path—not by the label “PDF toolkit”.
When a specialist service is the right choice
Basic page manipulation is only one part of the PDF world. OCR, certified redaction, digital signatures, form filling, PDF/A validation, accessibility remediation and complex layout extraction need specialist capabilities.
Enterprise document APIs and commercial SDKs can be justified when those requirements matter. Local open-source libraries make sense when data cannot leave your environment or when you need deep control. A hosted narrow tool is attractive when the work is routine, the data policy fits and maintaining the processing stack would be needless overhead.
The decision is not “API or library?” in the abstract. It is: which boundary makes this particular workflow reliable, observable and appropriately constrained?
Top comments (0)