forked from moodle/moodle
/
pdf.php
538 lines (486 loc) · 19.2 KB
/
pdf.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library code for manipulating PDFs
*
* @package assignfeedback_editpdf
* @copyright 2012 Davo Smith
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace assignfeedback_editpdf;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/pdflib.php');
require_once($CFG->dirroot.'/mod/assign/feedback/editpdf/fpdi/fpdi.php');
/**
* Library code for manipulating PDFs
*
* @package assignfeedback_editpdf
* @copyright 2012 Davo Smith
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class pdf extends \FPDI {
/** @var int the number of the current page in the PDF being processed */
protected $currentpage = 0;
/** @var int the total number of pages in the PDF being processed */
protected $pagecount = 0;
/** @var float used to scale the pixel position of annotations (in the database) to the position in the final PDF */
protected $scale = 0.0;
/** @var string the path in which to store generated page images */
protected $imagefolder = null;
/** @var string the path to the PDF currently being processed */
protected $filename = null;
/** No errors */
const GSPATH_OK = 'ok';
/** Not set */
const GSPATH_EMPTY = 'empty';
/** Does not exist */
const GSPATH_DOESNOTEXIST = 'doesnotexist';
/** Is a dir */
const GSPATH_ISDIR = 'isdir';
/** Not executable */
const GSPATH_NOTEXECUTABLE = 'notexecutable';
/** Test file missing */
const GSPATH_NOTESTFILE = 'notestfile';
/** Any other error */
const GSPATH_ERROR = 'error';
/**
* Combine the given PDF files into a single PDF. Optionally add a coversheet and coversheet fields.
* @param string[] $pdflist the filenames of the files to combine
* @param string $outfilename the filename to write to
* @return int the number of pages in the combined PDF
*/
public function combine_pdfs($pdflist, $outfilename) {
$this->setPageUnit('pt');
$this->setPrintHeader(false);
$this->setPrintFooter(false);
$this->scale = 72.0 / 100.0;
$this->SetFont('helvetica', '', 16.0 * $this->scale);
$this->SetTextColor(0, 0, 0);
$totalpagecount = 0;
foreach ($pdflist as $file) {
$pagecount = $this->setSourceFile($file);
$totalpagecount += $pagecount;
for ($i = 1; $i<=$pagecount; $i++) {
$this->create_page_from_source($i);
}
}
$this->save_pdf($outfilename);
return $totalpagecount;
}
/**
* The number of the current page in the PDF being processed
* @return int
*/
public function current_page() {
return $this->currentpage;
}
/**
* The total number of pages in the PDF being processed
* @return int
*/
public function page_count() {
return $this->pagecount;
}
/**
* Load the specified PDF and set the initial output configuration
* Used when processing comments and outputting a new PDF
* @param string $filename the path to the PDF to load
* @return int the number of pages in the PDF
*/
public function load_pdf($filename) {
$this->setPageUnit('pt');
$this->scale = 72.0 / 100.0;
$this->SetFont('helvetica', '', 16.0 * $this->scale);
$this->SetFillColor(255, 255, 176);
$this->SetDrawColor(0, 0, 0);
$this->SetLineWidth(1.0 * $this->scale);
$this->SetTextColor(0, 0, 0);
$this->setPrintHeader(false);
$this->setPrintFooter(false);
$this->pagecount = $this->setSourceFile($filename);
$this->filename = $filename;
return $this->pagecount;
}
/**
* Sets the name of the PDF to process, but only loads the file if the
* pagecount is zero (in order to count the number of pages)
* Used when generating page images (but not a new PDF)
* @param string $filename the path to the PDF to process
* @param int $pagecount optional the number of pages in the PDF, if known
* @return int the number of pages in the PDF
*/
public function set_pdf($filename, $pagecount = 0) {
if ($pagecount == 0) {
return $this->load_pdf($filename);
} else {
$this->filename = $filename;
$this->pagecount = $pagecount;
return $pagecount;
}
}
/**
* Copy the next page from the source file and set it as the current page
* @return bool true if successful
*/
public function copy_page() {
if (!$this->filename) {
return false;
}
if ($this->currentpage>=$this->pagecount) {
return false;
}
$this->currentpage++;
$this->create_page_from_source($this->currentpage);
return true;
}
/**
* Create a page from a source PDF.
*
* @param int $pageno
*/
protected function create_page_from_source($pageno) {
// Get the size (and deduce the orientation) of the next page.
$template = $this->importPage($pageno);
$size = $this->getTemplateSize($template);
$orientation = 'P';
if ($size['w'] > $size['h']) {
$orientation = 'L';
}
// Create a page of the required size / orientation.
$this->AddPage($orientation, array($size['w'], $size['h']));
// Prevent new page creation when comments are at the bottom of a page.
$this->setPageOrientation($orientation, false, 0);
// Fill in the page with the original contents from the student.
$this->useTemplate($template);
}
/**
* Copy all the remaining pages in the file
*/
public function copy_remaining_pages() {
$morepages = true;
while ($morepages) {
$morepages = $this->copy_page();
}
}
/**
* Add a comment to the current page
* @param string $text the text of the comment
* @param int $x the x-coordinate of the comment (in pixels)
* @param int $y the y-coordinate of the comment (in pixels)
* @param int $width the width of the comment (in pixels)
* @param string $colour optional the background colour of the comment (red, yellow, green, blue, white, clear)
* @return bool true if successful (always)
*/
public function add_comment($text, $x, $y, $width, $colour = 'yellow') {
if (!$this->filename) {
return false;
}
switch ($colour) {
case 'red':
$this->SetFillColor(255, 176, 176);
break;
case 'green':
$this->SetFillColor(176, 255, 176);
break;
case 'blue':
$this->SetFillColor(208, 208, 255);
break;
case 'white':
$this->SetFillColor(255, 255, 255);
break;
default: /* Yellow */
$this->SetFillColor(255, 255, 176);
break;
}
$x *= $this->scale;
$y *= $this->scale;
$width *= $this->scale;
$text = str_replace('<', '<', $text);
$text = str_replace('>', '>', $text);
// Draw the text with a border, but no background colour (using a background colour would cause the fill to
// appear behind any existing content on the page, hence the extra filled rectangle drawn below).
$this->MultiCell($width, 1.0, $text, 0, 'L', 0, 4, $x, $y); /* width, height, text, border, justify, fill, ln, x, y */
if ($colour != 'clear') {
$newy = $this->GetY();
// Now we know the final size of the comment, draw a rectangle with the background colour.
$this->Rect($x, $y, $width, $newy - $y, 'DF');
// Re-draw the text over the top of the background rectangle.
$this->MultiCell($width, 1.0, $text, 0, 'L', 0, 4, $x, $y); /* width, height, text, border, justify, fill, ln, x, y */
}
return true;
}
/**
* Add an annotation to the current page
* @param int $sx starting x-coordinate (in pixels)
* @param int $sy starting y-coordinate (in pixels)
* @param int $ex ending x-coordinate (in pixels)
* @param int $ey ending y-coordinate (in pixels)
* @param string $colour optional the colour of the annotation (red, yellow, green, blue, white, black)
* @param string $type optional the type of annotation (line, oval, rectangle, highlight, pen, stamp)
* @param int[]|string $path optional for 'pen' annotations this is an array of x and y coordinates for
* the line, for 'stamp' annotations it is the name of the stamp file (without the path)
* @param string $imagefolder - Folder containing stamp images.
* @return bool true if successful (always)
*/
public function add_annotation($sx, $sy, $ex, $ey, $colour = 'yellow', $type = 'line', $path, $imagefolder) {
global $CFG;
if (!$this->filename) {
return false;
}
switch ($colour) {
case 'yellow':
$colourarray = array(255, 255, 0);
break;
case 'green':
$colourarray = array(0, 255, 0);
break;
case 'blue':
$colourarray = array(0, 0, 255);
break;
case 'white':
$colourarray = array(255, 255, 255);
break;
case 'black':
$colourarray = array(0, 0, 0);
break;
default: /* Red */
$colour = 'red';
$colourarray = array(255, 0, 0);
break;
}
$this->SetDrawColorArray($colourarray);
$sx *= $this->scale;
$sy *= $this->scale;
$ex *= $this->scale;
$ey *= $this->scale;
$this->SetLineWidth(3.0 * $this->scale);
switch ($type) {
case 'oval':
$rx = abs($sx - $ex) / 2;
$ry = abs($sy - $ey) / 2;
$sx = min($sx, $ex) + $rx;
$sy = min($sy, $ey) + $ry;
$this->Ellipse($sx, $sy, $rx, $ry);
break;
case 'rectangle':
$w = abs($sx - $ex);
$h = abs($sy - $ey);
$sx = min($sx, $ex);
$sy = min($sy, $ey);
$this->Rect($sx, $sy, $w, $h);
break;
case 'highlight':
$w = abs($sx - $ex);
$h = 8.0 * $this->scale;
$sx = min($sx, $ex);
$sy = min($sy, $ey) + ($h * 0.5);
$this->SetAlpha(0.5, 'Normal', 0.5, 'Normal');
$this->SetLineWidth(8.0 * $this->scale);
$this->Rect($sx, $sy, $w, $h);
$this->SetAlpha(1.0, 'Normal', 1.0, 'Normal');
break;
case 'pen':
if ($path) {
$scalepath = array();
$points = preg_split('/[,:]/', $path);
foreach ($points as $point) {
$scalepath[] = intval($point) * $this->scale;
}
$this->PolyLine($scalepath, 'S');
}
break;
case 'stamp':
$imgfile = $imagefolder . '/' . clean_filename($path);
$w = abs($sx - $ex);
$h = abs($sy - $ey);
$sx = min($sx, $ex);
$sy = min($sy, $ey);
$this->Image($imgfile, $sx, $sy, $w, $h);
break;
default: // Line.
$this->Line($sx, $sy, $ex, $ey);
break;
}
$this->SetDrawColor(0, 0, 0);
$this->SetLineWidth(1.0 * $this->scale);
return true;
}
/**
* Save the completed PDF to the given file
* @param string $filename the filename for the PDF (including the full path)
*/
public function save_pdf($filename) {
$this->Output($filename, 'F');
}
/**
* Set the path to the folder in which to generate page image files
* @param string $folder
*/
public function set_image_folder($folder) {
$this->imagefolder = $folder;
}
/**
* Generate an image of the specified page in the PDF
* @param int $pageno the page to generate the image of
* @throws moodle_exception
* @throws coding_exception
* @return string the filename of the generated image
*/
public function get_image($pageno) {
if (!$this->filename) {
throw new \coding_exception('Attempting to generate a page image without first setting the PDF filename');
}
if (!$this->imagefolder) {
throw new \coding_exception('Attempting to generate a page image without first specifying the image output folder');
}
if (!is_dir($this->imagefolder)) {
throw new \coding_exception('The specified image output folder is not a valid folder');
}
$imagefile = $this->imagefolder.'/image_page' . $pageno . '.png';
$generate = true;
if (file_exists($imagefile)) {
if (filemtime($imagefile)>filemtime($this->filename)) {
// Make sure the image is newer than the PDF file.
$generate = false;
}
}
if ($generate) {
// Use ghostscript to generate an image of the specified page.
$gsexec = \get_config('assignfeedback_editpdf', 'gspath');
$imageres = 100;
$filename = $this->filename;
$pagenoinc = $pageno + 1;
$command = "$gsexec -q -sDEVICE=png16m -dSAFER -dBATCH -dNOPAUSE -r$imageres -dFirstPage=$pagenoinc -dLastPage=$pagenoinc ".
"-dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sOutputFile=\"$imagefile\" \"$filename\"";
//$command = escapeshellcmd($command);
$output = null;
$result = exec($command, $output);
if (!file_exists($imagefile)) {
$fullerror = '<pre>'.get_string('command', 'assignfeedback_editpdf')."\n";
$fullerror .= $command . "\n\n";
$fullerror .= get_string('result', 'assignfeedback_editpdf')."\n";
$fullerror .= htmlspecialchars($result) . "\n\n";
$fullerror .= get_string('output', 'assignfeedback_editpdf')."\n";
$fullerror .= htmlspecialchars(implode("\n",$output)) . '</pre>';
throw new \moodle_exception('errorgenerateimage', 'assignfeedback_editpdf', '', $fullerror);
}
}
return 'image_page'.$pageno.'.png';
}
/**
* Check to see if PDF is version 1.4 (or below); if not: use ghostscript to convert it
* @param stored_file $file
* @return string path to copy or converted pdf (false == fail)
*/
public static function ensure_pdf_compatible(\stored_file $file) {
global $CFG;
$fp = $file->get_content_file_handle();
$ident = fread($fp, 10);
if (substr_compare('%PDF-', $ident, 0, 5) !== 0) {
return false; // This is not a PDF file at all.
}
$ident = substr($ident, 5); // Remove the '%PDF-' part.
$ident = explode('\x0A', $ident); // Truncate to first '0a' character.
list($major, $minor) = explode('.', $ident[0]); // Split the major / minor version.
$major = intval($major);
$minor = intval($minor);
if ($major == 0 || $minor == 0) {
return false; // Not a valid PDF version number.
}
$temparea = \make_temp_directory('assignfeedback_editpdf');
$hash = $file->get_contenthash(); // Use the contenthash to make sure the temp files have unique names.
$tempsrc = $temparea . "/src-$hash.pdf";
$tempdst = $temparea . "/dst-$hash.pdf";
if ($major = 1 && $minor<=4) {
// PDF is valid version - just create a copy we can use.
$file->copy_content_to($tempdst); // Copy the file.
return $tempdst;
}
$file->copy_content_to($tempsrc); // Copy the file.
$gsexec = \get_config('assignfeedback_editpdf', 'gspath');
$command = "$gsexec -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=\"$tempdst\" \"$tempsrc\"";
//$command = escapeshellcmd($command);
exec($command);
@unlink($tempsrc);
if (!file_exists($tempdst)) {
// Something has gone wrong in the conversion.
return false;
}
return $tempdst;
}
/**
* Test that the configured path to ghostscript is correct and working.
* @param bool $generateimage - If true - a test image will be generated to verify the install.
* @return bool
*/
public static function test_gs_path($generateimage = true) {
global $CFG;
$ret = (object)array(
'status' => self::GSPATH_OK,
'message' => null,
);
$gspath = \get_config('assignfeedback_editpdf', 'gspath');
if (empty($gspath)) {
$ret->status = self::GSPATH_EMPTY;
return $ret;
}
if (!file_exists($gspath)) {
$ret->status = self::GSPATH_DOESNOTEXIST;
return $ret;
}
if (is_dir($gspath)) {
$ret->status = self::GSPATH_ISDIR;
return $ret;
}
if (!is_executable($gspath)) {
$ret->status = self::GSPATH_NOTEXECUTABLE;
return $ret;
}
$testfile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/testgs.pdf';
if (!file_exists($testfile)) {
$ret->status = self::GSPATH_NOTESTFILE;
return $ret;
}
if (!$generateimage) {
return $ret;
}
$testimagefolder = \make_temp_directory('assignfeedback_editpdf_test');
@unlink($testimagefolder.'/image_page0.png'); // Delete any previous test images.
$pdf = new pdf();
$pdf->set_pdf($testfile);
$pdf->set_image_folder($testimagefolder);
try {
$pdf->get_image(0);
} catch (\moodle_exception $e) {
$ret->status = self::GSPATH_ERROR;
$ret->message = $e->getMessage();
}
return $ret;
}
/**
* If the test image has been generated correctly - send it direct to the browser.
*/
public static function send_test_image() {
global $CFG;
header('Content-type: image/png');
require_once($CFG->libdir.'/filelib.php');
$testimagefolder = \make_temp_directory('assignfeedback_editpdf_test');
$testimage = $testimagefolder.'/image_page0.png';
send_file($testimage, basename($testimage), 0);
die();
}
}