-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfgen.php
95 lines (71 loc) · 3.06 KB
/
pdfgen.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
<?php
require_once C::get('sharedir', 'cfg') . DIRECTORY_SEPARATOR . 'plugins/custom/pdfgen/vendor/autoload.php';
use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\ClientException;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\HTMLRequest;
use TheCodingMachine\Gotenberg\URLRequest;
use TheCodingMachine\Gotenberg\Request;
use TheCodingMachine\Gotenberg\RequestException;
use GuzzleHttp\Psr7\LazyOpenStream;
class pdfgen extends Plugins {
public function enableAction (&$context, &$error) {
if(!parent::_checkRights(LEVEL_ADMINLODEL)) { return; }
}
public function disableAction (&$context, &$error) {
if(!parent::_checkRights(LEVEL_ADMINLODEL)) { return; }
}
public function preview (&$context) {
// Set [#PDFGEN_URL] if pdfgen is ready
if ($this->_config['gotenberg_url']) {
C::set('pdfgen_url', "${context['siteurl']}/?do=_pdfgen_get&document=${context['id']}&lang=${context['sitelang']}");
}
if ($context['view']['tpl'] != 'pdfgen') return;
C::set('view.base_rep.pdfgen', 'pdfgen');
}
public function viewAction (&$context, &$error) {
View::getView()->renderCached('pdfgen');
return "_ajax";
}
public function getAction (&$context, &$error) {
global $db;
setlocale(LC_ALL, 'C');
// Configuration de Gotenberg
$waitDelay = 5;
$waitTimeout = 30;
$site_name = $context['siteinfos']['name'];
$id = $context['document'];
$document = DAO::getDAO("entities")->getById($id);
$last_child = $db->getRow(lq("SELECT id, upd FROM entities INNER JOIN relations ON relations.id2 = entities.id WHERE relations.id1 = {$document->id} ORDER BY upd DESC"));
$cache_disabled = isset($this->_config['cache_disabled']['value']) ? $this->_config['cache_disabled']['value'] : false;
$clearcache = $cache_disabled || (C::get('editor', 'lodeluser') && isset($context['clearcache'])) ? true : false;
$lang = isset($context['lang']) ? $context['lang'] : "";
$debug = isset($context['debug']) ? $context['debug'] : "";
$cache_path = getcwd() . DIRECTORY_SEPARATOR . 'CACHE';
if ( ! is_dir($cache_path) ) {
mkdir($cache_path, 0755, TRUE);
}
$article_url = "${context['siteurl']}/?do=_pdfgen_view&document=${id}&lang=${lang}&debug=${debug}";
$cache_key = md5($article_url);
$cache_file = $cache_path . DIRECTORY_SEPARATOR . $cache_key;
$clearcache = $clearcache
|| ! file_exists( $cache_file )
|| filemtime( $cache_file ) < strtotime($document->upd)
|| sizeof($last_child) > 0 && filemtime( $cache_file ) < strtotime($last_child['upd']);
if ( $clearcache ) {
$client = new Client($this->_config['gotenberg_url']['value']);
$request = new URLRequest($article_url);
$request->setWaitDelay($waitDelay);
$request->setWaitTimeout($waitTimeout);
$request->setPaperSize(Request::A4);
$request->setMargins(Request::NO_MARGINS);
$client->store($request, $cache_file);
$client->post($request);
}
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=${site_name}-${id}.pdf");
echo file_get_contents($cache_file);
return "_ajax";
}
}
?>