Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
44 / 44 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| |
100.00% |
44 / 44 |
|
100.00% |
7 / 7 |
20 | |
100.00% |
1 / 1 |
|
| writeToFile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| outputToHttp | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| importFromFile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| importRawData | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| importFromImages | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| extractTextFromFile | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| extractTextFromData | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Pdf; |
| 15 | |
| 16 | use Pop\Pdf\Document\AbstractDocument; |
| 17 | use Pop\Pdf\Document\Exception; |
| 18 | |
| 19 | /** |
| 20 | * Pop Pdf class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Pdf |
| 24 | * @author Nick Sagona, III <dev@noladev.com> |
| 25 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 5.2.2 |
| 28 | */ |
| 29 | class 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 | /** |
| 126 | * Extract text from file |
| 127 | * |
| 128 | * @param string $file |
| 129 | * @param mixed $pages |
| 130 | * @param ?int $pageLimit |
| 131 | * @return string |
| 132 | */ |
| 133 | public static function extractTextFromFile(string $file, mixed $pages = null, ?int $pageLimit = null): string |
| 134 | { |
| 135 | $parser = new \Smalot\PdfParser\Parser(); |
| 136 | $document = $parser->parseFile($file); |
| 137 | |
| 138 | if ($pages !== null) { |
| 139 | $text = ''; |
| 140 | $pages = (!is_array($pages)) ? [$pages] : $pages; |
| 141 | $docPages = $document->getPages(); |
| 142 | |
| 143 | foreach ($docPages as $i => $docPage) { |
| 144 | if (in_array(($i + 1), $pages)) { |
| 145 | $text .= $docPage->getText(); |
| 146 | } |
| 147 | } |
| 148 | } else { |
| 149 | $text = $document->getText($pageLimit); |
| 150 | } |
| 151 | |
| 152 | return $text; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Extract text from raw data stream |
| 157 | * |
| 158 | * @param string $data |
| 159 | * @param mixed $pages |
| 160 | * @param ?int $pageLimit |
| 161 | * @return string |
| 162 | */ |
| 163 | public static function extractTextFromData(string $data, mixed $pages = null, ?int $pageLimit = null): string |
| 164 | { |
| 165 | $parser = new \Smalot\PdfParser\Parser(); |
| 166 | $document = $parser->parseContent($data); |
| 167 | |
| 168 | if ($pages !== null) { |
| 169 | $text = ''; |
| 170 | $pages = (!is_array($pages)) ? [$pages] : $pages; |
| 171 | $docPages = $document->getPages(); |
| 172 | |
| 173 | foreach ($docPages as $i => $docPage) { |
| 174 | if (in_array(($i + 1), $pages)) { |
| 175 | $text .= $docPage->getText(); |
| 176 | } |
| 177 | } |
| 178 | } else { |
| 179 | $text = $document->getText($pageLimit); |
| 180 | } |
| 181 | |
| 182 | return $text; |
| 183 | } |
| 184 | |
| 185 | } |