Search by

hryvinskyi / magento2-document-export

hryvinskyi

Documents a store hands out: CSV lines safe from spreadsheet formula injection, PDF documents in an embedded Unicode font with wrapping and page breaks, zip archives written in memory, and file downloads streamed as private attachments.

Package info

github.com/hryvinskyi/magento2-document-export

Type:magento2-module

pkg:composer/hryvinskyi/magento2-document-export

Statistics

Installs: 1

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-30 18:52 UTC

This package is auto-updated.

Last update: 2026-11-30 20:09:26 UTC


README

The documents a store hands out, as small building blocks: CSV lines that a spreadsheet never runs as a formula, PDF documents in an embedded Unicode font, zip archives written in memory, and file downloads streamed as private attachments. A module that exports data composes them and writes none of the formats itself.

What it does for a store

  • Safe CSV. CsvLineInterface::line() writes one line ended by CRLF. Each field is quoted as RFC 4180 says, and also when it holds a semicolon or a tab. A field that starts like a formula (a leading =, +, -, @, tab or carriage return), or has a part after a semicolon, tab or line break that starts with =, +, - or @, gets a leading ' so a spreadsheet shows it as text. CsvCellInterface::format() does the same for one field. FormulaGuardInterface::guard() adds only the ' and never quotes, for a writer that quotes every field itself, such as Magento's grid export.
  • PDF with real text. PdfWriterInterface draws headings, label and value pairs and paragraphs on A4 pages. The text is wrapped to its column, cleaned of control characters, drawn as text runs in the FreeSerif font file that ships with Magento (lib/internal/GnuFreeFont/FreeSerif.ttf), and continued on a new page when it no longer fits. Nothing in a text is read as markup or as a command.
  • Zip archives in memory. ZipWriterInterface::archive() packs files without PHP's zip module and without a temporary file. The timestamps and permissions are fixed, so the same files in the same order give the same bytes.
  • Private downloads. FileAttachmentFactoryInterface streams a file on the server a megabyte at a time. ContentAttachmentFactoryInterface sends bytes made in memory. Both answer an attachment no browser or proxy may keep: Cache-Control: no-store, no-cache, must-revalidate, max-age=0, Pragma: no-cache, an Expires in the past, X-Content-Type-Options: nosniff, a Content-Length, and the file name as an ASCII fallback plus the full name as RFC 5987 filename*.

Requirements

  • Magento Open Source or Adobe Commerce, PHP 8.1 to 8.5 (the range composer.json allows).
  • ext-mbstring, ext-zlib and magento/zend-pdf ^1.16. ext-zip is needed only to run the tests.

Installation

Run from the Magento root.

composer require hryvinskyi/magento2-document-export
bin/magento module:enable Hryvinskyi_DocumentExport
bin/magento setup:upgrade
bin/magento setup:di:compile  # production mode only
bin/magento cache:flush

The module has no settings and creates no table.

Using the blocks

Inject the interfaces you need. Each sample runs inside a class that takes them in its constructor.

// One CSV line; the cell `=SUM(A1)` is written as '=SUM(A1) and `a,b` as "a,b".
$line = $this->csvLine->line(['=SUM(A1)', 'a,b', 'plain']);

// One PDF document: take a fresh writer for each document.
$pdf = $this->pdfWriters->create();
$pdf->heading('Orders');
$pdf->pair('Order', '000000123');
$pdf->paragraph($longText);
$bytes = $pdf->finish();

// One value for a writer that quotes fields itself: `=SUM(A1)` becomes '=SUM(A1), `a,b` stays a,b.
$value = $this->formulaGuard->guard('=SUM(A1)');

// A zip archive: path inside the archive (relative, forward slashes) => content.
$zip = $this->zipWriter->archive(['data.json' => $json, 'sections/orders.csv' => $csv]);

// A private download of a file on the server, as a controller's result.
return $this->attachments->create($path, 'personal-data.zip', 'application/zip');

ZipWriterInterface::archive() throws \InvalidArgumentException for a path that is empty, absolute, holds a backslash, or holds a . or .. segment. FileAttachmentFactoryInterface::create() throws Magento\Framework\Exception\FileSystemException when the file's size cannot be read.

Limits

  • The zip format written is plain zip without ZIP64: more than 65 535 entries, or any size or offset of 4 GiB or more, is refused. A file is deflated when that makes it smaller and stored as it is otherwise.
  • The PDF writer draws with one font, the FreeSerif file of the installation.
  • A PDF writer makes one document: take a fresh one from PdfWriterFactoryInterface for every document.

Registrations

Where What
preference Hryvinskyi\DocumentExport\Api\Csv\FormulaGuardInterface Hryvinskyi\DocumentExport\Model\Csv\FormulaGuard
preference Hryvinskyi\DocumentExport\Api\Csv\CsvCellInterface Hryvinskyi\DocumentExport\Model\Csv\CsvCell
preference Hryvinskyi\DocumentExport\Api\Csv\CsvLineInterface Hryvinskyi\DocumentExport\Model\Csv\CsvLine
preference Hryvinskyi\DocumentExport\Api\Pdf\PdfWriterFactoryInterface Hryvinskyi\DocumentExport\Model\Pdf\PdfWriterFactoryAdapter
preference Hryvinskyi\DocumentExport\Api\Pdf\PdfWriterInterface Hryvinskyi\DocumentExport\Model\Pdf\ZendPdfWriter (not shared: every resolve gives a new writer)
preference Hryvinskyi\DocumentExport\Api\Download\FileAttachmentFactoryInterface Hryvinskyi\DocumentExport\Model\Download\FileAttachmentFactoryAdapter
preference Hryvinskyi\DocumentExport\Api\Download\ContentAttachmentFactoryInterface Hryvinskyi\DocumentExport\Model\Download\ContentAttachmentFactoryAdapter
preference Hryvinskyi\DocumentExport\Api\Archive\ZipWriterInterface Hryvinskyi\DocumentExport\Model\Archive\ZipWriter

The module registers no route, command, cron job, event or plugin.

Extension point

Every Api\ interface is @api and replaceable by a preference. A store that needs a different PDF look replaces the PdfWriterInterface and PdfWriterFactoryInterface preferences with its own classes; the modules that call the interfaces keep working.

<preference for="Hryvinskyi\DocumentExport\Api\Pdf\PdfWriterFactoryInterface" type="Vendor\Module\Model\Pdf\WriterFactory"/>

Tests

  • Unit: vendor/bin/phpunit -c app/code/Hryvinskyi/DocumentExport/phpunit.xml.dist, run from the Magento root. It needs ext-zip, which reads the archives back.
  • Integration: the platform's harness in dev/tests/integration, one run at a time against its database. cd dev/tests/integration && php -d memory_limit=4G ../../../vendor/bin/phpunit -c phpunit.xml ../../../app/code/Hryvinskyi/DocumentExport/Test/Integration.
  • Static analysis: PHPStan 2.x at level 10 with the package's phpstan.neon.dist (phpstan analyse -c app/code/Hryvinskyi/DocumentExport/phpstan.neon.dist --level=10), and vendor/bin/phpcs --standard=app/code/Hryvinskyi/DocumentExport/phpcs.xml.dist app/code/Hryvinskyi/DocumentExport.