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