Completed
Push — master ( 1a4c19...c1db7b )
by Ben
02:15
created

PdfWriter::getDocumentInformation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\Options\PdfWriterOptions;
14
use Bacon\Pdf\Writer\ObjectWriter;
15
use SplFileObject;
16
17
class PdfWriter
18
{
19
    /**
20
     * @var ObjectWriter
21
     */
22
    private $objectWriter;
23
24
    /**
25
     * @var PdfWriterOptions
26
     */
27
    private $options;
28
29
    /**
30
     * @var string
31
     */
32
    private $permanentFileIdentifier;
33
34
    /**
35
     * @var string
36
     */
37
    private $changingFileIdentifier;
38
39
    /**
40
     * @var EncryptionInterface
41
     */
42
    private $encryption;
43
44
    /**
45
     * @var DocumentInformation
46
     */
47
    private $documentInformation;
48
49
    /**
50
     * @var array
51
     */
52
    private $objectOffsets = [];
53
54
    /**
55
     * @param SplFileObject    $fileObject
56
     * @param PdfWriterOptions $options
57
     */
58
    public function __construct(SplFileObject $fileObject, PdfWriterOptions $options = null)
59
    {
60
        if (null === $options) {
61
            $options = new PdfWriterOptions();
62
        }
63
64
        $this->objectWriter = new ObjectWriter($fileObject);
65
        $this->objectWriter->writeRawLine(sprintf("%PDF-%s", $this->options->getVersion()));
0 ignored issues
show
Bug introduced by
The method getVersion() does not seem to exist on object<Bacon\Pdf\Options\PdfWriterOptions>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        $this->objectWriter->writeRawLine("%\xff\xff\xff\xff");
67
68
        $this->permanentFileIdentifier = $this->changingFileIdentifier = md5(microtime(), true);
69
        $this->encryption = $options->getEncryption($this->permanentFileIdentifier);
70
        $this->documentInformation = new DocumentInformation();
71
    }
72
73
    /**
74
     * @return DocumentInformation
75
     */
76
    public function getDocumentInformation()
77
    {
78
        return $this->documentInformation;
79
    }
80
81
    /**
82
     * Closes the document by writing the file trailer.
83
     *
84
     * While the PDF writer will remove all references to the passed in file object in itself to avoid further writing
85
     * and to allow the file pointer to be closed, the callee may still have a reference to it. If that is the case,
86
     * make sure to unset it if you don't need it.
87
     *
88
     * Any further attempts to append data to the PDF writer will result in an exception.
89
     */
90
    public function closeDocument()
91
    {
92
        $xrefOffset = $this->writeXrefTable();
93
        $this->writeTrailer();
94
        $this->writeFooter($xrefOffset);
95
    }
96
97
    /**
98
     * Creates a PDF writer which writes everything to a file.
99
     *
100
     * @param  string                $filename
101
     * @param  PdfWriterOptions|null $options
102
     * @return static
103
     */
104
    public static function toFile($filename, PdfWriterOptions $options = null)
105
    {
106
        return new static(new SplFileObject($filename, 'wb'), $options);
107
    }
108
109
    /**
110
     * Creates a PDF writer which outputs everything to the STDOUT.
111
     *
112
     * @param  PdfWriterOptions|null $options
113
     * @return static
114
     */
115
    public static function output(PdfWriterOptions $options = null)
116
    {
117
        return new static(new SplFileObject('php://stdout', 'wb'), $options);
118
    }
119
120
    /**
121
     * Writes the xref table.
122
     *
123
     * @return int
124
     */
125
    private function writeXrefTable()
126
    {
127
        $this->objectWriter->ensureBlankLine();
0 ignored issues
show
Bug introduced by
The method ensureBlankLine() does not seem to exist on object<Bacon\Pdf\Writer\ObjectWriter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
        $xrefOffset = $this->objectWriter->currentOffset();
0 ignored issues
show
Bug introduced by
The method currentOffset() does not exist on Bacon\Pdf\Writer\ObjectWriter. Did you maybe mean getCurrentOffset()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
129
130
        $this->objectWriter->writeRawLine('xref');
131
        $this->objectWriter->writeRawLine(sprintf('0 %d', count($this->objectOffsets) + 1));
132
        $this->objectWriter->writeRawLine(sprintf('%010d %05d f ', 0, 65535));
133
134
        foreach ($this->objectOffsets as $offset) {
135
            $this->objectWriter->writeRawLine(sprintf('%010d %05d n ', $offset, 0));
136
        }
137
138
        return $xrefOffset;
139
    }
140
141
    /**
142
     * Writes the trailer.
143
     */
144
    private function writeTrailer()
145
    {
146
        $this->objectWriter->ensureBlankLine();
0 ignored issues
show
Bug introduced by
The method ensureBlankLine() does not seem to exist on object<Bacon\Pdf\Writer\ObjectWriter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
        $this->objectWriter->writeRawLine('trailer');
148
        $this->objectWriter->startDictionary();
149
150
        $this->objectWriter->writeName('Id');
151
        $this->objectWriter->startArray();
152
        $this->objectWriter->writeHexadecimalString($this->permanentFileIdentifier);
153
        $this->objectWriter->writeHexadecimalString($this->changingFileIdentifier);
154
        $this->objectWriter->endArray();
155
156
        $this->encryption->writeEncryptDictionary($this->objectWriter);
157
158
        $this->objectWriter->endDictionary();
159
    }
160
161
    /**
162
     * Writes the footer.
163
     *
164
     * @param int $xrefOffset
165
     */
166
    private function writeFooter($xrefOffset)
167
    {
168
        $this->objectWriter->ensureBlankLine();
0 ignored issues
show
Bug introduced by
The method ensureBlankLine() does not seem to exist on object<Bacon\Pdf\Writer\ObjectWriter>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
        $this->objectWriter->writeRawLine('startxref');
170
        $this->objectWriter->writeRawLine((string) $xrefOffset);
171
        $this->objectWriter->writeRawLine("%%%EOF");
172
    }
173
}
174