xref: /plugin/totext/vendor/prinsfrank/pdfparser/docs/7.11-file-specifications.md (revision 0fbee1892eb8b7339ce48e27e48e3fc821a8695f)
1# File Specifications (7.11)
2
3A PDF can refer to the contents of another file by using a file specification. File specifications are described in chapter 7.11 of the PDF specification. This library provides several ways to interact with them.
4
5Files can be linked to objects using ["associated files"](14.3-associated-files.md). This is the recommended way to retrieve file specifications for a document/page.
6
7Once you have retrieved file specifications, you can interact with them through several methods. All of them are available on the `PrinsFrank\PdfParser\Document\Object\Decorator\FileSpecification` class.
8
9## File specification strings (7.11.2)
10
11The file specification string is an identifier that can either be a file name or a URL, and can be absolute and relative. It can be retrieved using the `getFileSpecificationString` method;
12
13```php
14/** @var \PrinsFrank\PdfParser\Document\Object\Decorator\FileSpecification $fileSpecification */
15
16$fileSpecification->getFileSpecificationString();
17```
18
19## Embedded files (7.11.4)
20
21Files don't necessarily have to be embedded in the PDF document. They can also be referenced/external data. If they are embedded, they can be retrieved using the `getEmbeddedFile` method;
22
23```php
24/** @var \PrinsFrank\PdfParser\Document\Object\Decorator\FileSpecification $fileSpecification */
25
26$fileSpecification->getEmbeddedFile();
27```
28
29If this method returns `null`, it means that the file is not embedded.
30
31### Embedded file parameters (Table 45)
32
33Stored within an Embedded file dictionary are several parameters with information about the file:
34
35```php
36/** @var \PrinsFrank\PdfParser\Document\Object\Decorator\EmbeddedFile $embeddedFile */
37
38$embeddedFile->getLength();
39$embeddedFile->getFileSpecificInformation();
40$embeddedFile->getSize();
41$embeddedFile->getCreationDate();
42$embeddedFile->getModificationDate();
43$embeddedFile->getParams();
44```
45
46### Embedded file content
47
48To retrieve the content of an embedded file, you can use the `getStream` method. This will return a `PrinsFrank\Stream\Stream` object, that can be used to retrieve the content of the file. It points to a position within the PDF document or a file in memory depending on if the original PDF was encrypted and had filters.
49
50```php
51/** @var \PrinsFrank\PdfParser\Document\Object\Decorator\EmbeddedFile $embeddedFile */
52$embeddedFile->getStream();
53```
54
55To get the full content of the file, you can use the `toString` method. This will return the content as a string. Be aware that this will consume a lot of memory if the file is large.
56
57```php
58/** @var \PrinsFrank\PdfParser\Document\Object\Decorator\EmbeddedFile $embeddedFile */
59$embeddedFile->getStream()->toString();
60```
61
62If you want to retrieve the content of the file while not increasing the memory usage by a lot, you can also read the file content in chunks. This can be done using the `read` method:
63
64```php
65/** @var \PrinsFrank\PdfParser\Document\Object\Decorator\EmbeddedFile $embeddedFile */
66$fileContentStream = $embeddedFile->getStream();
67const CHUNK_SIZE = 1024;
68for ($i = 0; $i < $fileContentStream->getSize(); $i += CHUNK_SIZE) {
69    file_put_contents('output.txt', $fileContentStream->read($i, CHUNK_SIZE), FILE_APPEND);
70}
71```
72