PdfWriter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 2 Features 1
Metric Value
wmc 8
c 7
b 2
f 1
lcom 1
cbo 6
dl 0
loc 123
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A getDocumentInformation() 0 4 1
A addPage() 0 7 1
A importRasterImage() 0 10 1
A endDocument() 0 4 1
A toFile() 0 4 1
A output() 0 4 1
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\DocumentWriter;
15
use Bacon\Pdf\Writer\ObjectWriter;
16
use Bacon\Pdf\Writer\PageWriter;
17
use SplFileObject;
18
19
class PdfWriter
20
{
21
    /**
22
     * @var PdfWriterOptions
23
     */
24
    private $options;
25
26
    /**
27
     * @var ObjectWriter
28
     */
29
    private $objectWriter;
30
31
    /**
32
     * @var DocumentWriter
33
     */
34
    private $documentWriter;
35
36
    /**
37
     * @var EncryptionInterface
38
     */
39
    private $encryption;
40
41
    /**
42
     * @param SplFileObject    $fileObject
43
     * @param PdfWriterOptions $options
44
     */
45
    public function __construct(SplFileObject $fileObject, PdfWriterOptions $options = null)
46
    {
47
        if (null === $options) {
48
            $options = new PdfWriterOptions();
49
        }
50
51
        $this->options = $options;
52
53
        $fileIdentifier = md5(microtime(), true);
54
        $this->objectWriter = new ObjectWriter($fileObject);
55
        $this->documentWriter = new DocumentWriter($this->objectWriter, $options, $fileIdentifier);
56
        $this->encryption = $options->getEncryption($fileIdentifier);
57
    }
58
59
    /**
60
     * Returns the document information object.
61
     *
62
     * @return DocumentInformation
63
     */
64
    public function getDocumentInformation()
65
    {
66
        return $this->documentWriter->getDocumentInformation();
67
    }
68
69
    /**
70
     * Adds a page to the document.
71
     *
72
     * @param  float $width
73
     * @param  float $height
74
     * @return Page
75
     */
76
    public function addPage($width, $height)
77
    {
78
        $pageWriter = new PageWriter($this->objectWriter);
79
        $this->documentWriter->addPageWriter($pageWriter);
80
81
        return new Page($pageWriter, $width, $height);
82
    }
83
84
    /**
85
     * Imports a raster image into the document.
86
     *
87
     * @param  strings $filename
88
     * @param  bool    $useLossyCompression
89
     * @param  int     $compressionQuality
90
     * @return Image
91
     */
92
    public function importRasterImage($filename, $useLossyCompression = false, $compressionQuality = 80)
93
    {
94
        return new RasterImage(
95
            $this->objectWriter,
96
            $filename,
97
            $this->options->getPdfVersion(),
98
            $useLossyCompression,
99
            $compressionQuality
100
        );
101
    }
102
103
    /**
104
     * Ends the document by writing all pending data.
105
     *
106
     * While the PDF writer will remove all references to the passed in file object in itself to avoid further writing
107
     * and to allow the file pointer to be closed, the callee may still have a reference to it. If that is the case,
108
     * make sure to unset it if you don't need it.
109
     *
110
     * Any further attempts to append data to the PDF writer will result in an exception.
111
     */
112
    public function endDocument()
113
    {
114
        $this->documentWriter->endDocument($this->encryption);
115
    }
116
117
    /**
118
     * Creates a PDF writer which writes everything to a file.
119
     *
120
     * @param  string                $filename
121
     * @param  PdfWriterOptions|null $options
122
     * @return static
123
     */
124
    public static function toFile($filename, PdfWriterOptions $options = null)
125
    {
126
        return new static(new SplFileObject($filename, 'wb'), $options);
127
    }
128
129
    /**
130
     * Creates a PDF writer which outputs everything to the STDOUT.
131
     *
132
     * Make sure to send the appropriate headers beforehand if you are in a web environment.
133
     *
134
     * @param  PdfWriterOptions|null $options
135
     * @return static
136
     */
137
    public static function output(PdfWriterOptions $options = null)
138
    {
139
        return new static(new SplFileObject('php://stdout', 'wb'), $options);
140
    }
141
}
142