Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
PdfParser
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
342
0.00% covered (danger)
0.00%
0 / 1
 pdf2text
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
342
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Utils\Parser\Pdf
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Utils\Parser\Pdf;
16
17use phpOMS\Ai\Ocr\Tesseract\TesseractOcr;
18use phpOMS\System\SystemUtils;
19use phpOMS\Utils\StringUtils;
20
21/**
22 * Pdf parser class.
23 *
24 * @package phpOMS\Utils\Parser\Pdf
25 * @license OMS License 2.0
26 * @link    https://jingga.app
27 * @since   1.0.0
28 */
29class PdfParser
30{
31    /**
32     * PDFToText path.
33     *
34     * @var string
35     * @var 1.0.0
36     */
37    public static string $pdftotext = '/usr/bin/pdftotext';
38
39    /**
40     * PDFToPPM path.
41     *
42     * @var string
43     * @var 1.0.0
44     */
45    public static string $pdftoppm = '/usr/bin/pdftoppm';
46
47    /**
48     * Pdf to text
49     *
50     * @param string $path Pdf path
51     *
52     * @return string
53     *
54     * @since 1.0.0
55     */
56    public static function pdf2text(string $path, string $optimizer = '') : string
57    {
58        $text   = '';
59        $tmpDir = \sys_get_temp_dir();
60
61        $out = \tempnam($tmpDir, 'oms_pdf_');
62        if ($out === false) {
63            return '';
64        }
65
66        if (\is_file(self::$pdftotext)) {
67            try {
68                SystemUtils::runProc(
69                    self::$pdftotext, '-layout '
70                        . \escapeshellarg($path) . ' '
71                        . \escapeshellarg($out)
72                );
73            } catch (\Throwable $_) {
74                \unlink($out);
75
76                return '';
77            }
78        }
79
80        $text = \file_get_contents($out);
81        \unlink($out);
82
83        if ($text === false) {
84            $text = '';
85        }
86
87        if (\strlen($text) > 255) {
88            return $text;
89        }
90
91        $out = \tempnam($tmpDir, 'oms_pdf_');
92        if ($out === false) {
93            return '';
94        }
95
96        if (\is_file(self::$pdftoppm)) {
97            try {
98                SystemUtils::runProc(
99                    self::$pdftoppm,
100                    '-jpeg -r 300 '
101                        . \escapeshellarg($path) . ' '
102                        . \escapeshellarg($out)
103                );
104            } catch (\Throwable $_) {
105                \unlink($out);
106
107                return '';
108            }
109        }
110
111        $files = \glob($out . '*');
112        if ($files === false) {
113            \unlink($out);
114
115            return $text === false ? '' : $text;
116        }
117
118        foreach ($files as $file) {
119            if (!StringUtils::endsWith($file, '.jpg')
120                && !StringUtils::endsWith($file, '.png')
121                && !StringUtils::endsWith($file, '.gif')
122            ) {
123                continue;
124            }
125
126            /* Too slow
127            Thresholding::integralThresholding($file, $file);
128            Skew::autoRotate($file, $file, 10);
129            */
130
131            if (!empty($optimizer) && \is_file($optimizer)) {
132                try {
133                    SystemUtils::runProc(
134                        $optimizer,
135                        \escapeshellarg($file) . ' '
136                        . \escapeshellarg($file)
137                    );
138                } catch (\Throwable $_) {
139                    \unlink($file);
140
141                    continue;
142                }
143            }
144
145            $ocr  = new TesseractOcr();
146            $text = $ocr->parseImage($file);
147
148            \unlink($file);
149        }
150
151        \unlink($out);
152
153        return $text;
154    }
155}