Saving & Output
Pdf is where a document becomes bytes — to a file, to an HTTP response, or into a PHP string.
Writing to a File#
Pdf::writeToFile() compiles a document and writes the result to disk:
use Pop\Pdf\Document;
use Pop\Pdf\Document\Page;
use Pop\Pdf\Pdf;
$document = new Document(new Page(Page::LETTER));
Pdf::writeToFile($document, filename: __DIR__ . '/quarterly-report.pdf');
The filename argument defaults to 'pop.pdf' in the current working directory, so
Pdf::writeToFile($document) with no second argument writes there instead of raising an error. A
one-page letter document with nothing on it compiled to 574 bytes on disk.
Point the path at a directory that already exists — writeToFile() hands the compiled string
straight to file_put_contents() with no directory check of its own, so a missing directory surfaces
as a PHP warning rather than a caught exception.
Sending to the Browser#
Pdf::outputToHttp() compiles a document and streams it as an HTTP response instead of a file:
use Pop\Pdf\Document;
use Pop\Pdf\Document\Page;
use Pop\Pdf\Pdf;
$document = new Document(new Page(Page::LETTER));
Pdf::outputToHttp($document, filename: 'quarterly-report.pdf', forceDownload: true);
The method always sets Content-type: application/pdf and a Content-disposition header built from
the filename and force-download flag. With the flag true, the header reads
attachment; filename=quarterly-report.pdf and the browser downloads the file. Leave it false —
the default — and the header drops the attachment; prefix, which most browsers open inline instead:
Pdf::outputToHttp($document, filename: 'quarterly-report.pdf'); // opens inline, no forced download
A fourth argument merges in extra headers as a name-to-value array. Content-type and
Content-disposition are always set by outputToHttp() itself, after the array merges in, so an
entry for either there is overwritten rather than kept. Anything else — Cache-Control below, or a
custom header — passes through untouched:
Pdf::outputToHttp($document, filename: 'quarterly-report.pdf', forceDownload: true, headers: [
'Cache-Control' => 'no-store',
]);
Control Content-disposition's filename through the method's own $filename argument rather than
this array — a value passed there is what ends up in the header regardless of what the array holds.
outputToHttp() never sets Content-Length itself, so a caller that wants one adds it through the
same array — strlen((string)$document) gives the exact byte count, since it's the same compile the
method runs internally:
$bytes = (string)$document;
Pdf::outputToHttp($document, filename: 'quarterly-report.pdf', forceDownload: true, headers: [
'Content-Length' => strlen($bytes),
]);
outputToHttp() calls PHP's header() function directly, so it only sends real HTTP headers when
the script is actually handling an HTTP request — from a command-line script, header() is silently
inert rather than raising a warning.
The compiled PDF bytes that follow are identical to what writeToFile() would write for the same
document, so the method is safe to call as the last statement in whatever endpoint serves the file.
Getting the Raw Bytes#
Casting a document to a string compiles it and returns the bytes in memory rather than writing them anywhere:
use Pop\Pdf\Document;
use Pop\Pdf\Document\Page;
$document = new Document(new Page(Page::LETTER));
$bytes = (string)$document;
Those bytes match a file writeToFile() writes for the same document exactly — nothing about
compression, metadata or the version string differs between the two paths. Use (string)$document
to store the compiled document somewhere other than the filesystem, hash it, or hand it to a
component that expects a string rather than a path.
Each cast recompiles the document from scratch, so two casts of the same, unchanged document produce identical bytes but do the compile work twice. Compile once into a local variable and reuse it for writing, hashing and length rather than casting again for each:
$bytes = (string)$document;
file_put_contents(__DIR__ . '/quarterly-report.pdf', $bytes);
$hash = md5($bytes);
$size = strlen($bytes);
See Also#
- Creating Documents — building the document
Pdfcompiles, plus the compression flag and version stringwriteToFile()andoutputToHttp()both honor at compile time - Metadata — the info object baked into every compiled file
- Importing —
Pdf::importFromFile(), the read side of a filewriteToFile()produced