Use PDF Ink to Embed File Streams in Your PDFs

We often handle a deluge of files for a single project—contracts, spreadsheets, presentations, licenses, and source data all floating around in different emails, shared drives, or chat threads. What if all of that could live in one self-contained PDF “package?”

This isn’t just a hypothetical scenario. File stream embedding, an advanced PDF feature, allows businesses to create intelligent, all-in-one digital containers. This approach streamlines workflows, enhances branding and brand loyalty, enhances compliance, and ensures data integrity.

The Power of the “All-in-One” Document

Below we list some practical, real-world uses for the powerful PDF stream embedding & file attachment capability:

User Manuals and Product Information: A product manual PDF can embed supplementary content like software installers, instructional videos, or related technical data sheets that are not practical to display directly in the main document.

E-Invoicing (ZUGFeRD format): This specification allows for a human-readable PDF invoice to contain an embedded, machine-readable XML file with structured invoice information. This enables automated processing by accounting systems while remaining viewable by any standard PDF reader.

Financial Services: Financial statements or audit reports can embed source data such as Excel spreadsheets with detailed revenue models, or other financial reconciliation data, to support transparency and future audits.

Project Management & Collaboration: A main project proposal can embed various related files, such as PowerPoint presentations, CAD drawings, or external links/documents, allowing a project team to access all relevant materials from a single document.

Legal and Compliance: A primary contract or legal filing can have supporting evidence, correspondence, or court orders embedded as attachments, ensuring all related information travels together for long-term archiving and accessibility. This supports legal protection and due diligence.

Healthcare and Research: Patient reports or clinical study submissions to regulatory bodies like the FDA can include embedded lab results, imaging files (X-rays, CT scans), and physician comments, ensuring a complete and compliant record.

PDF Ink and SetaPDF-Stamper can be used in PHP-based web applications to add embedded streams conditionally and dynamically, making these tasks magical and almost… fun.

Each PDF download can be customized programmatically for the file, purpose, and/or end user — without a human having to set it up manually each time. Personalized, data-driven documents like invoices or reports can be added to PDF files without having to create or save temporary files to disk. For developers, embedding data from a live stream (rather than a physical file) at runtime is efficient for memory management.

How to Add Embedded File Streams Using PDF Ink + SetaPDF-Stamper

How do we dynamically generate these complex PDFs from streams, without needing to save temporary files to disk?

The SetaSign API is designed to handle advanced PDF manipulations, including the dynamic embedding of file streams. It provides a robust, scalable way for your PHP-based system to generate rich, data-driven documents on the fly. This is another reason why we always recommend our customers upgrade their PDF Ink installations by purchasing the SetaPDF-Stamper as an add-on. (This purchase is optional, but the following code will not work without it.)

The following snippet is a very simple example of how a small, customized memory-generated text file can be added to a PDF during download:

use setasign\SetaPDF2\Core\EmbeddedFileStream;
use setasign\SetaPDF2\Core\FileSpecification;

function maybe_add_embedded_stream( object $document, object $writer, array $settings, object $pages, object $stamp )

  // Get the embedded files name tree
  $embedded_files = $document->getCatalog()->getNames()->getEmbeddedFiles();

  // Do something here to fetch customer's name & license key, possibly using $settings array.
  // If using WooCommerce we will have an order_id and product_id:
  $order_id = isset( $settings['order_id'] ) ? intval( $settings['order_id'] ) : 0;
  $order = wc_get_order( $order_id );
  // $product_id = isset( $settings['product_id'] ) ? intval( $settings['product_id'] ) : 0;

  $dynamically_generated_license_key = 'license12345';
  $dynamically_generated_name = $order->get_billing_first_name() ?? 'Jane Doe';

  $txt = 'License Key: ' . $dynamically_generated_license_key .
   '\n\n' . 'Generated for $dynamically_generated_name' .
   '\n' . 'Terms: ...legalese...';

  // https://manuals.setasign.com/setapdf-core-manual/embedded-file-streams/#index-3
  $file_spec = FileSpecification::createEmbedded(
    $document,
    new StringReader($txt),
    'license.txt',
    [
        EmbeddedFileStream::PARAM_CHECK_SUM => md5( $txt, true ),
        EmbeddedFileStream::PARAM_MODIFICATION_DATE => new \DateTime(
            '2025-05-05 00:00:01'
        )
    ],
    'text/plain'
  );
  $file_spec->setDescription( 'License document for main file.' );

  // Embed the dynamically created text file
  $embedded_files->add( Encoding::toPdfString('license.txt'), $file_spec );

  $document->getCatalog()->setPageMode( Document\PageMode::USE_ATTACHMENTS );

add_action( 'pdfink_before_setapdf_stamper_callback', 'maybe_add_embedded_stream', 10, 5 );

This is similar to the embedded file stream examples at SetaSign.com, and both are fairly basic implementations. If you do not understand the code or how to effect it, you should consider working with a developer, as this can get a bit complicated. However, once it is set up, such code can save your team a ton of time and effort managing file packages. And even without knowing how to write any code, if you are already using WooCommerce or Easy Digital Downloads (or even Download Monitor) with WordPress, it’s easy to get PDF Ink installed and rolling (and maybe upgrade later).

A PHP developer can interact with and leverage the SetaSign API directly from within common creative/e-commerce workflows using PDF Ink. Hooking into the SetaSign API allows PDF Ink users to:

  1. Define and manage the types of files we need to embed (XML, JPEGs, spreadsheets, etc.).
  2. Stream data directly from memory or a network location into the generated PDF document, avoiding local file system operations.
  3. Ensure compliance with specific standards like PDF/A-3 (the standard that permits embedded files) which are essential for long-term archiving and legal validity.

By combining the power of the SetaSign API with a user-friendly PDF Ink plugin integration, you can unlock the full potential of the portable document format, transforming documents into intelligent data containers. Streamline your document handling workflows by upgrading to PDF Ink and using it with SetaPDF-Stamper. For WordPress and other PHP-based sites, there simply is no competition!

If you are considering an stream-embedding implementation and have questions or want to bounce ideas, please get in touch. We are happy to consult.