-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppropediaPDF.php
More file actions
129 lines (116 loc) · 3.59 KB
/
Copy pathAppropediaPDF.php
File metadata and controls
129 lines (116 loc) · 3.59 KB
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
<?php
use chillerlan\QRCode\QRCode;
use MediaWiki\MediaWikiServices;
use MediaWiki\Rest\SimpleHandler;
use MediaWiki\Rest\Stream;
use MediaWiki\Title\Title;
use Wikimedia\ParamValidator\ParamValidator;
/**
* This endpoint returns a PDF containing a specified set of pages
* @todo Maybe switch from wkhtmltopdf to dompdf or mPDF or headless Chrome
*/
class AppropediaPDF extends SimpleHandler {
public function run() {
// Get the relevant params
$params = $this->getValidatedParams();
$pages = $params['pages'];
$title = $params['title'];
$subtitle = $params['subtitle'];
$logo = $params['logo'];
$qrpage = $params['qrpage'];
// Set some paths
$tempDir = wfTempDir();
$coverPath = $tempDir . '/cover.html';
$pdfPath = $tempDir . '/temp.pdf';
// Start building the command
$command = 'wkhtmltopdf';
$command .= ' --print-media-type';
$command .= ' --user-style-sheet ' . __DIR__ . '/resources/AppropediaPDF.css';
$command .= ' --footer-center [page]';
// Set the cover
$html = '<!DOCTYPE HTML>';
$html .= '<html>';
$html .= '<head>';
$html .= '<meta charset="utf-8">';
$html .= '<title>' . $title . '</title>';
$html .= '</head>';
$html .= '<body id="cover">';
$html .= '<header>';
if ( $logo ) {
$html .= '<img id="cover-logo" src="' . $logo . '" width="100" />';
}
$html .= '<h1 id="cover-title">' . $title . '</h1>';
if ( $subtitle ) {
$html .= '<p id="cover-subtitle">' . $subtitle . '</p>';
}
$html .= '</header>';
if ( $qrpage ) {
$qrpage = str_replace( ' ', '_', $qrpage );
$qrcode = new QRCode;
$src = $qrcode->render( 'https://www.appropedia.org/' . $qrpage );
$html .= '<img id="cover-qrcode" src="' . $src . '" />';
}
$html .= '<footer>';
$html .= '<img id="cover-appropedia" src="https://www.appropedia.org/logos/Appropedia-logo.png" />';
$html .= '</footer>';
$html .= '</body>';
$html .= '</html>';
file_put_contents( $coverPath, $html );
$command .= ' cover ' . $coverPath;
// Set the pages
$pages = explode( ',', $pages );
foreach ( $pages as $page ) {
$page = urldecode( $page );
$page = trim( $page );
$page = str_replace( ' ', '_', $page );
$page = urlencode( $page );
$url = "https://www.appropedia.org/$page";
$command .= " $url";
}
// Set the output
$command .= ' ' . $pdfPath;
// Make the PDF
exec( $command );
$pdfResource = fopen( $pdfPath, 'rb' );
// Clean up
unlink( $pdfPath );
unlink( $coverPath );
// Return the PDF
$responseFactory = $this->getResponseFactory();
$response = $responseFactory->createNoContent();
$response->setStatus( 200 );
$response->setHeader( 'Content-Type', 'application/pdf' );
$response->setHeader( 'Content-Length', (string)filesize( $pdfPath ) );
$response->setHeader( 'Content-Disposition', 'attachment; filename=' . $title . '.pdf' );
$stream = new Stream( $pdfResource );
$response->setBody( $stream );
return $response;
}
/** @inheritDoc */
public function getParamSettings() {
return [
'pages' => [
self::PARAM_SOURCE => 'query',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => true
],
'title' => [
self::PARAM_SOURCE => 'query',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => true
],
'subtitle' => [
self::PARAM_SOURCE => 'query',
ParamValidator::PARAM_TYPE => 'string'
],
'logo' => [
self::PARAM_SOURCE => 'query',
ParamValidator::PARAM_TYPE => 'string'
],
'qrpage' => [
self::PARAM_SOURCE => 'query',
ParamValidator::PARAM_TYPE => 'string'
]
];
}
}