Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Pdf
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 writeToFile
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 outputToHttp
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 importFromFile
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 importRawData
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 importFromImages
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Pdf;
15
16use Pop\Pdf\Document\AbstractDocument;
17use Pop\Pdf\Document\Exception;
18
19/**
20 * Pop Pdf class
21 *
22 * @category   Pop
23 * @package    Pop\Pdf
24 * @author     Nick Sagona, III <dev@nolainteractive.com>
25 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
26 * @license    http://www.popphp.org/license     New BSD License
27 * @version    5.0.0
28 */
29class Pdf
30{
31
32    /**
33     * Write document to file
34     *
35     * @param  AbstractDocument $document
36     * @param  string           $filename
37     * @return void
38     */
39    public static function writeToFile(AbstractDocument $document, string $filename = 'pop.pdf'): void
40    {
41        $compiler = new Build\Compiler();
42        $compiler->finalize($document);
43        file_put_contents($filename, $compiler->getOutput());
44    }
45
46    /**
47     * Output to HTTP response
48     *
49     * @param  AbstractDocument $document
50     * @param  string           $filename
51     * @param  bool             $forceDownload
52     * @param  array            $headers
53     * @return void
54     */
55    public static function outputToHttp(
56        AbstractDocument $document, string $filename = 'pop.pdf', bool $forceDownload = false, array $headers = []
57    ): void
58    {
59        $headers['Content-type']        = 'application/pdf';
60        $headers['Content-disposition'] = (($forceDownload) ? 'attachment; ' : null) . 'filename=' . $filename;
61
62        $compiler = new Build\Compiler();
63        $compiler->finalize($document);
64
65        // Send the headers and output the PDF
66        if (!headers_sent()) {
67            header('HTTP/1.1 200 OK');
68            foreach ($headers as $name => $value) {
69                header($name . ': ' . $value);
70            }
71        }
72
73        echo $compiler->getOutput();
74    }
75
76    /**
77     * Import from an existing PDF file
78     *
79     * @param  string $file
80     * @param  mixed  $pages
81     * @return AbstractDocument
82     */
83    public static function importFromFile(string $file, mixed $pages = null): AbstractDocument
84    {
85        $parser = new Build\Parser();
86        return $parser->parseFile($file, $pages);
87    }
88
89    /**
90     * Import from raw data stream
91     *
92     * @param  string $data
93     * @param  mixed  $pages
94     * @return AbstractDocument
95     */
96    public static function importRawData(string $data, mixed $pages = null): AbstractDocument
97    {
98        $parser = new Build\Parser();
99        return $parser->parseData($data, $pages);
100    }
101
102    /**
103     * Import from an existing PDF file
104     *
105     * @param  string|array $images
106     * @param  int          $quality
107     * @throws Exception
108     * @return AbstractDocument
109     */
110    public static function importFromImages(string|array $images, int $quality = 70): AbstractDocument
111    {
112        if (!is_array($images)) {
113            $images = [$images];
114        }
115
116        $document = new Document();
117
118        foreach ($images as $image) {
119            $document->addPage(Document\Page::createFromImage($image, $quality));
120        }
121
122        return $document;
123    }
124
125}