-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPdfTocPlugin.php
executable file
·289 lines (268 loc) · 8.84 KB
/
PdfTocPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* PDF ToC
*
* Adapted from PDF Text plugin by Roy Rosenzweig Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
*
* This plugin is to be combined with Internet Archive Bookreader plugin
* to allow table of contents browsing within the viewer.
*
* The PDF Text plugin.
*
* @package Omeka\Plugins\PdfToc
*/
class PdfTocPlugin extends Omeka_Plugin_AbstractPlugin
{
const ELEMENT_SET_NAME = 'PDF Table of Contents';
const ELEMENT_NAME = 'Text';
protected $_hooks = array(
'install',
'uninstall',
'config_form',
'config',
'before_save_file',
'toc_for_bookreader',
'toc_for_public_show',
);
protected $_pdfMimeTypes = array(
'application/pdf',
'application/x-pdf',
'application/acrobat',
'text/x-pdf',
'text/pdf',
'applications/vnd.pdf',
);
/**
* Install the plugin.
*/
public function hookInstall()
{
// Don't install if the pdftk command doesn't exist.
// See: http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
if ((int) shell_exec('hash pdftk 2>&- || echo -1')) {
throw new Omeka_Plugin_Installer_Exception(__('The pdftk command-line utility '
. 'is not installed. pdftk must be installed to install this plugin.'));
}
// Don't install if a PDF element set already exists.
if ($this->_db->getTable('ElementSet')->findByName(self::ELEMENT_SET_NAME)) {
throw new Omeka_Plugin_Installer_Exception(__('An element set by the name "%s" already '
. 'exists. You must delete that element set to install this plugin.', self::ELEMENT_SET_NAME));
}
insert_element_set(
array('name' => self::ELEMENT_SET_NAME, 'record_type' => 'File'),
array(array('name' => self::ELEMENT_NAME))
);
}
/**
* Uninstall the plugin
*/
public function hookUninstall()
{
// Delete the PDF element set.
$this->_db->getTable('ElementSet')->findByName(self::ELEMENT_SET_NAME)->delete();
}
/**
* Display the config form.
*/
public function hookConfigForm()
{
echo get_view()->partial(
'plugins/pdf-toc-config-form.php',
array('valid_storage_adapter' => $this->isValidStorageAdapter())
);
}
/**
* Handle the config form.
*/
public function hookConfig()
{
// Run the text extraction process if directed to do so.
if ($_POST['pdf_toc_process'] && $this->isValidStorageAdapter()) {
Zend_Registry::get('bootstrap')->getResource('jobs')
->sendLongRunning('PdfTocProcess');
}
}
/**
* Add the PDF text to the file record.
*
* This has a secondary effect of including the text in the search index.
*/
public function hookBeforeSaveFile($args)
{
// Extract table of contents only on file insert.
if (!$args['insert']) {
return;
}
$file = $args['record'];
// Ignore non-PDF files.
if (!in_array($file->mime_type, $this->_pdfMimeTypes)) {
return;
}
// Add the PDF toc to the file record.
$element = $file->getElement(self::ELEMENT_SET_NAME, self::ELEMENT_NAME);
$toc = $this->pdfToc($file->getPath());
// pdftoc must return a string to be saved to the element_texts table.
$file->addTextForElement($element, $toc);
}
/**
* Extract the table of contents from a PDF file.
*
* @param string $path
* @return string
*/
public function pdfToc($path)
{
$path = escapeshellarg($path);
// dump_data PDFtk parameter encodes non-ASCII charaters as XML numerical entities
// which are displayed as it stands in the Universal Viewer. e.g PDF bookmark "Scène première"
// will be displayed as "Scène Première" in the UV index (generated from a JSON file).
// This problem does not occurs with BookReader which generates its TOC in HTML.
$dump_data = shell_exec("pdftk $path dump_data_utf8");
if (is_string($dump_data)) {
//prepare Toc. Tested with PDFtk 2.02.
$dump_data = preg_replace("/^.*(Bookmark.*)$/isU", "$1", $dump_data);
$dump_data = preg_replace("/BookmarkBegin\n/isU", "", $dump_data);
$dump_data = preg_replace("/(^.*)PageMediaBegin.*$/isU", "$1", $dump_data);
$dump_data_array = preg_split("/\n/", $dump_data);
$toc = "";
for ($i = 0; $i <= sizeof($dump_data_array); $i+=3)
{
// Bookmarks created with Acrobat Pro may contain carriage returns
// i.e. \r (displayed when using PDFtk dump_data instead of PDFtk dump_data_utf8).
$bm_title = str_replace("BookmarkTitle: ", "", preg_replace("/\r/", "", $dump_data_array[$i]));
// Deletion of spare spaces before and after the titles (e.g. " Le Horla ").
$bm_title = preg_replace("/ *$/", "", $bm_title);
$bm_title = preg_replace("/^ */", "", $bm_title);
$bm_level = str_replace("BookmarkLevel: ", "", $dump_data_array[$i+1]);
$bm_page = str_replace("BookmarkPageNumber: ", "", $dump_data_array[$i+2]);
if ($toc != "")
{
$toc .= "\n";
}
if ( ($bm_level != "") and ($bm_title != "") and ($bm_page != "") )
{
$toc .= $bm_level."|".$bm_title."|".$bm_page;
}
}
return $toc;
}
}
/**
* Determine if the plugin supports the storage adapter.
*
*
* @return bool
*/
public function isValidStorageAdapter()
{
$storageAdapter = Zend_Registry::get('bootstrap')
->getResource('storage')->getAdapter();
if (!($storageAdapter instanceof Omeka_Storage_Adapter_Filesystem)) {
return false;
}
return true;
}
/**
* Get the PDF MIME types.
*
* @return array
*/
public function getPdfMimeTypes()
{
return $this->_pdfMimeTypes;
}
/**
* Display Table of Contents for BookReader.
*
* @param array $args
* Two specific arguments:
* - (integer) page: set the page to be shown when including the iframe,
* - (boolean) embed_functions: allow user to include an iframe with all
* functions (Zoom, Search...). Can be used to include a better viewer
* into items/views.php without requiring user to use the full viewer.
*
* @return void
*/
public function hookTocForBookreader($args)
{
$args["toc_view"] = 'views' . DIRECTORY_SEPARATOR . 'public'. DIRECTORY_SEPARATOR . 'bookreader_toc.php';
$this->formatToc($args);
}
/**
* Display Table of Contents for Public show
*
* @param array $args
* Two specific arguments:
* - (integer) page: set the page to be shown when including the iframe,
* - (boolean) embed_functions: allow user to include an iframe with all
* functions (Zoom, Search...). Can be used to include a better viewer
* into items/views.php without requiring user to use the full viewer.
*
* @return void
*/
public function hookTocForPublicShow($args)
{
$args["toc_view"] = 'views' . DIRECTORY_SEPARATOR . 'public'. DIRECTORY_SEPARATOR . 'public_toc.php';
$this->formatToc($args);
}
private function formatToc($args)
{
$view = $args['view'];
$item = isset($args['item']) && !empty($args['item'])
? $args['item']
: $view->item;
$files = $item->getFiles();
foreach ($files as $file) {
if (in_array($file->mime_type, $this->_pdfMimeTypes)) {
$textElement = $file->getElementTexts(
self::ELEMENT_SET_NAME,
self::ELEMENT_NAME
);
$toc = $textElement[0];
if (preg_match("/InfoValue/", $toc))
{
return;
}
$sortie = "";
$toc = rtrim($toc);
$tab_toc = preg_split("/\n/", $toc);
$niveau_pdt = "";
$total = (count($tab_toc)-1);
for ($i = 0; $i <= $total; $i++)
{
$tab_ligne = preg_split("/\|/", $tab_toc[$i]);
$niveau = $tab_ligne[0];
$titre = $tab_ligne[1];
$page = $tab_ligne[2];
if ($niveau_pdt == "")
{
// Première ligne
$sortie .= "<ul>";
}
elseif ($niveau_pdt < $niveau)
{
for ($k = $niveau_pdt; $k < $niveau; $k++)
{
$sortie .= "<ul>\n";
}
}
elseif ($niveau_pdt > $niveau)
{
for ($k = $niveau_pdt; $k > $niveau; $k--)
{
$sortie .= "</ul>\n";
}
}
if ($page)
{
$sortie .= "<li><a onclick='javascript:br.jumpToIndex(".($page - 1).");return false;' href='".url()."?page=".($page - 1)."#lire-doc'>".$titre."</a></li>\n";
}
$niveau_pdt = $niveau;
}
$toc = $sortie ;
include_once $args["toc_view"];
}
}
}
}