Completed
Push — master ( a43f74...38a66f )
by Ben
02:27
created

PdfWriter::closeDocument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4286
cc 2
eloc 13
nc 2
nop 0
1
<?php
2
/**
3
 * BaconPdf
4
 *
5
 * @link      http://github.com/Bacon/BaconPdf For the canonical source repository
6
 * @copyright 2015 Ben Scholzen (DASPRiD)
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Bacon\Pdf;
11
12
use Bacon\Pdf\Encryption\EncryptionInterface;
13
use Bacon\Pdf\Writer\ObjectWriter;
14
use SplFileObject;
15
16
class PdfWriter
17
{
18
    /**
19
     * @var ObjectWriter
20
     */
21
    private $objectWriter;
22
23
    /**
24
     * @var PdfWriterOptions
25
     */
26
    private $options;
27
28
    /**
29
     * @var string
30
     */
31
    private $permanentFileIdentifier;
32
33
    /**
34
     * @var string
35
     */
36
    private $changingFileIdentifier;
37
38
    /**
39
     * @var EncryptionInterface|null
40
     */
41
    private $encryption;
42
43
    /**
44
     * @param SplFileObject $fileObject
45
     */
46
    public function __construct(SplFileObject $fileObject, PdfWriterOptions $options)
47
    {
48
        $this->objectWriter = new ObjectWriter($fileObject);
49
50
        $options->freeze();
51
        $this->options = $options;
52
53
        $this->objectWriter->writeRawLine(sprintf("%PDF-%s", $this->options->getVersion()));
54
        $this->objectWriter->writeRawLine("%\xff\xff\xff\xff");
55
56
        $this->permanentFileIdentifier = hex2bin(md5(microtime()));
57
        $this->changingFileIdentifierFileIdentifier = $this->permanentFileIdentifier;
0 ignored issues
show
Bug introduced by
The property changingFileIdentifierFileIdentifier does not seem to exist. Did you mean changingFileIdentifier?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58
    }
59
60
    /**
61
     * Closes the document by writing the file trailer.
62
     *
63
     * While the PDF writer will remove all references to the passed in file object in itself to avoid further writing
64
     * and to allow the file pointer to be closed, the callee may still have a reference to it. If that is the case,
65
     * make sure to unset it if you don't need it.
66
     *
67
     * Any further attempts to append data to the PDF writer will result in an exception.
68
     */
69
    public function closeDocument()
70
    {
71
        $this->objectWriter->writeRawLine('trailer');
72
        $this->objectWriter->startDictionary();
73
74
        $this->objectWriter->writeName('Id');
75
        $this->objectWriter->startArray();
76
        $this->objectWriter->writeHexadecimalString($this->permanentFileIdentifier);
77
        $this->objectWriter->writeHexadecimalString($this->changingFileIdentifier);
78
        $this->objectWriter->endArray();
79
80
        if (null !== $this->encryption) {
81
            $this->objectWriter->writeName('Encrypt');
82
            $this->encryption->writeEncryptDictionary($this->objectWriter);
83
        }
84
85
        $this->objectWriter->endDictionary();
86
        $this->objectWriter->writeRawLine("%%%EOF");
87
    }
88
89
    /**
90
     * Creates a PDF writer which writes everything to a file.
91
     *
92
     * @param  string $filename
93
     * @return static
94
     */
95
    public static function toFile($filename)
96
    {
97
        return new static(new SplFileObject($filename, 'wb'));
0 ignored issues
show
Bug introduced by
The call to PdfWriter::__construct() misses a required argument $options.

This check looks for function calls that miss required arguments.

Loading history...
98
    }
99
100
    /**
101
     * Creates a PDF writer which outputs everything to the STDOUT.
102
     *
103
     * @return static
104
     */
105
    public static function output()
106
    {
107
        return new static(new SplFileObject('php://stdout', 'wb'));
0 ignored issues
show
Bug introduced by
The call to PdfWriter::__construct() misses a required argument $options.

This check looks for function calls that miss required arguments.

Loading history...
108
    }
109
}
110