-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneratePDF.php
More file actions
95 lines (84 loc) · 2.49 KB
/
generatePDF.php
File metadata and controls
95 lines (84 loc) · 2.49 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
<?php
/**
* This script generates a PDF containing a specified set of pages.
* See the README for details.
*/
// Debug mode
$debug = $_GET['debug'] ?? false;
if ( $debug ) {
error_reporting( E_ALL );
ini_set( 'display_errors', 1 );
}
// Load the library for generating QR codes
require 'vendor/autoload.php';
use chillerlan\QRCode\QRCode;
// Get the params
$title = $_GET['title'] ?? null;
$subtitle = $_GET['subtitle'] ?? null;
$logo = $_GET['logo'] ?? null;
$logowidth = $_GET['logowidth'] ?? 100;
$qrpage = $_GET['qrpage'] ?? null;
$pages = $_GET['pages'] ?? null;
// If no pages are given, use Appropedia's main page to avoid throwing an error
if ( !$pages ) {
$pages = 'Welcome to Appropedia';
}
// Start building the command
$command = 'wkhtmltopdf';
$command .= ' --print-media-type';
$command .= ' --user-style-sheet generatePDF.css';
$command .= ' --footer-center [page]';
// Set the cover
if ( $title ) {
$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="' . $logowidth . '" />';
}
$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( 'cover.html', $html );
$command .= ' cover cover.html';
}
// 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 .= ' temp.pdf';
exec( $command );
// Download the PDF
$filename = $title ? $title : 'appropedia';
header( 'Content-Type: application/pdf' );
header( 'Content-Disposition: attachment; filename=' . $filename . '.pdf' );
header( 'Access-Control-Allow-Origin: *' );
readfile( 'temp.pdf' );
// Clean up
unlink( 'temp.pdf' );
unlink( 'cover.html' );